public static IPublishedContent ConvertInnerContentToPublishedContent( JObject item, IPublishedContent parentNode = null, int sortOrder = 0, int level = 0, bool preview = false) { var publishedContentType = GetPublishedContentTypeFromItem(item); if (publishedContentType == null) { return(null); } var propValues = item.ToObject <Dictionary <string, object> >(); var properties = new List <IPublishedProperty>(); foreach (var jProp in propValues) { var propType = publishedContentType.GetPropertyType(jProp.Key); if (propType != null) { properties.Add(new DetachedPublishedProperty(propType, jProp.Value, preview)); } } // Manually parse out the special properties propValues.TryGetValue("name", out object nameObj); propValues.TryGetValue("key", out object keyObj); // Get the current request node we are embedded in var pcr = UmbracoContext.Current.PublishedContentRequest; var containerNode = pcr != null && pcr.HasPublishedContent ? pcr.PublishedContent : null; var node = new DetachedPublishedContent( keyObj == null ? Guid.Empty : Guid.Parse(keyObj.ToString()), nameObj?.ToString(), publishedContentType, properties.ToArray(), containerNode, parentNode, sortOrder, level, preview); // Process children if (propValues.ContainsKey("children")) { var children = ConvertInnerContentToPublishedContent((JArray)propValues["children"], node, level + 1, preview); node.SetChildren(children); } if (PublishedContentModelFactoryResolver.HasCurrent && PublishedContentModelFactoryResolver.Current.HasValue) { // Let the current model factory create a typed model to wrap our model return(PublishedContentModelFactoryResolver.Current.Factory.CreateModel(node)); } return(node); }
public static IPublishedContent ConvertInnerContentToPublishedContent(JObject item, IPublishedContent parentNode = null, int sortOrder = 0, int level = 0, bool preview = false) { var contentTypeAlias = GetContentTypeAliasFromItem(item); if (string.IsNullOrEmpty(contentTypeAlias)) { return(null); } var publishedContentType = PublishedContentType.Get(PublishedItemType.Content, contentTypeAlias); if (publishedContentType == null) { return(null); } var propValues = item.ToObject <Dictionary <string, object> >(); var properties = new List <IPublishedProperty>(); foreach (var jProp in propValues) { var propType = publishedContentType.GetPropertyType(jProp.Key); if (propType != null) { properties.Add(new DetachedPublishedProperty(propType, jProp.Value, preview)); } } // Parse out the name manually object nameObj; if (propValues.TryGetValue("name", out nameObj)) { // Do nothing, we just want to parse out the name if we can } // Parse out key manually object keyObj; if (propValues.TryGetValue("key", out keyObj)) { // Do nothing, we just want to parse out the key if we can } // Get the current request node we are embedded in var pcr = UmbracoContext.Current.PublishedContentRequest; var containerNode = pcr != null && pcr.HasPublishedContent ? pcr.PublishedContent : null; var node = new DetachedPublishedContent( keyObj == null ? Guid.Empty : Guid.Parse(keyObj.ToString()), nameObj?.ToString(), publishedContentType, properties.ToArray(), containerNode, parentNode, sortOrder, level, preview); // Process children if (propValues.ContainsKey("children")) { var children = ConvertInnerContentToPublishedContent((JArray)propValues["children"], node, level + 1, preview); node.SetChildren(children); } return(node); }