This is the second article in the series of using Fiddler as a debugging and testing tool for SharePoint REST API EndPoints.
You can read the article on GET Requests here,
POST requests are different in nature than GET requests. They require more authentication layers to get through in order to push the data to SharePoint Lists and Libraries.
In order to run the POST request successfully, we need an additional request header “X-RequestDigest” which is nothing but the User Authentication Token.
In order to request this token from SharePoint, we need to make use of the “contextInfo” endpoint that will return the “FormDigestValue” containing the required user authentication token.
Now, let us see how we can request Authentication Token from SharePoint.
Get Authorization Token
http://<Host Name>_api/contextinfo
Once we get the Authentication Token from SharePoint, we can add this token information in the Request Header of each of the POST requests.
Request Headers
Accept: application/json;odata=verbose Content-Type:
application/json;odata=verbose
X-RequestDigest:0xE1AE266A42214DA2940689826F68426D10620220CEDD3093CA2C234993E4ECA265BA57D357E8D3BD32F56660613CADBF72495F2C858B38F7C9B9C3CAD797F6D5,06 Feb 2017 01:22:08 -0000
Once we are ready with Request Headers, we can start issuing POST Requests, as shown below.
Add Data to List
Let’s consider we have a list called Categories, as shown below.
First, see the XML return based on querying schema for Categories List using the following URL.
http://<Host Name>/_api/Web/Lists/getByTitle('Categories')
Then, we will see the XML return based on querying for Categories List Items, using the following URL.
http://<Host Name>/_api/Web/Lists/getByTitle('Categories')/Items
The next step is to prepare the Request Body and we have to include the following properties to add the items.
Please note that I am taking properties that are required for this list to add the category and add any desired number of properties to the Request Body as per the schema of the target list.
Request Body
"__metadata": { type: " SP.Data.CategoriesListItem" }, Title: "Category From Fiddler", CategoryID: 9, Description: “New Category Added from Fiddler”
Once we execute this request, we can inspect the response to ensure that the requested item has been added successfully to the Categories List.
Also, we can validate this new item added by browsing the Categories List.
Update List Item
http://<Host Name>/_api/Web/Lists/getByTitle('Categories')/Items(9)
For update request, you have to include “eTag” value that was returned with the item during initial query to the Request Body. SharePoint uses this value to determine if there are any updates made to the item since it was last queried.
“If-Match: *” can be used to match any “eTag” value resulting in the operation being performed regardless of the actual value.
“X-Http-Method: PATCH” is to override the existing values
So, the request body would be like this,
IF-MATCH: * X-Http-Method: PATCH { "__metadata": { type: "SP.Data.CategoriesListItem" }, Title: "Category From Fiddler - Updated", Description: "New Category Added from Fiddler - Updated" };
Once the request executes successfully, we can see that the item is updated in the Categories List.
Delete List Item
http://<Host Name>/_api/Web/Lists/getByTitle('Categories')/Items(9)
The Delete operation is more or less similar to Update operations.
In case of delete, we will use “X-Http-Method: DELETE” in the Request Body.
Request Body
IF-MATCH: * X-Http-Method: DELETE
Once the request executes successfully, we can validate if the item is deleted from the list.
Add New List
http://<Host Name>/_api/Web/Lists
Adding a new SharePoint List involves a little bit more of configuration information in Request body apart from request headers.
Request Headers
Accept: application/json;odata=verbose Content-Type: application/json;odata=verbose
Request Body
Content - Length: 0 {
"__metadata": {
type: "SP.List"
},
"AllowContentTypes": true,
"ContentTypesEnabled": true,
"Description": "This is Task List Created using Fiddler",
"BaseTemplate": 107,
"Title": "Task List By Fiddler"
}
Once this request has been executed successfully, we can see the Response Body holding information about the newly added SharePoint List.
Also, we can see this new list added to SharePoint by browsing the respective site.
Also, we can verify if the “AllowContentTypes” & “ContentTypesEnabled” properties are configured as expected or not, by browsing the Advanced Properties of the new List as shown below.
Delete List
http://<Host Name>/_api/Web/Lists/getByTitle('Task%20List%20By%20Fiddler')
Deleting a list is rather simpler than adding it. It takes “X-Http-Method: DELETE” to be added to the request header and the rest will be done for you.
Request Headers
Accept: application/json;odata=verbose Content-Type: application/json;odata=verbose Content-Length: 0 IF-MATCH: * X-Http-Method: DELETE
Once the request has been completed, it will delete the required list from SharePoint Lists Collection.
Hope you find it helpful.