Iterate Media Tree - .NET UserControl Options
psterling@homax
Posted: Friday, May 16, 2008 7:33:35 PM

Rank: Fanatic

Joined: 10/30/2007
Posts: 215
Location: Bellingham
I'm a bit hung up on this one, any pointers or solutions would be appreciated.

I'd like to iterate through the Umbraco Media Tree in order to display a list of folders (in the proper hierarchy). I know that T. Hoehler has done this for his Utilities for Umbraco, but in this case I'm looking for a fairly simple approach to include in a user control.

I also noted this thread, but it appears to be open still: http://forum.umbraco.org/yaf_postst2179_How-to-get-the-MediaTreeRoot-using-C.aspx

I can get as far as listing the Media Nodes and determining the ContentType ("Folder" in this case), but can't seem to get to the next step (nodeName, ParentId, etc...)

Code:
           
Media[] m = umbraco.cms.businesslogic.media.Media.GetRootMedias();

            for (int i = 0; i < m.Length; i++)
            {
                if (m[i].ContentType.Alias.Equals("Folder"))
                {...


Thanks, -Paul

motusconnect.com :: level-2 certified :: MVP 2008/2009
drobar
Posted: Friday, May 16, 2008 10:17:45 PM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 1,698
Location: KY, USA
Can you just use the GetMedia() function for each folder and set the boolean to go deep?

(not that I've ever done it, but that's how I expect it to work :)

cheers,
doug.

MVP 2007-2009 - Official Umbraco Trainer for North America - Percipient Studios
jHodgkinson
Posted: Saturday, May 17, 2008 5:00:16 AM
Rank: Fanatic

Joined: 3/15/2007
Posts: 378
Location: Cary, NC USA
Paul - not sure if this helps or not but here's some code I use to index media files (variant of umbraco logic):

Code:


        public static Guid _mediaObjectType = new Guid("b796f64c-1f99-4ffb-b886-4bf4bc011a9c");

        /// <summary>
        /// Retrieve a list of all medias and folders
        /// </summary>
        /// <returns></returns>
        public static umbraco.cms.businesslogic.media.Media[] GetAllMediaItems()
        {
            Guid[] mediaNodeIds = umbraco.cms.businesslogic.CMSNode.getAllUniquesFromObjectType(_mediaObjectType);

            umbraco.cms.businesslogic.media.Media[] retval = new umbraco.cms.businesslogic.media.Media[mediaNodeIds.Length];
            for (int i = 0; i < mediaNodeIds.Length; i++)
            {
                umbraco.cms.businesslogic.media.Media d = new umbraco.cms.businesslogic.media.Media(mediaNodeIds[i]);
                retval[i] = d;
            }
            return retval;
        }
    
        public static string Index()
       {

        ........

            umbraco.cms.businesslogic.media.Media[] medias = GetAllMediaItems();
            foreach (umbraco.cms.businesslogic.media.Media mediaItem in medias)
            {
                try
                {
                    if (mediaItem.ContentType.Alias == "File" && (";" + Settings.MediaTypesToIndex + ";").IndexOf(";" + mediaItem.Text.Split('.').GetValue(1).ToString() + ";") > -1)
                    {
                        XmlDocument doc = new XmlDocument();
                        if (AddNodeToIndex(mediaItem.ToXml(doc, true), writer, excludeIds))
                            mediaCounter++;
                    }
                }
                catch { }
            }

       .........
       }

psterling@homax
Posted: Monday, May 19, 2008 10:10:32 PM

Rank: Fanatic

Joined: 10/30/2007
Posts: 215
Location: Bellingham
Thanks John -

That got me on the right track! The below iterates the media tree, from here I apply the output to a TreeView control or whatever else makes sense.

Code:
           
          Media[] m = GetAllMediaItems();  // see John's post above

            for (int i = 0; i < m.Length; i++)
            {
                if (m[i].ContentType.Alias.Equals("Folder"))
                { 
                    output += m[i].Id.ToString() + " is a folder :: ";
                    output += m[i].Text + " is the Name :: ";

                    if (m[i].Level > 1)
                        output += m[i].Parent.Id.ToString() + " is the Parent :: ";
                }            
            }


-Paul

motusconnect.com :: level-2 certified :: MVP 2008/2009
jHodgkinson
Posted: Tuesday, May 20, 2008 7:44:05 PM
Rank: Fanatic

Joined: 3/15/2007
Posts: 378
Location: Cary, NC USA
sweet - good deal...
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.