我有一个名为event的DocumentType,它有一个名为eventCategories的属性,它是ContetPicker的NestedContent。我正在尝试在UmbracoApiController中创建一个事件 - 外部服务将填充这些事件。
我已经使用了这个要点 - https://gist.github.com/robertjf/c6d9122d90b4406b19ec - 这个问题 - Create NestedContent Items In SurfaceController - 作为我的起点。
这是我创建事件的代码(删除了不相关的属性)
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);
这会失败,因为在GetContentType函数中
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不包含'docTypeGuid',它包含这些属性
{[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>
我想也许我将错误的属性传递给NestedContentCreator.GenerateNestedContentValue但不知道我应该传递什么。
答案 0 :(得分:0)
我已经通过不使用Gist - https://gist.github.com/robertjf/c6d9122d90b4406b19ec来解决这个问题。
相反,我使用了来自此处的信息 - https://github.com/umco/umbraco-nested-content/issues/57
生成此代码
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是
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;
}