[O365] Adding reusable content using CSOM
The reusable content feature of SharePoint can be useful when you have content you want to have repeated on multiple pages. When you update the content, it will change on the pages accordingly.
For a project I had the requirement to insert reusable content on a page programatically. There are quite some pages on this subject, but they’re all focussed on the server side object model. One example is this post by Jeremy Jameson who describes how to add HTML to your publishing field which mimics a reusable content block. The good news is that these techniques also apply to CSOM!
In code:
string crhelptext = @" "; string crhelptextValue = String.Format(crhelptext, clientContext.Web.ServerRelativeUrl); item.ParseAndSetFieldValue("FieldName", crhelptextValue); item.Update(); item.File.CheckIn(string.Empty, CheckinType.MajorCheckIn); item.File.Publish(string.Empty); clientContext.ExecuteQuery();
The HTML fragment is what represents the reusable content. SharePoint will detect this when processing the publishing HTML field (note: it’s important that you put this content in a publishing HTML field!) and replace it with the actual content. It will also ensure the content is updated when the user updates the reusable content item. The rest of the process is straightforward: set the field value and check-in your page. I didn’t bother to include creating the clientcontext and item objects by the way, to keep the code sample short.
Leave a Comment