programmatically updating content in NestedContent by type
Hello
Does anybody have a convenient way of using the content service API to update content that resides in a NestedContent property?
For example, say that a “Widget” doctype has been used to content load 100 pages in the website via NestedContent, how would you go about programmatically identifying the pages that use this Widget doctype in the NestedContent property so that you could then update a value within the Widget?
The ContentService GetContentOfContentType doesn’t do this.
You need to generate the JSON blob required by Nested Content yourself. It should be an array of objects, and each object must have the properties key, ncContentTypeAlias, and the properties of your desired document type.
I have previously done like this
var contentItem = ContentService.GetById(1);
var nestedContentValue = new JArray();
// you can add a loop here, if you need multiple nested items
var nestedContentItem = new JObject();
// you need to add a Guid as a key to your item
nestedContentItem .Add("key", Guid.NewGuid().ToString());
// and the desired doctype
nestedContentItem.Add("ncContentTypeAlias", "myNestedType");
// add the values for each property of your doctype
nestedContentItem.Add("myPropertyAlias", "some property value");
// finally, add you nestedContentItem, to your nestedContentValue
nestedContentValue.Add(nestedContentItem);
// then set the value of your IContent contentItem
contentItem.SetValue("nestedContentProperty", JsonConvert.SerializeObject(nestedContentValue));
i was just as interested to know how i should identify pages that contain the said doctype in their NestedContent property (your first line of code). Does anyone know convenient/efficient ways of doing that? thank you
programmatically updating content in NestedContent by type
Hello
Does anybody have a convenient way of using the content service API to update content that resides in a NestedContent property?
For example, say that a “Widget” doctype has been used to content load 100 pages in the website via NestedContent, how would you go about programmatically identifying the pages that use this Widget doctype in the NestedContent property so that you could then update a value within the Widget?
The ContentService GetContentOfContentType doesn’t do this.
thanks
Hi Andrew
You need to generate the JSON blob required by Nested Content yourself. It should be an array of objects, and each object must have the properties key, ncContentTypeAlias, and the properties of your desired document type.
I have previously done like this
Hope it helps :)
Hi Søren,
Do you know if there is a way to update a just a single Nested Content record?
I can save and replace all the NC items with the one I am saving but I am looking to just replace it based on its key?
OR how you delete a single record from a list of nested content items?
Thanks, Jack
thanks Søren :D
i was just as interested to know how i should identify pages that contain the said doctype in their NestedContent property (your first line of code). Does anyone know convenient/efficient ways of doing that? thank you
Hope this helps. You can fetch the key variable from each nested content element and update that speific element.
Thanks - I have managed to get is all working now.
How did you get this to work?
is working on a reply...