I have a DocumentType called event, this has a property called eventCategories which is a NestedContent of ContetPicker. I am trying to create a event in an UmbracoApiController - an external service will be populating these.
I have used this Gist - https://gist.github.com/robertjf/c6d9122d90b4406b19ec - and this question - Create NestedContent Items In SurfaceController - as my starting point.
this is my code to create the event (with irrelevant properties removed)
var newEvent = Services.ContentService.CreateContent(pageName, EventRootGuid, EventContentTypeAlias);
var categories = new List<Dictionary<string, object>>();
categories.Add(getCategory("test 1"));
categories.Add(getCategory("test 2"));
var pt = newEvent.PropertyTypes.FirstOrDefault(p => p.Alias == "eventCategories");
newEvent.SetValue("eventCategories", NestedContentCreator.GenerateNestedContentValue(pt, categories));
Services.ContentService.SaveAndPublishWithStatus(newEvent);
This fails because in the GetContentType function
private static IContentType GetContentType(int propertyDefinitionId)
{
var preValueCollection = (PreValueCollection)ApplicationContext.Current.ApplicationCache.RuntimeCache.GetCacheItem(
string.Concat("Our.Umbraco.NestedContent.GetPreValuesCollectionByDataTypeId_", propertyDefinitionId),
() => ApplicationContext.Current.Services.DataTypeService.GetPreValuesCollectionByDataTypeId(propertyDefinitionId));
var preValuesDict = preValueCollection.PreValuesAsDictionary.ToDictionary(x => x.Key, x => x.Value.Value);
Guid contentTypeGuid;
if (!preValuesDict.ContainsKey("docTypeGuid") || !Guid.TryParse(preValuesDict["docTypeGuid"], out contentTypeGuid))
return null;
var contentTypeAlias = GetContentTypeAliasByGuid(Guid.Parse(preValuesDict["docTypeGuid"]));
return ApplicationContext.Current.Services.ContentTypeService.GetContentType(contentTypeAlias);
}
preValuesDict does not contain 'docTypeGuid' it contains these properties
{[contentTypes, [ {"ncAlias": "eventCategoryList", "ncTabAlias": "Event Category List", "nameTemplate": "" }]]} System.Collections.Generic.KeyValuePair<string, string>
{[minItems, 1]} System.Collections.Generic.KeyValuePair<string, string>
{[maxItems, 10]} System.Collections.Generic.KeyValuePair<string, string>
{[confirmDeletes, 1]} System.Collections.Generic.KeyValuePair<string, string>
{[showIcons, 1]} System.Collections.Generic.KeyValuePair<string, string>
{[hideLabel, ]} System.Collections.Generic.KeyValuePair<string, string>
I think maybe I am passing the wrong property to NestedContentCreator.GenerateNestedContentValue but don't know what I should pass in instead.