Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • andrew shearer 345 posts 440 karma points
    1 week ago
    andrew shearer
    0

    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

  • Søren Kottal 315 posts 1805 karma points c-trib
    1 week ago
    Søren Kottal
    0

    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

    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));
    

    Hope it helps :)

  • andrew shearer 345 posts 440 karma points
    1 week ago
    andrew shearer
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft