How to check a folder is exist or not in Media Library through API??

Joy Basak asked on August 3, 2017 17:33

Is there any way to check a folder is exist or not in Media Library ?? Currently i am using V10.

Recent Answers


Peter Mogilnitski answered on August 3, 2017 18:55 (last edited on August 3, 2017 18:58)

Well. Kind of. Media Library in Kenitco essentially 3 tables: Media_Lirary, Media_File and Media_rolePermision. You are interested in Media_File. If you have a folder in your Media Library it is reflected in the FilePath column of Media_File table.

  • So if a file is the root of your library: filepath is img.gif
  • if it is in a subfolder filepath: Folder1/img.gif

To check if a folder exists you check that filepath contains folder name, i.e. in terms of SQL: select top 1 * from media_file where filelibraryid = 123 and FilePath like 'folder1/%' If you get back 1 row - the folder exists

This SQL where clause you can be converted to rest, i.e. you do GET for ~/rest/media.file?format=json&topn=1&Where=UrlEncodedWhere

URL Encode this filelibraryid = 123 and FilePath like 'folder1/%' so the full URL will be:

~/rest/media.file?format=json&topn=1&Where=filelibraryid%20%3D%20123%20and%20FilePath%20like%20%27folder1%2F%25%27

If you result is not empty - Folder1 exists in the library with libraryid = 123 (check Media_Lirary to get libraryid )

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on August 3, 2017 20:36 (last edited on August 3, 2017 20:42)

I just realized you were looking for API, not for REST API :), but I guess it is pretty much the same principle

bool folderExists =  MediaFileInfoProvider.GetMediaFiles()
    .TopN(1)
    .WhereEquals("filelibraryid","123")
    .WhereStartsWith("FilePath", "Folder1")
    .Count > 0;

MediaLibraryInfoProvider has only CreateMediaLibraryFolder and DeleteMediaLibraryFolder, I guess you were looking for some sort of exist.

2 votesVote for this answer Mark as a Correct answer

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