Saturday, February 6, 2021

Postman : Add item to SharePoint from postman using SharePoint REST api

Postman : Add item to SharePoint from postman using SharePoint REST api (Can be used to add/update items in SharePoint from external application through REST api)

 Problem statement : Leverage Postman desktop or chrome extension to make SharePoint REST api calls to add/update items in SharePoint online site


There are many articles available in internet to make READ type calls from Postman . I am not going to detail out all the basic steps to configure the Postman , rather I will be focusing on the information required to make the ADD/UPDATE type of REST api calls.

Note: I am using Postman desktop application , but you can do the same through Postman Google chrome extension as well . Postman desktop client


Step-1 : Configure app permission in SharePoint Register add-in

Step-2 : Retrieve tenant ID

Step-3 : Retrieve access token 

Step-4 : Connect SharePoint REST api to perform READ operations 

For Step 1 through 4 , I suggest this article which consists of all the details 

Additional reference article 

https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/authorization-and-authentication-of-sharepoint-add-ins

https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-rest-endpoints


When you are confident that you are able to perform READ operations through Postman per above details , you are good to make next step to make Add/update operations , make below changes to ADD/UPDATE items in SharePoint list/libraries 


App registration 

While you register your app and provide permission level , make sure you are providing Write/Manage/FullControl types of access level , Refer to link App access level .

In my case I was trying to add a new item to SharePoint online list , I opted for below permission level , but you can provide minimum required permission as well 

<AppPermissionRequests AllowAppOnlyPolicy="true">

    <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/" Right="FullControl" />

</AppPermissionRequests>


Follow above process with new client ID and secret to record below items 

  1. Tenant ID (Bearer realm)
  2. ResourceID
  3. Client ID 
  4. Client Secret 
With all this info , as earlier make call to below REST api to retrieve access token

https://accounts.accesscontrol.windows.net/<TenantID>/tokens/OAuth/2

Copy "access_token" from the response


Next step is to try make a Write/Update type REST api call , In my case I wanted to try below REST API call to
add item to SPO list

POST https://{site_url}/_api/web/lists/GetByTitle('Test')/items Authorization: "Bearer " + accessToken Accept: "application/json;odata=verbose" Content-Type: "application/json;odata=verbose" Content-Length: {length of request body as integer} X-RequestDigest: "{form_digest_value}" { "__metadata": { "type": "SP.Data.TestListItem" }, "Title": "Test" }


As you can see we need 2 items highlighted in yellow to make this call
  1. Form digest value
  2. ListItemEntityTypeFullName - This is different for each list/Library
Retrieve "Form digest value"

We can use Postman to retrieve form digest value through below REST api call - Reference Link

https://SiteCollection/_api/contextinfo

In the header for this call you need to include

  • Accept - application/json;odata=verbose
  • Authorization - Bearer "Access token value"
Save the form digest value you receive in property " "FormDigestValue"

Retrieve "ListItemEntityTypeFullName "

Make another call as below to retrieve ListItemEntityTypeFullName 

https://SitecollectionURL/_api/web/lists/GetByTitle('ThiloshList')?$select=ListItemEntityTypeFullName

You should see a response as below , copy the value 

{
    "ListItemEntityTypeFullName""SP.Data.ThiloshListListItem"
}


Make SharePoint REST api call to add item to SharePoint Online list 


Make REST api  POST call to 

https://SiteCollectionURL/_api/web/lists/GetByTitle('ThiloshList')/items

With below parameter in header

  1. Accept - application/json;odata=nometadata
  2. Authorization - Berer [access token]
  3. X-RequestDigest - [Form digest value previously copied]
  4. Content-Type - application/json;odata=verbose

In the Body , copy below JSON 

{
  "__metadata": {
    "type""SP.Data.ThiloshListListItem"
  },
  "Title""Test"
}

This will
  1. Authenticate the application
  2. Add the "test" item into "ThiloshList" SharePoint Online list


You can use same process to make any other ADD/Update SharePoint REST api calls.






No comments:

Post a Comment