Update item name using Sitecore Item Web API
Recently i have been using Sitecore Item Web API frequently to add/update/retrieve content from sitecore, Its easy to setup and use, and can save you time and effort when you integrate sitecore with other plateforms/systems
I was asked a question recently on how to change Sitecore Item through the Item web API, and i thought that should simple and out of the box, right?
Well, i was wrong, i could not find that its possible to update item name while going through Sitecore documentation:
Last thing, You will need to add the following Patch config in your Include folder:
Thats It! Now you get to update your item name through Item Web API
Developers use the Sitecore Item Web API to manipulate the content items in Sitecore through HTTP requests. The API gives access to content through items paths, IDs, and Sitecore queries. This service produces output in JSON format and is both highly customizable and optimized to support a minimum number of requests.
I was asked a question recently on how to change Sitecore Item through the Item web API, and i thought that should simple and out of the box, right?
Well, i was wrong, i could not find that its possible to update item name while going through Sitecore documentation:
So, I cam up with a solution to allow "itemName" to be sent in the request body, Like itemName=NewName, First i created this class:
Updating Existing Items The HTTP PUT method is used to update existing items. This affects all items in the resolved scope. To update the item fields in the scope, you must specify the field data in the request body in the standard format. The field values can contain special characters and should therefore be encoded when they are passed to the Item Web API. You must specify the fields in the PUT request body in one of the following formats:
- [fieldname1]=[fieldvalue1]&[fieldname2]=[fieldvalue2]&[fieldnameN]=[fieldvalueN]
If an item contains a field with the specified name, the field is updated with the value that you specify.
- [fieldid1]=[fieldvalue1]&[fieldid2]=[fieldvalue2]&[fieldidN]=[fieldvalueN]
public class UpdateExtended : UpdateProcessor { public UpdateExtended() { } private static bool CanUpdateField(Field field, Mode mode) { if (!field.CanRead) { return false; } if (!field.CanWrite) { return false; } if (mode == Mode.AdvancedSecurity && !field.CanRemoteRead()) { return false; } return true; } private static Field GetField(Item item, string fieldId) { Assert.ArgumentNotNull(item, "item"); Assert.ArgumentNotNull(fieldId, "fieldId"); if (!ID.IsID(fieldId)) { return item.Fields.FirstOrDefaultAs you can see above, Inside Process method i have IF statement to check one of the parameters sent = "itemName", So now when i send this inside my request Body itemName = New Name, it will update my item to the new value!((Field x) => x.Name.Equals(fieldId, StringComparison.InvariantCultureIgnoreCase)); } return item.Fields.FirstOrDefault ((Field x) => x.ID == ID.Parse(fieldId)); } public override void Process(UpdateArgs arguments) { Assert.ArgumentNotNull(arguments, "arguments"); Item[] scope = arguments.Scope; NameValueCollection form = arguments.Context.HttpContext.Request.Form; Item[] itemArray = scope; for (int i = 0; i < (int)itemArray.Length; i++) { Item item = itemArray[i]; if (!(Sitecore.Context.Site.Name == "shell") || item.Access.CanWriteLanguage()) { item.Fields.ReadAll(); item.Editing.BeginEdit(); foreach (string str in form) { Field field = GetField(item, str); if (str == "itemName" && !string.IsNullOrEmpty(form[str])) { item.Name = form[str]; } if (field == null || !CanUpdateField(field, arguments.Context.Settings.Mode)) { continue; } else { field.Value = form[str]; } } item.Editing.EndEdit(); } } } }
Last thing, You will need to add the following Patch config in your Include folder:
Thats It! Now you get to update your item name through Item Web API
Comments
Post a Comment