I need to import a great number of nodes using CMSImport and map media to each imported node. As there might be more than one media for each node, I cannot use the builtin media importer, but instead I'll create a FieldAdapter which takes care of the mapping.
All media nodes are put in one folder which I know the id of. So basically I need to search for filenames in all child nodes of this already defined parent node. The filenames are saved as a text string array in the import.
My question is how I should do this without killing the performance? With my limited .NET skills I would be tempted to do loop through all media child elements with something like:
var media = new Media(1266); foreach (Media childMedia in media.Children) { if (childMedia.getProperty("umbracoFile").Value == fileName)) return childMedia.Id; } return 0;
This should work, but not for many nodes. So how can it be achieved in a better way? I assume LINQ or something would be better?! Or xpath?
This would be a performance penalty on the first hit. Then the item is cached so for the second call it shouldn't be a problem. What you also could do is use a sql query to return the node id like the one below. That will only use one query per fileName. Only thing is that you need to make sure your filenames are unique
SELECT distinct ContentNodeId FROM cmsPropertyData where dataNVarchar like '%mediaFilename.ext'
Map media - performance considerations
I need to import a great number of nodes using CMSImport and map media to each imported node. As there might be more than one media for each node, I cannot use the builtin media importer, but instead I'll create a FieldAdapter which takes care of the mapping.
All media nodes are put in one folder which I know the id of. So basically I need to search for filenames in all child nodes of this already defined parent node. The filenames are saved as a text string array in the import.
My question is how I should do this without killing the performance? With my limited .NET skills I would be tempted to do loop through all media child elements with something like:
This should work, but not for many nodes. So how can it be achieved in a better way? I assume LINQ or something would be better?! Or xpath?
Hi Nik,
This would be a performance penalty on the first hit. Then the item is cached so for the second call it shouldn't be a problem. What you also could do is use a sql query to return the node id like the one below. That will only use one query per fileName. Only thing is that you need to make sure your filenames are unique
SELECT distinct ContentNodeId FROM cmsPropertyData where dataNVarchar like '%mediaFilename.ext'
Hope this helps you,
Richard
is working on a reply...