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?
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.
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.
And then in the GetPropertyDictionary function:
I was wondering if someone could suggest a more efficient approach if there is one?
Thanks
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.
is working on a reply...