Thursday, September 25, 2014

Resolve “The property does not exist on type 'SP.ListItem'. Make sure to only use property names that are defined by the type” error when posting field values using SharePoint REST api

When I tried to update field data of a document in a document library using REST api I got Bad Request (400) error message. Surprisingly this was the same code which is working on my office 365 environment. Following is the full error message

The property 'ContentTypeId' does not exist on type 'SP.ListItem'. Make sure to only use property names that are defined by the type

Following is the code I used to update the document

  1. var updateUrl = "http://sp13/sites/dev/_api/Web/GetFileByServerRelativeUrl('/Sites/dev/Lists/DOA/Test/CSM.docx')/ListItemAllFields";
  2.  
  3. $.ajax(
  4. {
  5.    'url': updateUrl,
  6.    'method': 'POST',
  7.    'data': JSON.stringify({
  8.        '__metadata': { 'type': 'SP.ListItem' },
  9.        'ContentTypeId': '0x0101005C9775F3130F7A498C5C8CF69C1750AE03'
  10.    }),
  11.    'headers': {
  12.        'accept': 'application/json;odata=verbose',
  13.        'content-type': 'application/json;odata=verbose',
  14.        'X-RequestDigest': $('#__REQUESTDIGEST').val(),
  15.        'X-Http-Method': 'MERGE',
  16.        "If-Match": "*"
  17.    },
  18.    'success': function (data) {
  19.        alert('success');
  20.    },
  21.    'error': function (err) {
  22.        alert('error');
  23.    }
  24.   }
  25. );

The reason for above error is that the type attribute is not compatible in this scenario. I need to provide the exact entity name instead of generic SP.ListItem.

How can I find the entity name for the list? it is very easy. I just need to provide following request.

http://sp13/Sites/dev/_api/lists/getbytitle('DOA')

Following is the entity name that I should use in my code

image

Following is the modified code that works on my on premises environment as well

  1. var updateUrl = "http://sp13/sites/dev/_api/Web/GetFileByServerRelativeUrl('/Sites/dev/Lists/DOA/Test/CSM.docx')/ListItemAllFields";
  2.  
  3. $.ajax(
  4. {
  5.    'url': updateUrl,
  6.    'method': 'POST',
  7.    'data': JSON.stringify({
  8.        '__metadata': { 'type': 'SP.Data.DOAListItem' },
  9.        'ContentTypeId': '0x0101005C9775F3130F7A498C5C8CF69C1750AE03'
  10.    }),
  11.    'headers': {
  12.        'accept': 'application/json;odata=verbose',
  13.        'content-type': 'application/json;odata=verbose',
  14.        'X-RequestDigest': $('#__REQUESTDIGEST').val(),
  15.        'X-Http-Method': 'MERGE',
  16.        "If-Match": "*"
  17.    },
  18.    'success': function (data) {
  19.        alert('success');
  20.    },
  21.    'error': function (err) {
  22.        alert('error');
  23.    }
  24.   }
  25. );

1 comment:

Unknown said...

Hi

I stumbled upon your blog in searches because of this error that only I was getting but nobody else in my team, something you all should watch out for, if you get this error but everything else seems correct, it's most probably your environment, you need to do the SharePoint 2013 Service Pack 1 update...