I'm trying to programmatically set a nested content property but am having a bit of trouble getting it working fully. The following code creates the nested content item on the document however none of the properties (title & bodyText) are set.
Looking at some older threads the answers say the value should be a "JSON serialized Dictionary<string, object> string" but I don't see what the contents of the dictionary should be.
Am I barking up the wrong tree trying to serialise a list of IContents? If not how do I get the actual nested content item's properties set.
You can get this working, I hope the following code helps you out. You were close before, it is not a list of IContent you need though but a list of dictionary.
Content doc = (Content)sender;
IContentService contentService = ApplicationContext.Current.Services.ContentService;
// Same as in your above example, title and body text values
var items = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(doc.GetValue<string>("nestTextContent"));
items.Add(new Dictionary<string, object>() {
{
"title",
"test"
},
{
"bodyText",
cleanedDescription
}
});
doc.SetValue("pageContent", JsonConvert.SerializeObject(items));
contentService.Save(doc);
This is just a basic working example for 7.3+ that I have tested.
Hello I have a similar problem can you help me?
I can not initialize items it tells me that the value is null and item.add logically I must put an object but I do not know which object
thank you
Note: the serialized format of Nested Content property values has quite probably changed in v8. Here is what I have in database for a Nested Content property with only 1 TextBox:
I'm just trying to do something similiar to this. Ideally when I create a new page, which has a nested content field on it, I want to add an element to that nested content.
All these code snips are usefully but where do you implement these? Do you need to use DI to hook in to the save function or something else?
Sorry if I'm missing something that's been explained above. If anyone has more info that would be great. Thanks!
I have the nested content variant working. Does anyone have a example of how to do this with a blocklist editor
I see this in the database
{"layout":{"Umbraco.BlockList":[{"contentUdi":"umb://element/0aa543991746424fb5d709793a14b8e3"},{"contentUdi":"umb://element/34c4794b69404535bce54bb6e1570b79"}]},"contentData":[{"contentTypeKey":"7af0ad93-b236-4b5c-9664-3c408dee7f85","udi":"umb://element/0aa543991746424fb5d709793a14b8e3","description":"test","value":"test2"},{"contentTypeKey":"7af0ad93-b236-4b5c-9664-3c408dee7f85","udi":"umb://element/34c4794b69404535bce54bb6e1570b79","description":"test3","value":"test4"}],"settingsData":[]}
Hi all I have a similar problem can you please help me? I worked above code but it won't work currently i'm using umbraco 8.8. Can you please give me working sample code.
Creating Nested Content Programatically
I'm trying to programmatically set a nested content property but am having a bit of trouble getting it working fully. The following code creates the nested content item on the document however none of the properties (title & bodyText) are set.
Looking at some older threads the answers say the value should be a "JSON serialized
Dictionary<string, object>
string" but I don't see what the contents of the dictionary should be.Am I barking up the wrong tree trying to serialise a list of IContents? If not how do I get the actual nested content item's properties set.
These are the threads I've been referencing. https://our.umbraco.org/projects/backoffice-extensions/nested-content/nested-content-feedback/72977-can-i-write-collection-of-ipublishedcontent-back-to-model-property https://our.umbraco.org/projects/backoffice-extensions/nested-content/nested-content-feedback/76444-create-nestedcontent-items-in-surfacecontroller
Hi Suzy
You can get this working, I hope the following code helps you out. You were close before, it is not a list of IContent you need though but a list of dictionary.
This is just a basic working example for 7.3+ that I have tested.
hello
I am trying to Set value and save a Nested Content programatically. Pretty much following the above steps.
I get an error while Save - Object Reference not Set to an instance of an object.
ApplicationContext.Current.Services.ContentService.Save(myContent);
I am not sure why I get this.
Thanks so much David. I've got it working now.
Obvious when it's pointed out but I just couldn't figure it out last night.
Hello I have a similar problem can you help me? I can not initialize items it tells me that the value is null and item.add logically I must put an object but I do not know which object thank you
This line is probably return a null value as you don't have anything in the property yet:
You need to check if items is null and, if so, initialize it accordingly:
Note: the serialized format of Nested Content property values has quite probably changed in v8. Here is what I have in database for a Nested Content property with only 1 TextBox:
So this will probably need to be amended for v8
My solution in Umbraco 8.1.4:
you saved my life maaaan :)
Hi,
I'm trying this is Umbraco version 8.5.1 but having zero luck.
It's just not adding the content to the nested property. Do you know if anything has changed with nested content since you posted this?
I am using 8.5.3 and something similar to this works for me.
I have found a "key" is required to have a new guid.
And "ncContentTypeAlias" is required (This is the Alias of the Document type you are using for the nested content!)
Name is not required.
I'm just trying to do something similiar to this. Ideally when I create a new page, which has a nested content field on it, I want to add an element to that nested content.
All these code snips are usefully but where do you implement these? Do you need to use DI to hook in to the save function or something else?
Sorry if I'm missing something that's been explained above. If anyone has more info that would be great. Thanks!
I'm using Umbraco 8.
If your wanting to do it during Save i believe in v8 Composer and components are the way to do it.
This https://our.umbraco.com/documentation/implementation/composing/
And this article in particular helped me https://www.zpqrtbnk.net/posts/composing-umbraco-v8-components/
It works great :)
I have almost the same code,
But the issue is every time it overwrites the Old Value with the new one. As I need to append/add new Items to nested content!
so How can it be done?
Thanks
I have the nested content variant working. Does anyone have a example of how to do this with a blocklist editor
I see this in the database {"layout":{"Umbraco.BlockList":[{"contentUdi":"umb://element/0aa543991746424fb5d709793a14b8e3"},{"contentUdi":"umb://element/34c4794b69404535bce54bb6e1570b79"}]},"contentData":[{"contentTypeKey":"7af0ad93-b236-4b5c-9664-3c408dee7f85","udi":"umb://element/0aa543991746424fb5d709793a14b8e3","description":"test","value":"test2"},{"contentTypeKey":"7af0ad93-b236-4b5c-9664-3c408dee7f85","udi":"umb://element/34c4794b69404535bce54bb6e1570b79","description":"test3","value":"test4"}],"settingsData":[]}
Did you find a solution to this Stefan?
Ben/Stefan
There some WIP documentation here, regarding creating block list content programatically that might be helpful:
https://github.com/umbraco/UmbracoDocs/blob/e64ec0e5b28b4e5a37b7865691621e45dd82701f/Getting-Started/Backoffice/Property-Editors/Built-in-Property-Editors/Block-List-Editor/index.md#creating-blocklist-programmatically
regards
Marc
Hi all I have a similar problem can you please help me? I worked above code but it won't work currently i'm using umbraco 8.8. Can you please give me working sample code.
Thanks in advance
Hi Kiran,
Here a working example in umbraco 8.7. Nested content has 3 fields description, value1 and value2
Nested content is available is a doctype named slab
public class ContentDataThreeColumnItem : IContentDataItem { #region Properties
public class BlockListItem { #region Properties
private readonly IContentService _contentService;
public ExcelService(IContentService contentService, IContentTypeService contentTypeService, ISqlContext sqlContext, IMediaService mediaService, IMediaTypeService mediaTypeService) { _contentService = contentService; _contentTypeService = contentTypeService; _sqlContext = sqlContext; _mediaService = mediaService; _mediaTypeService = mediaTypeService; }
var slabNode = _contentService.GetPagedChildren(slabCollectionNode.Id, 0, 100, out totalRecords, filter).FirstOrDefault();
var slabNodeBlockListItemList = new List
slabNodeBlockListItemList.Add(new BlockListItem { Description = cpsExcelModel.Value1, Value1 = cpsExcelModel.Value2, Value2 = cpsExcelModel.Value3 });
var blockListItemContentType = ThreeColumnRow.GetModelContentType().Alias
foreach (var blockListItem in slabNodeBlockListItemList) { var contentUdi = new Content(null, slabNode, blockListItemContentType).GetUdi(); blockList.Layout.UmbracoBlockList.Add(new ContentUdiItem { ContentUdi = contentUdi });
var serializedBlockList = JsonConvert.SerializeObject(blockList); slabNode.SetValue(Slab.GetModelPropertyType(a => a.ReferenceTable).Alias, serializedBlockList, currentVariantLanguage);
_contentService.SaveAndPublish(slabNode, currentVariantLanguage, 0, false);
Hope this gives you a direction
Useful. Informative. Thanks to all of you
is working on a reply...