As part of integrating one Sitecore instance into another, reorganization was needed for proper placements of media library items. With two instances sharing some of the same media paths (think sitecore/media library/images, for example), integrating one instance’s media into another instance could become problematic. Best practice for media library item organization should assume site-specific paths and proper restrictions of image fields, anchored to specific media library folders. However, in this case, media library items were stored in more common item paths, opening the possibility of conflicts between the two instances.
The approach we took was to script the move of items to site-specific folders, ensuring no conflicts were encountered. The following scripts were executed in three steps:
Updating template source fields to point to a new path
Remove unnecessary path references in image fields (carry-over from a pre-Sitecore 7 deployment. This was deprecated in Sitecore 8)
Moving of the media library to the new path
Step 1: Update Template Image Source Values
The following function accepts the name of a site as a parameter and uses that to append to a default /sitecore/Media Library/sites folder for site-specific media item storage. That value is then used to replace existing, more generic references:
Step 2: Remove Unnecessary Path References
Running this script was not necessary for the movement of media, it was more of a clean-up task to eliminate path references to images that were about to be moved. While Sitecore has deprecated this method of item lookup, you can still see these values in the raw values of the image field. This method simple searches for all instances of mediapath and removed them.
Step 3: Moving Media Library Items to New Path
The final steps performs the movement of media library items to their new site-specific folder.