1

I am trying to update a List Item in Sharepoint using the Rest API but am getting the following error:

The type of data at position 0 is different than the one expected.

Here is my code. The type is correct, the id is correct. The field i'm updating is

var string = "{ '__metadata': { 'type': 'SP.Data.Presales_x0020_StagingListItem' }, '" + stagename + "': 'True'}";
        var id = getParameterByName('id');


$.ajax({
            url: "https://myurl.sharepoint.com/sites/EUBIEPresales/_api/web/lists/getbytitle('Presales Staging')/items(" + id.toString() + ")",
            type: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(string),
            headers: {
                "Authorization": "Bearer " + token
            },
            success: function (data) {
                console.log("Success");

            },
            error: function (data) {
                console.log("Failure");
            }
        });

A couple of things:

  • The Auth token is correct.
  • The Id of the list item is correct
  • The field i'm updating is a choice field

I think it's to do with the type i'm passing in, but I think it's correct. When I do a get I get the following:

<?xml version="1.0" encoding="utf-8"?>
<entry xml:base="https://bicusa.sharepoint.com/sites/EUBIEPresales/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" m:etag="&quot;1&quot;">
<id>8fee51a1-1f5d-47c9-b1dd-d3a347faf1b1</id>
<category term="SP.Data.Presales_x0020_StagingListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<link rel="edit" href="Web/Lists(guid'1364b153-851b-4897-ba61-f8daa59a7bde')/Items(5)" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FirstUniqueAncestorSecurableObject" type="application/atom+xml;type=entry" title="FirstUniqueAncestorSecurableObject" href="Web/Lists(guid'1364b153-851b-4897-ba61-f8daa59a7bde')/Items(5)/FirstUniqueAncestorSecurableObject" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/RoleAssignments" type="application/atom+xml;type=feed" title="RoleAssignments" href="Web/Lists(guid'1364b153-851b-4897-ba61-f8daa59a7bde')/Items(5)/RoleAssignments" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Activities" type="application/atom+xml;type=feed" title="Activities" href="Web/Lists(guid'1364b153-851b-4897-ba61-f8daa59a7bde')/Items(5)/Activities" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/AttachmentFiles" type="application/atom+xml;type=feed" title="AttachmentFiles" href="Web/Lists(guid'1364b153-851b-4897-ba61-f8daa59a7bde')/Items(5)/AttachmentFiles" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ContentType" type="application/atom+xml;type=entry" title="ContentType" href="Web/Lists(guid'1364b153-851b-4897-ba61-f8daa59a7bde')/Items(5)/ContentType" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/GetDlpPolicyTip" type="application/atom+xml;type=entry" title="GetDlpPolicyTip" href="Web/Lists(guid'1364b153-851b-4897-ba61-f8daa59a7bde')/Items(5)/GetDlpPolicyTip" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FieldValuesAsHtml" type="application/atom+xml;type=entry" title="FieldValuesAsHtml" href="Web/Lists(guid'1364b153-851b-4897-ba61-f8daa59a7bde')/Items(5)/FieldValuesAsHtml" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FieldValuesAsText" type="application/atom+xml;type=entry" title="FieldValuesAsText" href="Web/Lists(guid'1364b153-851b-4897-ba61-f8daa59a7bde')/Items(5)/FieldValuesAsText" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FieldValuesForEdit" type="application/atom+xml;type=entry" title="FieldValuesForEdit" href="Web/Lists(guid'1364b153-851b-4897-ba61-f8daa59a7bde')/Items(5)/FieldValuesForEdit" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/File" type="application/atom+xml;type=entry" title="File" href="Web/Lists(guid'1364b153-851b-4897-ba61-f8daa59a7bde')/Items(5)/File" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Folder" type="application/atom+xml;type=entry" title="Folder" href="Web/Lists(guid'1364b153-851b-4897-ba61-f8daa59a7bde')/Items(5)/Folder" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ParentList" type="application/atom+xml;type=entry" title="ParentList" href="Web/Lists(guid'1364b153-851b-4897-ba61-f8daa59a7bde')/Items(5)/ParentList" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Properties" type="application/atom+xml;type=entry" title="Properties" href="Web/Lists(guid'1364b153-851b-4897-ba61-f8daa59a7bde')/Items(5)/Properties" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Versions" type="application/atom+xml;type=feed" title="Versions" href="Web/Lists(guid'1364b153-851b-4897-ba61-f8daa59a7bde')/Items(5)/Versions" />
<title />
<updated>2017-08-31T08:08:10Z</updated>
<author>
    <name />
</author>
<content type="application/xml">
    <m:properties>
        <d:FileSystemObjectType m:type="Edm.Int32">0</d:FileSystemObjectType>
        <d:Id m:type="Edm.Int32">5</d:Id>
        <d:ServerRedirectedEmbedUri m:null="true" />
        <d:ServerRedirectedEmbedUrl></d:ServerRedirectedEmbedUrl>
        <d:ContentTypeId>0x0100C2F8BEC6D3FA244D8B1E7352DD885A0F</d:ContentTypeId>
        <d:Title>Test</d:Title>
        <d:Analyse_Goal_Pain_Points m:null="true" />
        <d:Analyse_Goal_Tech_Info m:null="true" />
        <d:Analyse_Goal_Deploy_Info m:null="true" />
        <d:ID m:type="Edm.Int32">5</d:ID>
        <d:Modified m:type="Edm.DateTime">2017-06-28T13:34:37Z</d:Modified>
        <d:Created m:type="Edm.DateTime">2017-06-28T13:34:37Z</d:Created>
        <d:AuthorId m:type="Edm.Int32">18</d:AuthorId>
        <d:EditorId m:type="Edm.Int32">18</d:EditorId>
        <d:OData__UIVersionString>1.0</d:OData__UIVersionString>
        <d:Attachments m:type="Edm.Boolean">false</d:Attachments>
        <d:GUID m:type="Edm.Guid">7f0e4060-c23c-457e-bee1-b904f058f8a2</d:GUID>
    </m:properties>
</content>

Any pointers as to where I think I should look? When I create new list items, I pass in the same type and they work, so i'm not really sure where to look?

1 Answer 1

0

I think you need to JSON.stringify an object instead of a string, like that :

JSON.stringify({ '__metadata': { 'type': 'SP.Data.Presales_x0020_StagingListItem' }, [stagename]: 'True'});
2
  • It's a good point, but hasn't resolved the issue. It's got to do with the Content Type but I can't see what content type "0x0100C2F8BEC6D3FA244D8B1E7352DD885A0F" is? Commented Aug 31, 2017 at 10:12
  • Well you don't need to specify the ContentType as you are using __metadata. I don't know if it can help but my header looks like this : headers: { "Accept": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "X-HTTP-Method": "MERGE", "If-Match": "*" } Commented Sep 1, 2017 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.