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
- var updateUrl = "http://sp13/sites/dev/_api/Web/GetFileByServerRelativeUrl('/Sites/dev/Lists/DOA/Test/CSM.docx')/ListItemAllFields";
- $.ajax(
- {
- 'url': updateUrl,
- 'method': 'POST',
- 'data': JSON.stringify({
- '__metadata': { 'type': 'SP.ListItem' },
- 'ContentTypeId': '0x0101005C9775F3130F7A498C5C8CF69C1750AE03'
- }),
- 'headers': {
- 'accept': 'application/json;odata=verbose',
- 'content-type': 'application/json;odata=verbose',
- 'X-RequestDigest': $('#__REQUESTDIGEST').val(),
- 'X-Http-Method': 'MERGE',
- "If-Match": "*"
- },
- 'success': function (data) {
- alert('success');
- },
- 'error': function (err) {
- alert('error');
- }
- }
- );
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.
Following is the entity name that I should use in my code
Following is the modified code that works on my on premises environment as well
- var updateUrl = "http://sp13/sites/dev/_api/Web/GetFileByServerRelativeUrl('/Sites/dev/Lists/DOA/Test/CSM.docx')/ListItemAllFields";
- $.ajax(
- {
- 'url': updateUrl,
- 'method': 'POST',
- 'data': JSON.stringify({
- '__metadata': { 'type': 'SP.Data.DOAListItem' },
- 'ContentTypeId': '0x0101005C9775F3130F7A498C5C8CF69C1750AE03'
- }),
- 'headers': {
- 'accept': 'application/json;odata=verbose',
- 'content-type': 'application/json;odata=verbose',
- 'X-RequestDigest': $('#__REQUESTDIGEST').val(),
- 'X-Http-Method': 'MERGE',
- "If-Match": "*"
- },
- 'success': function (data) {
- alert('success');
- },
- 'error': function (err) {
- alert('error');
- }
- }
- );
1 comment:
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...
Post a Comment