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

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.

Related

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

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.

SharePoint 2013 REST API AJAX update workflow task

I need your help.
I'd like to complete custom workflow task, (SH 2010 WF) running over 2013.
I've been using a pice of code. to update a task list using Rest API in JavaScript AJAX.
I test this code with other list and run OK, but When I like to update a task list. I received different error MSG.
If I like to updated Title filed I received ""message":{"lang":"es-ES","value":"Value does not fall within the expected range."}}},"status":400,"statusText":"Bad Request"}"
If I like to Update Result field I can see the filed in properties.
Do you have any conceptual description about how to work with workflow task and their content types using Rest API
Thank in advance
Ramiro
I'll share my code.
function updateJson(endpointUri,payload, success, error)
{
return getFormDigest('https://partner.coca-cola.com/sites/SLBU2013Test/POC').then(function (data) {
$.ajax({
url: endpointUri,
type: "POST",
data: JSON.stringify(payload),
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest" : data.d.GetContextWebInformation.FormDigestValue,
"X-HTTP-Method": "MERGE",
"If-Match": "*"
},
success: success,
error: error
});
});
}
function getItemTypeForListName(name) {
console.log("SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem");
return"SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";
}
function updateListItem(webUrl,listTitle,listItemId,itemProperties,success,failure)
{
var listItemUri = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + listItemId + ")";
console.log(listItemUri);
var itemPayload = {
'__metadata': {'type': 'SP.Data.TasksListItem'}
};
for(var prop in itemProperties){
itemPayload[prop] = itemProperties[prop];
console.log(itemProperties[prop]);
}
updateJson(listItemUri,itemPayload,success,failure);
}
function getFormDigest(webUrl) {
return $.ajax({
url: webUrl + "/_api/contextinfo",
method: "POST",
headers: { "Accept": "application/json; odata=verbose" }
});
}
function Calcular (){
var itemProperties = {'Status':'Completadas'};
updateListItem('https://partner.coca-cola.com/sites/SLBU2013Test/POC','Tasks',2,itemProperties,printInfo,logError);
function printInfo()
{
console.log('Item has been created');
}
function logError(error){
console.log(JSON.stringify(error));
}
};
There is another similar post. My answer there was to do some screen scraping and redirect users to the UI. Short story is that we could not update the list with REST, but could with CSOM. Regardless, the WF ignored the task changes. Here's the link: Update task item programatically in Sharepoint with CSOM.

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";
}
});

Create DocumentSet with REST api

How would one format the rest uri to create a document set in SP?
The JSOM code below works fine, but I would prefer to use the REST in order to be able to call it from a workflow.
var dsresult = SP.DocumentSet.DocumentSet.create(context, parentFolder, "docsetfromjsom", docsetCtId);
I tried this format based on this MSDN article
var restQueryUrl = spAppWebUrl + "/_api/SP.AppContextSite(#target)/SP.DocumentSet.DocumentSet.create('serverrelativeurl','docsetname','ctid')?#target='spHostUrl'";
Tried other formats as well but none successful. In jsom you also need to include the context, but I am assuming that for the rest call you don't need to use it (i think). Anyone tried this before?
Thanks!
I've already answered a similar question at SharePoint StackExchange.
To summarize, it does not seem possible to create Document Set using SharePoint 2013 REST API since SP.DocumentSet.DocumentSet.create function is not accessible via REST. But you could utilize SharePoint 2010 REST API instead for that purpose.
The following example demonstrates how to create a Document Set using SharePoint 2010 REST Interface:
function getListUrl(webUrl,listName)
{
return $.ajax({
url: webUrl + "/_api/lists/getbytitle('" + listName + "')/rootFolder?$select=ServerRelativeUrl",
type: "GET",
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose"
}
});
}
function createFolder(webUrl,listName,folderUrl,folderContentTypeId)
{
return $.ajax({
url: webUrl + "/_vti_bin/listdata.svc/" + listName,
type: "POST",
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose",
"Slug": folderUrl + "|" + folderContentTypeId
}
});
}
function createDocumentSet(webUrl,listName,docSetName)
{
return getListUrl(webUrl,listName)
.then(function(data){
var folderUrl = data.d.ServerRelativeUrl + '/' + docSetName;
return createFolder(webUrl,listName,folderUrl,'0x0120D520');
});
}
Usage
Create Document Set named Orders in Documents library:
createDocumentSet(webUrl,'Documents','Orders')
.done(function(data)
{
console.log('Document Set ' + data.d.Name + ' has been created succesfully');
})
.fail(
function(error){
console.log(JSON.stringify(error));
});

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() }