Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom 713 posts 954 karma points
    Aug 22, 2014 @ 06:34
    Tom
    0

    Load properties with umbraco.cms.web.businesslogic.Document

    Hi I'm using Node2JSON.cshtml and wanted to get media paths as the values in the json instead of the media id as the json is being consumed by angular.

    It uses INode so getting the media path obviously isn't an easy option

    At the moment the best way I can see to do it is to query for all documents of the same type as the INode collection and then grab properties and check if they're a media type and get the path.

      var document = hasNodeId && nodeId !=0 ? new umbraco.cms.businesslogic.web.Document(nodeId) : null;
        var documents = new List<umbraco.cms.businesslogic.web.Document>();      
        if(document != null)
        {
            documents = select.Equals(QS_SELECT_VAL_SELF)
                ? new List<umbraco.cms.businesslogic.web.Document> { document } : document.Children.Where(c => string.IsNullOrEmpty(nodeType) || c.ContentType.Alias == nodeType).ToList()
    
        }

    And then in the GetPropertyDictionary function:

     @functions {
            const int Media_Picker = 1035;
            Dictionary<string, object> GetPropertyDictionary(INode node, bool recursive, List<umbraco.cms.businesslogic.web.Document> documents)
            {
                var document = documents.Where(d => d.Id == node.Id).First();            
    
                var jsonItemPropertyDictionary = new Dictionary<string, object>
            {
                {"nodeName", node.Name},
                {"nodeId", node.Id.ToString()},
                {"nodeType", node.NodeTypeAlias}
            };
    
                var children = node.PropertiesAsList;
                foreach (var prop in children)
                {
                    var propertyTyped = document.GenericProperties.Where(p => p.PropertyType.Alias == prop.Alias).Single();
                    var value = propertyTyped.PropertyType.DataTypeDefinition.Id == Media_Picker ? Library.MediaById(prop.Value).umbracoFile : prop.Value;
    
                    jsonItemPropertyDictionary.Add(prop.Alias, value);
                }
                var childrenProperties = new List<Dictionary<string, object>>();
                if (recursive)
                {
                    foreach (var child in node.ChildrenAsList)
                    {
                        childrenProperties.Add(GetPropertyDictionary(child, recursive, documents));
                    }
                    jsonItemPropertyDictionary.Add("children", childrenProperties);
                }
                return jsonItemPropertyDictionary;
            }
        }

     

    I was wondering if someone could suggest a more efficient approach if there is one?

    Thanks

  • Charles Afford 1163 posts 1709 karma points
    Aug 24, 2014 @ 17:31
    Charles Afford
    0

    Only look at the code quickly, but i think you should get the results through Lucene rather than using a LINQ query for all nodes. This will be slow depending on how many nodes you have.

    If you did use Lucene you could get all the information in to a KeyValuePair of properties and values passed into your function.

Please Sign in or register to post replies

Write your reply to:

Draft