Search code examples
sharepoint-onlineteamsitesharepoint-rest-api

Create list item via REST API on Sharepoint online Team site


I am trying create list item on Sharepoint on line using REST API. When I am creating item on the root site or on the root/subsite - everything is awesome. But if I am trying to create list item on the Team-site with URL like root/teams/msteams_cc6dfd/, I am getting error:

' <m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> <m:code>-2130575251, System.Runtime.InteropServices.COMException</m:code> <m:message xml:lang="en-US">The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.</m:message> </m:error>'

Interisting, that GET on this site works fine, but POST no.

P.S. Sorry for my crazy English.


Solution

  • POST requests need X-RequestDigest header exists in your request. You can get it from current page or from /_api/web/contextinfo endpoint.
    Use this link for examples: https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-rest-endpoints#writing-data-by-using-the-rest-interface
    To get from page: "X-RequestDigest": $("#__REQUESTDIGEST").val()
    To get it from contextinfo create GET request to this endpoint and extract FormDigestValue param value.
    I think it will solve your problem.

    UPDATED:

    It can be permissions issue. Please check account permissions on this sites. If you done all correctly with X-RequestDigest then it can be only permissions issue. Is this account has required permissions to call API ? Is this account has permissions to create list item in this sites on required for you lists ?
    Try to add account to site collection administrators. Is it working? Is your lists has unique permissions ? May be account has Contribute role on site but not have access to specific list because there unique permissions exists.
    Can you create list items not from 1C but from other ways ? PowerShell script, Console App C#, javascript from some on your SharePoint sites? If it working correctly then issue in 1C code.