0

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.

Stuart
  • 1,123
  • 8
  • 24

1 Answers1

0

I have fixed this by not using the Gist - https://gist.github.com/robertjf/c6d9122d90b4406b19ec.

Instead I used info from here -https://github.com/umco/umbraco-nested-content/issues/57

to produce this code

        foreach (Dictionary<string, object> category in categories)
        {
            dynamic ncItem = new ExpandoObject();

            var locaUdi = Udi.Create(Constants.UdiEntityType.Document, ((Umbraco.Core.Models.Content)category["eventCategory"]).Key);

            ((IDictionary<string, object>)ncItem).Add("name", "Some item name");
            ((IDictionary<string, object>)ncItem).Add("ncContentTypeAlias", "eventCategoryList");
            ((IDictionary<string, object>)ncItem).Add("eventCategory", locaUdi);

            ncItems.Add(ncItem);
        }

getCategory is

    private Dictionary<string, object> getCategory(string v)
    {
        var eventCategoryRoot = Services.ContentService.GetById(EventCategoryRootGuid);
        var categoryGuid = Services.ContentService.GetChildren(eventCategoryRoot.Id).Where(c => c.Name == v).FirstOrDefault();

        if (categoryGuid == null)
        {
            return null;
        }

        var category = new Dictionary<string, object>();
        category.Add("eventCategory", categoryGuid);

        return category;
    }
Stuart
  • 1,123
  • 8
  • 24