Sitecore Item Web API – CRUD Operations
January 19, 2023 Hemachandhiran Harimoorthy
Sitecore Item Web API provides REST APIs to perform all CRUD operations for a Sitecore instance. This document provides a detailed explanation of the APIs.
https://doc.sitecore.com/xp/en/sdnarchive/upload/sdn5/modules/sitecore%20item%20web%20api/sitecore_item_web_api_developer_guide_sc66-71-usletter.pdf
In this blog I will try to provide C# code examples for the operation which is not available in the document.
Apply the Security Config
The Item Web API is shipped as part of Sitecore CMS from version 7.1 onwards, so you don’t need to install any new packages to use. However we need to configure what security model we need to use.
Usually developers manipulate the content in CM servers, so use the below patch config file in CM server.
Create custom-webapi.config and place in your App_config folder. Below is the sample config.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
<sitecore>
<sites>
<site name="website">
<patch:attribute name="itemwebapi.mode">StandardSecurity</patch:attribute>
<patch:attribute name="itemwebapi.access">ReadWrite</patch:attribute>
<patch:attribute name="itemwebapi.allowanonymousaccess">false</patch:attribute>
</site>
</sites>
</sitecore>
</configuration>
Config file explanation:
mode
• Off — the Item Web API is turned off.
• StandardSecurity — the Item Web API is turned on and the default Sitecore security model is used.
• AdvancedSecurity — the Item Web API is turned on, and the default Sitecore security model is extended with the requirement to set the remote:fieldread access right for content fields.
access
• ReadOnly — to only allow the READ operation.
• ReadWrite — to allow the CREATE, READ, UPDATE, and DELETE (CRUD) operations.
allowanonymousaccess
• The attribute can be set to true or false. It specifies if is for nonauthenticated users have access.
With the above config file in place you can use your own approach to execute the REST APIs.
For ease of use I’m using C# and RestSharp HTTP Client.
For all the Rest APIs you need to include two headers
X-Scitemwebapi-Username : "sitecore\admin"
X-Scitemwebapi-Password : "b"
Retrieving Existing Items
There are many ways to retrieve Sitecore items.
Specifying the item ID: http:///-/item/v1/?sc_itemid={A60ACD61-A6DB-4182-8329- C957982CEC74}
Specifying the query: http:///-/item/v1/?query=/sitecore/content/*
Specifying the item path: http:///-/item/v1/sitecore/content/home
I am using the third option and passing the Sitecore Item full path in place of itemPath
RestClient client = new RestClient("https://xmurl/-/item/v1");
RestRequest request = new RestRequest(itemPath, Method.GET);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("X-Scitemwebapi-Username", "sitecore\\admin");
request.AddHeader("X-Scitemwebapi-Password", "b");
try
{
IRestResponse response = client.Execute(request);
if (response.StatusCode == HttpStatusCode.OK)
{
SitecoreItemsModel responseDetail = JsonConvert.DeserializeObject<SitecoreItemsModel>(response.Content);
}
}
catch (Exception ex)
{
}
Creating Media Items
Please carefully note the content-type header in this request. Also the path should always be Media Library root or one of its descendants.
string cleanFilename = "sample";
string filePath = @"C:\MyFiles\sample.pdf";
RestClient client = new RestClient("https://xmurl/-/item/v1");
client.Timeout = -1;
var request = new RestRequest("Media Item Path", Method.POST);
request.AddHeader("X-Scitemwebapi-Username", "sitecore\\admin");
request.AddHeader("X-Scitemwebapi-Password", "b");
request.AddHeader("Content-Type", "multipart/form-data");
request.AddQueryParameter("name", cleanFilename);
request.AddFile(cleanFilename, filePath);
IRestResponse response = client.Execute(request);
Creating Items
To create Sitecore item we need to specify two query string parameters name and template.
name — specifies the name of the item being created.
For template parameter we have three options
• The template ID : Specify the Template Id
• Template path, : specify the relative template path
• The branch ID: Branch Template id
You can add other field values by specifying body parameter and its values. You can update values of all fields.
request.AddParameter("Title", "Sitecore WEP Item");
request.AddParameter("Description", "My Description");
Example code to create items
string branchTemplateId = "{ED30D497-1857-4C84-A5AA-37B17C4F3476}";
RestClient client = new RestClient("https://xmurl/-/item/v1");
RestRequest request = new RestRequest(itemPath, Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("X-Scitemwebapi-Username", "sitecore\\admin");
request.AddHeader("X-Scitemwebapi-Password", "b");
//Your item name and template id
request.AddQueryParameter("name", "My Sample Item");
request.AddQueryParameter("template", branchTemplateId);
//Your fields and its values
request.AddParameter("Title", "Sitecore WEP Item");
request.AddParameter("Description", "My Description");
try
{
IRestResponse response = client.Execute(request);
if (response.StatusCode == HttpStatusCode.OK)
{
}
}
catch (Exception ex)
{
}
Updating Existing Items
To Update Existing items we don’t need to specify template and name. We just need to update item fields and should use PUT command
var client = new RestClient("https://xmurl/-/item/v1");
client.Timeout = -1;
var request = new RestRequest("sitecore item path", Method.PUT);
request.AddHeader("X-Scitemwebapi-Username", "sitecore\\admin");
request.AddHeader("X-Scitemwebapi-Password", "b");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
//Your fields and its values
request.AddParameter("Title", "Sitecore WEP Item Update");
request.AddParameter("Description", "My Description Update");
try
{
IRestResponse response = client.Execute(request);
}
catch (Exception ex)
{
}
For more hints and tips please continue tro review our blogs www.anyaconsultancy.com/blog or for specific questions please contact info@anyaconsultancy.com