Developer forum

Forum » CMS - Standard features » Can't get path with Item type field type Folder

Can't get path with Item type field type Folder

Mafalda Correa
Mafalda Correa
Reply

Hi,

I created a Folder field in my Item Type because I needed to retrieve the path to a certain folder.

However, I can't seem to get the path from the item type.

Pageview.AreaSettings.GetItem("Custom").GetItem("CustomSettings").GetString("PathToSamplePages");

I tried with GetString() and GetValue() but that returned: 

"System.Collections.Generic.List`1[Dynamicweb.Frontend.FileViewModel]"

If I use GetFile(), I get closer, but instead of giving me the path to the folder, it returns the path to the first file in the folder.

Is there anyway that I'm not seeing to get the path to the folder?

Thank you


Replies

 
Nicolai Pedersen
Reply

You need to use GetFiles instead that will give you a list of files in the specified folder.

You can get the path like this:

 @{
        var files = Pageview.AreaSettings.GetItem("Custom").GetItem("CustomSettings").GetFiles("PathToSamplePages");
        var pathToFile = files[0].Path;
        foreach(var file in files)
        {
            @file.Name
        }
    }
 
Mafalda Correa
Mafalda Correa
Reply

But that pathToFile is going to return the path to the file, not to the folder.

Both Pageview.AreaSettings.GetItem("Custom").GetItem("CustomSettings").GetFiles("PathToSamplePages")[0] or Pageview.AreaSettings.GetItem("Custom").GetItem("CustomSettings").GetFile("PathToSamplePages") return the first file.

In my code, they return (and I tried with your code too)

"/Files/Files/Lorenz/Products/Sample_Audio/080689432385_1.mp3"

Whereas what I'm trying to get is just "/Files/Files/Lorenz/Products/Sample_Audio/"

Also, using GetFile or GetFiles will cause my code to break if the folder happens to be empty.

 
Mafalda Correa
Mafalda Correa
Reply

One way I did find to get around this was doing:

var customSettingsItem = Dynamicweb.Services.Items.GetItem("CustomWebsiteSettings", Pageview.AreaSettings.GetItem("Custom").GetItem("CustomSettings").Id);
var pathToSamplePages = customSettingsItem["PathToSamplePages"].ToString();

But it seems like an awfully long way around to get to it.

 
Nicolai Pedersen
Reply

Yes, that would give you the path to the file. Then you get the path to the folder - and add a null guard to avoid issues:

 @{
        var files = Model.Item.GetFiles("systemName");
        if (files != null && files.Any())
        {
            var pathToFile = files[0].Path;
            var pathToFolder = Path.GetDirectoryName(System.Web.HttpContext.Current.Server.MapPath(pathToFile));
            foreach (var file in files)
            {
                @file.Name
            }
        }
    }
 
Mafalda Correa
Mafalda Correa
Reply

But then, if I don't have files in the folder, I won't be able to get the path anymore. 

I shouldn't have to get a file and then find out the folder if I have a Folder type field and not a File type field.

I've figured out a way to get what I need, but maybe getting the path in a Filder type field should be revisited.

 

You must be logged in to post in the forum