Portal Engine Questions on portal engine and web parts.
Version 4.x > Portal Engine > How do I access Media Library Items through the API? View modes: 
User avatar
Member
Member
bubriski-gmail - 12/10/2009 8:41:41 PM
   
How do I access Media Library Items through the API?
I have created a media library to store some images for a custom web part. How can I access them through the API?

I added a Media Library Selector to my web part, and know how to access it through the code. I also added a path selector, but I'm not sure if I can use it? It doesn't really matter though, because I can just use the whole library.

I found some code in the Media Library File List Web Part, but hit a dead end. I checked all the API classes, but couldn't seem to find anything.

Here is what I currently have:


MediaLibraryInfo mediaLibraryInfo = MediaLibraryInfoProvider.GetMediaLibraryInfo(MediaLibraryName, CMSContext.CurrentSiteName);

if (mediaLibraryInfo != null)
{
var mediaLibraryRoot = MediaLibraryHelper.GetMediaRootFolderPath(CMSContext.CurrentSiteName) + mediaLibraryInfo.
var mediaLibraryUrl = UrlHelper.GetAbsoluteUrl("~/" + CMSContext.CurrentSiteName + "/media/" + mediaLibraryInfo.LibraryFolder);
}

User avatar
Member
Member
bubriski-gmail - 12/10/2009 10:39:12 PM
   
RE:How do I access Media Library Items through the API?
Found it buried deep in the admin section controls! Actually, I may have missed it too, since it's in a somewhat obvious class. However, there is no documentation on this! Here it is!

Just replace the media library with the one you want, and the path to the images or whatever. In this example, I'm building a list of images.

Note: the path selector property type wont work for this, since it only work in the CMS Content Tree. This kinda sucks becuase that means you have to hand type any media library paths. Small price to pay for good functionality though.


var mediaLibraryInfo = MediaLibraryInfoProvider.GetMediaLibraryInfo(MediaLibraryName, CMSContext.CurrentSiteName);

if (mediaLibraryInfo != null)
{
string where = "FilePath LIKE '" + MediaLibraryHelper.EnsurePath(Path).Trim('/') + "%' AND FilePath NOT LIKE '" + MediaLibraryHelper.EnsurePath(Path).Trim('/') + "_%/%' AND FileLibraryID = " + mediaLibraryInfo.LibraryID;

// Get all files from current folder
var dataSet = MediaFileInfoProvider.GetMediaFiles(where, "FileName", 0, "FileID, FilePath, FileName, FileExtension, FileSize");

var builder = new StringBuilder();

foreach (DataRow row in dataSet.Tables[0].Rows)
{
var fileInfo = MediaFileInfoProvider.GetMediaFileInfo(int.Parse(row["FileId"].ToString()));
builder.AppendLine(string.Format("<img src=\"{0}\" />", MediaFileInfoProvider.GetMediaFileAbsoluteUrl(fileInfo.FileGUID, fileInfo.FileName)));
}

uxSlideshow.InnerHtml = builder.ToString();
}