Moving Datasource items to their pages using Sitecore PowerShell Extensions
Recently on a project I had a need to tidy up the Content tree. In this particular project, renderings on pages made use of Datasource items (best practice, of course!), however rather than keep these items locally to the page in a nested folder, they were all located in a Global folder.
This can certainly work, and it all depends on how you use items, but in this instance it lead to MANY items sitting under that Global folder, most of which were only used by a single item.
To tidy this up, I prepared an SPE script that would find items that are only used by a single page and then move them to sit under that item, inside a Content folder.
This is the script:
|
|
That’s all there is. The general flow of the script is:
- Recurse through all items in the content folder specified
- Find items that have only 1 referrer. Multiple referrals from different versions of the same item count as 1.
- Move those content items to sit under the referrer in a _Content folder. Create the folder if it doesn’t exist.
The top-half of the script can be executed (uncomment the return line) if you just want to identify which items can and can’t be moved.