Swapping Templates

Posted 02/10/2014 by Dexter Yoon

I recently came across a Sitecore implementaion where users want to store "custom" media items in Sitecore's Media Library.  The end user wanted to upload an item of a custom media template type to the media library, but also wanted the default sitecore media template attributes and the upload functionality to remain the same.

To solve this problem, we begin by looking at the sitecore's default media templates located at:/sitecore/templates/System/Media/Unversioned

The base template for all Sitecore media items is the File template:

/sitecore/templates/System/Media/Unversioned/File

To accomodate the user's needs, a copy of all the Unversioned media templates were created.

We removed all the fields and the base template references from the copy of the unversioned media templates.  And, we replace them with references to:

1.) A custom base template - The custom base template is called Upload File and contains the custom fields requested the by the users.

2.) Default sitecore media template – the custom media template will reference its corresponding Sitecore media template.  For example,

The /sitecore/templates/[Project]/[Project_Name]/Media/Unversioned/Audio template will reference the /sitecore/templates/System/Media/Unversioned/Audio template as its base template.

Since we have created the custom media templates, we can focus on how we will handle creating the new custom media items in Media Library. 

To handle the swapping of the templates while the user is attempting to upload a media file, we add a custom pipeline in the uiUpload section of the web.config.  Note, that the pipeline must be created after the Upload.Save process.

 Below is a code snippet

    public void Process(UploadArgs args)
        {
           Assert.ArgumentNotNull(args, "args");

            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                //iterate through each media item that was uploaded
                Item mediaRoot = null;
                foreach (Item mediaItem in args.UploadedItems)
                {
                    //check if this item is being created under the correct folder in Media Library 
                    //MediaRoot template contains name value pair list. 
                    //short id guid of sitecore system media template --> guid of custom media template
                    if (IsParentItemMediaRoot(mediaItem, out mediaRoot))
                    {
                        string idOfTemplate = FetchNameValueList(mediaItem, mediaRoot);
                        ChangeMediaTemplate(mediaItem, GetMediaTemplate(idOfTemplate));
                    }
                }
            }
        }

The users will upload media files to a set path designated in the Media Library.  The item that the custom media items will reside in is under an item called Media Root.  The Media Root item will have one field that is a Name Value List field type.

The name value list field will contain key value of the short id guid of the Sitecore media item.  The corresponding value is the GUID of the custom media item that we have defined.

We check if the item is being created under the Media Root item.  If it is, then we check if the upload media item has been created under the correct path.  If it is in the path that will contain the custom media items, we will return the Media Root item.

   string idOfTemplate = FetchNameValueList(mediaItem, mediaRoot);

   ChangeMediaTemplate(mediaItem, GetMediaTemplate(idOfTemplate));
}

Swapping the templates is rather simple, but the work involved before getting to this point was more time consuming.

//Swap template from the Sitecore system media template with corresponding custom media template
        public void ChangeMediaTemplate(Item mediaItem, Item customTemplate)
        {
            try
            {                
                //change template to custom template - UploadFile template
                mediaItem.Editing.BeginEdit();
                mediaItem.ChangeTemplate(new TemplateItem(new TemplateItem(customTemplate)));
                mediaItem.Editing.EndEdit();
            }
            catch (Exception ex)
            {
                Log.Error("Could not save posted file: ", ex, this);
            }
        }

You can see below that the newly created item is of the custom template that we have defined.  The item will have the new fields and the default Sitecore media item fields.

This feature will not affect the creation of items elsewhere in the Media Library as demonstrated in the below screenshot. 


Share:

Archive

Syndication