Resetting default Reusable Content in SharePoint 2010

 

SharePoint 2010 comes with default bits of code that the end user can snap into any page they are editing. The default items are Byline, Copyright, and Quote and they are accessible through the Insert set of commands on the Ribbon when editing a page. The default items all live in a list named "Reusable Content" and each site collection has its own reusable content list. So, that means on the web application with 128 site collections which I upgraded from SharePoint 2007 to SharePoint 2010, there are 128 instances of the resuable content list, all with defaults that don't make any sense in my organization.

So of course, I found a way to code around the problem. Below is my PowerShell script which loops through all the site collections, finds the "Reusable Content" list in the root site of each site collection, and then changes the default items in the reusable content list. I left the Quote default as-is, but removed the Copyright item since we don't use copyrights on individual pages, and I changed the byline to something which hopefully will trigger the user to remember to change it to their name. Items in the reusable content list can contain HTML code, but it has to be in the "Reusable HTML" column of the list.

One last note: because SharePoint lists are immutable, deleting an item from them changes the list size as soon as the delete call is complete. Because of this, I couldn't send the reusable content list through the ForEach-Object function to modify the list because ForEach-Object breaks if the list its operating on changes underneath it. To get around that, I just used an old-fashioned for loop like you'd see in C, Java, etc.

#------------------------------------------------------------------------------#
#       Program: Update-ReusableContent.ps1
#        Author: Steven Tomcavage
#          Date: August, 2011
#   Description: Globally changes the default reusable content, which is content 
#                that can be inserted from the edit page's Ribbon in SharePoint
#------------------------------------------------------------------------------#

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"

#----------------------------------------------------------------------------
function Fix-ReusableContent {
#----------------------------------------------------------------------------
    param ([object]$list)
    
    $listItems = $list.Items
    $numListItems = $listItems.Count
    
    # Deleting a list item changes the list data structure, so it has to
    # be done inside of a loop which counts down
    for ($i = $numListItems - 1; $i -ge 0; $i--) {
        $item = $listItems[$i]
        switch ($item.Title) {
            Byline { 
                $item["Reusable HTML"] = "<strong>By <em>&quot;Author Name&quot;</em>"
                $item.Update() 
                Write-Host "Updated Byline item in " ($list.ParentWeb).Title
                break 
            }
            Copyright { 
                $item.Delete()
                Write-Host "Deleted Copyright item in " ($list.ParentWeb).Title
                break 
            }
            default { 
                # do nothing
                break 
            }
        }       
    }
}

#----------------------------------------------------------------------------
# MAIN SCRIPT STARTS HERE
#----------------------------------------------------------------------------

# Each line this this set of commands get the result set from the previous line
Get-SPSite -Limit ALL | 
ForEach-Object { ($_.rootWeb).Lists["Reusable Content"] } | 
ForEach-Object { Fix-ReusableContent($_) }