Monday, July 29, 2013

Working with Media Items in Umbraco CMS

Different ways of  fetching media items in Umbraco CMS
Following are some different ways of fetching Media items in Umbraco CMS.

(1) To get media by id user following line of code
code : var varMedia = new umbraco.cms.businesslogic.media.Media(1);
description: This will fetch details of the media item by its id, in above code item 1 is some media item and based on this id we can find details of this media file.
-- to get file value use following line of code, in this code "umbracoFile" is the name of the property which contains name of the file
var file = varMedia.getProperty("umbracoFile");
further, you can get value by following line item
string filename = (string)file.Value


(2) To get file placed at root location on Media
code: var media = Services.MediaService.GetRootMedia().FirstOrDefault(c => c.Name == "filename");
description: This line of code will get specified file in rootmedia
-- to get file value user following line of code
string filename = media.Properties["umbracoFile"].Value.ToString()

(3) To get all files of a folder in Media
-- First, fetch id of the media folder using following line of code
var media = Services.MediaService.GetRootMedia().Where(c => c.Name == "foldername").FirstOrDefault();
- You can also check here that the item that we found is file or folder using following line of code
media.ContentType.Alias == "Folder"
-- Get if of the folder
int ID = media.Id;
-- following line of code will get all childrens/subfiles of the specified folder id. 
IEnumerable mediaChildList = Services.MediaService.GetChildren(ID);
You can loop through the child items of folder and can get specific file details

(4) To get file name using umbraco namespace on Partial View.
This line of code coulde be written on partial view.

code: var rootNode = new Node(-1).Children;
var Mainlist = ((Node)(rootNode[0])).ChildrenAsList.FirstOrDefault(c => c.Name == "foldername");

description: above line of code will get root node with given folder name
-- to get a file from above line of code
var DetailList = Mainlist.ChildrenAsList.FirstOrDefault(c => c.Name == "filename");
-- using following line of code we can get url of the file name

var src = Umbraco.Media(Convert.ToInt32(@DetailList.GetProperty("thumbnail").Value)).umbracoFile;

(5) To get file name using umbraco namespace on Partial View with other way. 
code: var rootNode = new Node(-1).ChildrenAsList.FirstOrDefault(c => c.Name == "foldername");
description: above line of code will fetch folder with name specified
-- to get child items of the folder use following line of code
var list = rootNode.ChildrenAsList;
-- to get url of the file in above folder loop through the all items in list and if file name matches, use following line of code

@node.NiceUrl

We can also get first node of the Media using following line of code code:rootNode = new Node(-1).Children;
List list = rootNode[0].ChildrenAsList;
description: above line of code will get files from first node and fetches child items from this node.       

       


No comments:

Post a Comment