Quantcast
Channel: Optimizely Search & Navigation
Viewing all articles
Browse latest Browse all 6894

EPiServer - GetChildren from Nested File Structure by Name

$
0
0

I am new to EPiServer so please let me know if there is a such library
dedicated for this specific use.

This is my problem:

Based on a given referencePath and Image name , I need to **getChildren()** of each given
folder so I can find the next folder by its name and GetChildren() and so an until I hit the last folder to get Image by its name to return its **contentLink**.

Referencepath : MomDirectory > Children1a > SubChildren > Images > Bad
Images > Img1.jpg

In EpiServer , I have contentFolderstructure like this

Family Directory

- MomDirectory 1

- Children 1a
- SubChildren
- img.jpg
- img1.jpg
- imgN.jpg
....
- Children 1b
- img.jpg
- img1.jpg
- imgN.jpg
....
- Children 1c
- SubChildren
- Images
- Bad Images
- img.jpg
- img1.jpg
- imgN.jpg
- Good Images
- img.jpg
- img1.jpg
- imgN.jpg
....
- - FatherDirectory 2
- Children 2a
- SubChildren
- img.jpg
- img1.jpg
- imgN.jpg
....
- Children 1b
- SubChildren
- img.jpg
- img1.jpg
- imgN.jpg
....
- Children 1c
- SubChildren
- img.jpg
- img1.jpg
- imgN.jpg
....


As I mentioned the very last folder contains the images where I get the final result I need which the contentReference of the image.
The below code is a manual way based on the sample referencePath. As you can see I get the first folder and gets its children to find the next folder and again get its children to find the next and so on.

// opening the family directory
var list = this._contentLoader.GetChildren<IContent>
(familyDirectory).ToList();
// finding the momDirectory by Name
var imageFolder = list.FirstOrDefault(x => x.Name.ToLower() ==
MomDirectoryName.ToLower());
// Get its Children
var list1 = this._contentLoader.GetChildren<IContent>
(imageFolder.ContentLink).ToList();
// find the Children Direcoctory by its Name from the MomChildren List
var imageFolder2 = list1.FirstOrDefault(x => x.Name.ToLower() ==
testName2.ToLower());
var list3 = this._contentLoader.GetChildren<IContent>
(imageFolder2.ContentLink).ToList();
var imageFolder3 = list3.FirstOrDefault(x => x.Name.ToLower() ==
"vintage collection");
var list4 = this._contentLoader.GetChildren<IContent>
(imageFolder3.ContentLink).ToList();
foreach (var l in list4)
{
if (l.Name.ToLower() == "subChildren")
{
var list5 = this._contentLoader.GetChildren<IContent>
(l.ContentLink).ToList();
var imageFolder5 = list5.FirstOrDefault(x =>
x.Name.ToLower() == "GoodImage");
var list6 = this._contentLoader.GetChildren<IContent>
(imageFolder5.ContentLink).ToList();
var imgk = list6.FirstOrDefault(x => x.Name.ToLower() == "img1.jpg");
contentReference = imgk.ContentLink;
}
}

This is my attempt to make it dynamic:

I tried to make a loop to go through each children but I hit a wall. How to create nested loop based on number of folders ?

private ContentReference GetImageReference(string imageTitle, string
referencePath)
{
ContentReference imageReference = new ContentReference();
var familyDirectory = ExternalImagesContentProvider.GetEntryPoint();
List<string> foldersList = referencePath.Split('>').ToList();
var parentLink =
var children = this._contentLoader.GetChildren<IContent>
(familyDirectory);

var lastItem = foldersList.Last().ToLower();
foreach (var folder in foldersList)
{
foreach (var child in children.Where(x => x.Name.ToLower()
== folder.ToLower()))
{
if (child.Name.ToLower() == lastItem)
{
child.ContentLink = imageReference;
}
}
}

return imageReference;
}


Viewing all articles
Browse latest Browse all 6894

Trending Articles