SharePoint Rest API - Set Lookup field when [Lookup Name]Id value is already in use - rest

I have a SharePoint list with a number field that has an internal name of [ProjectId]. I later created a lookup column with an internal name of [Project]. I need to use a REST API to update the lookup column value. However, when I use
"ProjectId": 403
It sets the number field. I know SharePoint will append a 0 to a duplicate internal name. However, in this case it was created by the system. I can expand the Lookup column using "Project/Id" in a GET call. I just can't set it.
Does anyone know how to reference the lookup id in this case?
Here is the full code:
// basic patch items
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/lists/GetByTitle('My List')/items(1)",
headers: {
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"IF-MATCH": "*",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
},
data: JSON.stringify({
"__metadata": {
"type": "SP.Data.MyListListItem"
},
"ProjectId": 403
}),
method: 'PATCH',
success: function (data) {
console.log("success");
console.log(data);
},
error: function (data) {
console.log("error");
console.log(data);
}
});
I've tried these as well:
"Project0Id": 403
"ProjectId0": 403
It just returns an error saying the column doesn't exist.

Related

How i can perform a POST request using fireFox RESTClient add-in

I have this function to perform a POST request to a SharePoint Online site collection:-
function SetListColumnReadOnly() {
$.ajax
({
// _spPageContextInfo.webAbsoluteUrl - will give absolute URL of the site where you are running the code.
// You can replace this with other site URL where you want to apply the function
url: "https://****.sharepoint.com/_api/web/lists/getByTitle('tickets')/fields/getbytitle('defineFields')",
method: "POST",
data: JSON.stringify({
'__metadata': {
// Type that you are modifying.
'type': 'SP.FieldText'
},
'ReadOnlyField': true
}),
headers:
{
// IF-MATCH header: Provides a way to verify that the object being changed has not been changed since it was last retrieved.
// "IF-MATCH":"*", will overwrite any modification in the object, since it was last retrieved.
"IF-MATCH": "*",
"X-HTTP-Method": "PATCH",
// Accept header: Specifies the format for response data from the server.
"Accept": "application/json;odata=verbose",
//Content-Type header: Specifies the format of the data that the client is sending to the server
"Content-Type": "application/json;odata=verbose",
// X-RequestDigest header: When you send a POST request, it must include the form digest value in X-RequestDigest header
//"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data, status, xhr) {
console.log("Success");
},
error: function (xhr, status, error) {
console.log("Failed");
}
});}
Now how we can run this POST inside the FireFox's RESTClient addin or anyother tool using the browser credentials?
when i tried this >> i got forbidden error:-
Also with those headers:-

Create a file and add it to a folder with Sharepoint's Api Rest using Postman

I use this call: https://{site_url}/_api/web/GetFolderByServerRelativeUrl('/ Folder Name')/Files/add(url ='a.txt', overwrite = true).The file is inserted correctly but I don't know what to add so that I can fill the rest of the columns of the library of documents.But it can be done in the same call, it doesn't matter. But I need to modify the value in a record of a document library
Warrior,
Do you want to update values of other columns after uploading a file to a library? If so, you may hava a look below demo:
function getItem(file) {
var call = jQuery.ajax({
url: file.ListItemAllFields.__deferred.uri,
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});
return call;
}
function updateItemFields(item) {
var now = new Date();
var call = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl +
"/_api/Web/Lists/getByTitle('Documents')/Items(" +
item.Id + ")",
type: "POST",
data: JSON.stringify({
"__metadata": { type: "SP.Data.DocumentsItem" },
CoordinatorId: _spPageContextInfo.userId,
Year: now.getFullYear()
}),
headers: {
Accept: "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
"IF-MATCH": item.__metadata.etag,
"X-Http-Method": "MERGE"
}
});
return call;
}
You need to put this operation in a separated request.
BR

How to get list items from a folder in custom list using rest API?

I am working with Office 365. I have used REST API for different types of operations. I can easily find list items in a folder in document library because they are files. I want the list items in a folder in custom list. For this I am not able to find any REST API. Can you please provide me any REST API which will be able to retrieve list items from folder in custom list ?
I got the answer. Following is the way
var camlQuery = {
'query': {
'__metadata': { 'type': 'SP.CamlQuery' },
'ViewXml': '<View><Query/></View>',
'FolderServerRelativeUrl': '/sites/EdvelopTest3/Lists/QuestionsList/test/test1'
}
};
var url = OEPContext.appWebUrl + "/_api/SP.AppContextSite(#target)/web/lists/getByTitle('OEPLMSQuestions')/getitems?$select=ID,Title&#target='" + OEPContext.hostWebUrl + "'";
jQuery.ajax({
async: false,
url: url,
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
data: JSON.stringify(camlQuery),
success: function (data) {
var result = "success";
},
error: function (data, msg) {
var result = "Fail";
}
});

SharePoint REST Service Create List Item Error The required version of WcfDataServices is missing.

I am trying to insert an item to a list (just a basic custom list with the title column) using the SharePoint Web Services. This is the code I am using
function GetItemTypeForListName(name) {
return "SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";
}
var itemType = GetItemTypeForListName(lisNameTitle);
var item = {
'__metadata': { 'type': itemType },
'Title': 'another item check if works'
};
var jsonItem = JSON.stringify(item);
alert(jsonItem);
$http({
method: "POST",
url: reportDownloadSubmitDataUrl,
contentType: "application/json;odata=verbose",
data: jsonItem,
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
}
})
when I execute this code, on the SharePoint side logs I get the following errors
"The required version of WcfDataServices is missing. Please refer to http://go.microsoft.com/fwlink/?LinkId=321931 for more information." String
WcfDataServices 5.6 is missing.
I have SharePoint 2013 Service Pack 1 installed on my server.
I found the problem was due to the $http( method in Angularjs. When I used directly jquery to post the result ($.ajax) it worked fine.

403 forbidden and 400 bad request errors while adding and deleting items to SharePoint list using REST

I'm new to SharePoint development. I'm Trying to develop simple SharePoint App using SharePoint online. I have a List named 'Products' in my site collection. In my app I wrote the following code to add and delete items to that list
function addProduct(product) {
var executor;
executor = new SP.RequestExecutor(appwebUrl);
var url = appwebUrl +"/_api/SP.AppContextSite(#target)/web/lists/getbytitle('Products')/items/?#target='" + hostwebUrl+"'";
executor.executeAsync({
url: url,
method: "POST",
body: JSON.stringify({__metadata: { type: 'SP.Data.ProductsListItem' },
Title: product.ProductName(),
ProductId: product.ProductId(),
ProductName: product.ProductName(),
Price:product.Price()
}),
headers: {
"Accept": "application/json; odata=verbose",
"content-type": "application/json;odata=verbose",
},
success: successProductAddHandler,
error: errorProductAddHandler
});
}
function successProductAddHandler(data) {alert('added successfully') }
function errorProductAddHandler(data, errorCode, errorMessage) { alert('cannot perform action') }
function deleteProduct(product) {
var executor;
executor = new SP.RequestExecutor(appwebUrl);
var url=appwebUrl+"/_api/SP.AppContextSite(#target)/web/lists/getbytitle('Products')/items('" + product.ID() + "')/?#target='" + hostwebUrl + "'";
executor.executeAsync({
url: url,
method: "POST",
headers: {
"IF-MATCH": "*",
"X-HTTP-Method": "DELETE"
},
success: successProductAddHandler,
error: errorProductAddHandler
});`
Im getting 403 error code when I call addProduct,
and 400 error code when I call deleteProduct.
I'm able to get the list items and display.
I tried adding X-RequestDigest": $("#__REQUESTDIGEST").val() but it did not work
If I include "Accept": "application/json; odata=verbose" in a request header for deleteProduct(), and when I call deleteProduct, two requests are going to server
/sites/productsdev/productsapp/_api/contextinfo (getting digest value)
/sites/ProductsDev/ProductsApp/_api/SP.AppContextSite(#target)/web/lists/getbytitle('Products')/items(itemid)/?#target='mysitecollectionurl' (using the digest value returned by the above call for X-RequestDigest)
Whenever you are doing any POST operation in SharePoint 2013 using REST API you have to pass below snippet in header
"X-RequestDigest": $("#__REQUESTDIGEST").val()
eg
headers: { "Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val() }