Pnpjs: ListItem Update not sending __metadata type in Request Body

Created on 27 Nov 2019  路  2Comments  路  Source: pnp/pnpjs

Category

  • [ ] Enhancement
  • [ ] Bug
  • [x] Question
  • [ ] Documentation gap/issue

Version

Please specify what version of the library you are using: [1.3.6]

Please specify what version(s) of SharePoint you are targeting: [onPrem 2016]

Expected / Desired Behavior / Question

It should Update an Item in a CustomList after i call sp.web.lists.getByTitle('test').items.getItemByStringId('1').update(test);

Observed Behavior

Sharepoint just returns an Microsoft.SharePoint.Client.InvalidClientQueryException.

Steps to Reproduce

I manually created a Custom List in Sharepoint 2016 with the name 'test' and added one item with only a Title. Hence the ItemId 1 and the given Name for the List.

// Remove this if you don't have any code sample to provide
import { sp } from "@pnp/sp";
    public static async testUpdate(){
        return await sp.web.lists.getByTitle('test').items.getItemByStringId('1').update({
            'Title': 'Another Title',
        }).then(result => {
            console.log(result);
    });
}

After calling the above function through the workbench (spfx 1.1.0 gulp serve) on my running sharepoint instance (not local) i get the following scenario:

  • it's calling "lists?$select=ListItemEntityTypeFullName" and the response has all data as i would expect
  • it calls "contextinfo' also with data in the response body
  • it calls getItemByStringId("1") and here i get:
{
  "odata.error": {
    "code": "-1, Microsoft.SharePoint.Client.InvalidClientQueryException",
    "message": {
      "lang": "en-US",
      "value": "An entry without a type name was found, but no expected type was specified. To allow entries without type information, the expected type must also be specified when the model is specified."
    }
  }
}

and after inspecting the request body i found this body without a metadata type:

{ 
    "__metadata": {}, 
    "Title": "Another Title" 
}

after that i tested the following code with the correct type from the ListItemEntityTypeFullName call:

import { sp } from "@pnp/sp";

public static async testUpdate(){
  return await sp.web.lists.getByTitle('test').items.getItemByStringId('1').update({
    '__metadata': {'type': 'SP.Data.TestListItem'},
    'Title': 'Another Title',
    }).then(result => {
      console.log(result);
   });
}

And it worked without problems.
I don't know if i did something wrong but Adding Items and Deleting Items is working on different lists and also some Batch operations with Add/Delete. I also used the onInit() context setup for sp.

I hope someone can help here and find the real culprit.

answered not a bug question

Most helpful comment

Hi @Sedor,

It's not a bug.

getItemByStringId serves the purpose of working with BDC/BCS (external lists) it is not supported with native lists, getById should be used instead. For external lists __metadata is not required.

All 2 comments

Hi @Sedor,

It's not a bug.

getItemByStringId serves the purpose of working with BDC/BCS (external lists) it is not supported with native lists, getById should be used instead. For external lists __metadata is not required.

Hey @koltyakov,

Thanks for the fast help !
I tested your suggestion and it's working. I also saw the getItemByStringId description .... Didn't see the BCS part of it there.

Thanks again :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SpliceVW picture SpliceVW  路  3Comments

AJIXuMuK picture AJIXuMuK  路  3Comments

ITAndy23 picture ITAndy23  路  3Comments

SpliceVW picture SpliceVW  路  3Comments

pavan2920 picture pavan2920  路  3Comments