how to create media library folder with programming

toufique ahmed asked on June 16, 2015 11:24

I have created a media library in media, but i want create its content folder with code so i can upload file to that folder with custom part. custom is ready folder is required. i also want check if that folder exist or not by programming

Correct Answer

Brenden Kehren answered on June 22, 2015 13:51

The examples are also in v7 so you might want to check that out in your install Toufique.

You can check if a folder exists already by following some of the code in the "get and update" method then checking to see if the object is null or not.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Roman Hutnyk answered on June 16, 2015 23:14 (last edited on June 16, 2015 23:16)

See API Examples:

    private bool CreateMediaLibrary()
    {
        // Create new media library object
        MediaLibraryInfo newLibrary = new MediaLibraryInfo();

        // Set the properties
        newLibrary.LibraryDisplayName = "My new library";
        newLibrary.LibraryName = "MyNewLibrary";
        newLibrary.LibraryDescription = "My new library description";
        newLibrary.LibraryFolder = "MyNewLibrary";
        newLibrary.LibrarySiteID = SiteContext.CurrentSiteID;
        newLibrary.LibraryGUID = Guid.NewGuid();
        newLibrary.LibraryLastModified = DateTime.Now;

        // Create the media library
        MediaLibraryInfoProvider.SetMediaLibraryInfo(newLibrary);

        return true;
    }

    private bool CreateMediaLibrary()
{
    // Create new media library object
    MediaLibraryInfo newLibrary = new MediaLibraryInfo();

    // Set the properties
    newLibrary.LibraryDisplayName = "My new library";
    newLibrary.LibraryName = "MyNewLibrary";
    newLibrary.LibraryDescription = "My new library description";
    newLibrary.LibraryFolder = "MyNewLibrary";
    newLibrary.LibrarySiteID = SiteContext.CurrentSiteID;
    newLibrary.LibraryGUID = Guid.NewGuid();
    newLibrary.LibraryLastModified = DateTime.Now;

    // Create the media library
    MediaLibraryInfoProvider.SetMediaLibraryInfo(newLibrary);

    return true;
}
0 votesVote for this answer Mark as a Correct answer

Martin Danko answered on June 17, 2015 16:53

Hello Toufique,

Do you know that Kentico comes with the special application which will provide you with many helpful: API Examples?

You can find there also sample code for this scenario:

Kentico API Examples - Media folder

Best regards, Martin

0 votesVote for this answer Mark as a Correct answer

toufique ahmed answered on June 18, 2015 14:51 (last edited on June 18, 2015 14:56)

Thanks Martin

but i am using Kentico 7. and the answer you provided is checking against Media library i just want check if a folder exist in library. i will create library manually there will be many other folders in my library but my custom web part will check if a folder named exactly as user department exist or not. example: i have 2 departments legal and IT. i have created IT folder but not Legal. so when user loged in with department name "Legal" code should check if any folder exist with same name or not. if not then it should create one.

your help will be appreciated thanks regards Toufique

1 votesVote for this answer Mark as a Correct answer

Timothy Fenton answered on June 22, 2015 16:29

Hello Toufique

As Brenden said these examples should be included in your Kentico 7 install in the CMSSiteManager if you click the "Support" tab you should see "API Examples" then you navigate to tools > media and in there you will see the "get and update" method that Brenden is talking about. This method will show you how to look up a folder using the API, there is also a create library example that will be perfect for your code for creating the folder should one not exist.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.