Thursday, 15 July 2010

Replacing the Page Contact on all pages for all sites in a site collection using PowerShell

Here is the scenario: A page author at your company has decided to leave and there are a large number of publishing pages in SharePoint associated with them as a Page Contact. Your options are to a) manually replace the Page Contact on each page in each site with the person now responsible for managing publishing pages in the site collection, b) pay the old page author more money and persuade them to stay to avoid the extra management costs, or c) use this PowerShell script below to walk through each page in each site of the site collection and automatically change the Page Contact from the old page author to the new one:

#Set up default variables
$webSiteUrl = "http://portal"

#Add SharePoint assemblies
Add-Type -AssemblyName "Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
Add-Type -AssemblyName "Microsoft.SharePoint.Publishing, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c"

#Get site and web objects
$site = Get-SPSite $webSiteUrl

#Set original and new Page Contact AD accounts
$originalContact = "DOMAIN\oldpageauthor"
$newContact = "DOMAIN\newpageauthor"

#Walk through each Publishing Web and change the page contact on every page
#where the original contact equals the originalContact variable set above
$site | Get-SPWeb -limit all | ForEach-Object {
    #Check to see if site is a publishing site
    if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($_)) {
        write-host "Reviewing pages in"$_.Title"site...."
        #Get the Publishing Web and pages within it
        $publishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($_)
        $publishingPages = $publishingWeb.GetPublishingPages()
        foreach ($publishingPage in $publishingPages)
            $originalUser = $_.AllUsers[$originalContact]
            #Check if the new user exists in the web - if not, create them
                $newUser = $_.AllUsers[$newContact]
                $_.AllUsers.Add($newContact, "", "", "")
                $newUser = $_.AllUsers[$newContact]
            #Check to ensure the page isn't checked out
            if ($publishingPage.ListItem.File.CheckOutStatus -eq "None")
                #Check to ensure a Page Contact has been set on the page
                if ($publishingPage.Contact -ne $null)
                    #If the Page Contact is set to the original user,
                    #change to the new user and check in the page
                    if ($publishingPage.Contact.ToString() -eq $originalUser.ToString())
                        $publishingPage.Contact = $newUser
                        $publishingPage.ListItem.File.CheckIn("Contact name changed", [Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
                        write-host "Page contact for"$publishingPage.Title"changed to"$publishingPage.Contact.LoginName
                        #Notify administrator that the Page Contact is someone other than the originalContact specified in this script
                        write-host "Page contact for"$publishingPage.Title"is"$publishingPage.Contact.LoginName"and shall not be modified"
                    #Notify administrator that the Page Contact has not been set on this page
                    write-host "Page contact for"$publishingPage.Title"has not been set and shall not be modified"
                #Notify administrator that the page is checked out and cannot be modified
                write-host "Page"$publishingPage.Title"is currently checked out to"$publishingPage.ListItem.File.CheckedOutBy"and cannot be modified"
        #Notify administrator that the site is not a publishing site
        write-host $_.Title"is not a publishing site"

#Dispose of Site object

In summary, this script does the following:

- Connects to a site collection and walks through each site
- Checks to see if the site is a publishing site before making any changes (if not it moves on to the next site)
- Checks each publishing page in the site
- If a page is already checked out then it doesn't make any changes to the page
- If a Page Contact is set to someone else other than the old author you specified then it doesn't make any changes
- If the Page Contact is set to the old author then it will check out the page, change the contact to the new author specified and check the page back in again
- Reports all activities to the console whilst reviewing the sites - see below for an example log:

Reviewing pages in PAC Portal site....
Page contact for Home has not been set and shall not be modified
Page Query Web Parts test is currently checked out to DOMAIN\spadmin and cannot be modified
Reviewing pages in Publishing Site site....
Page contact for Home changed to DOMAIN\newpageauthor
Page contact for Test is DOMAIN\phil.childs and shall not be modifed
Reviewing pages in Search site....
Page contact for Search Center has not been set and shall not be modified
Page contact for Advanced search has not been set and shall not be modified
Page contact for People Search has not been set and shall not be modified
Page contact for Search Results has not been set and shall not be modified
Page contact for People Results has not been set and shall not be modified
Team is not a publishing site

Note: If approval is required on pages in your publishing sites, then you will need to go through manually and approve any pages changed by this script to publish them (of course, this can be done in PowerShell, but I think I’ll leave that to another time!)


  1. The Nike Flyknit Racer Triple Black colorway is a highly anticipated release that will be debuting for the holiday season.This Nike Air Max Nike Flyknit Racer receives the “Triple Black” color scheme. Both Black and Anthracite covers the Nike Flyknit upper, Nike Air Max 2016 while having murdered out Nike Swoosh logos on the sides. Completing the look are matching Black laces and Nike Huarache rubber sole.Look for the Nike Flyknit Racer “Triple Black” to release on December 2nd, 2016 at select Nike Nike Flyknit 4.0 Sportswear retail stores. The retail price tag is set at $150 USD.NOTE: Some retailers could be launching them Nike Flyknit Trainer on Black Friday, before there official release. Stay tuned to Sneaker Bar for updates.
    A Black and White Stephen Curry Shoes Nike KD 9 Oreo will officially be making a debut during the holiday season in December.Dressed in a Nike Outlet clean White and Black color scheme. This Nike KD 9 features a full Nike Flyknit constructed upper with nike free 4.0 flyknit solid Black Nike Swoosh logos. A Black speckled midsole completes the “Oreo” theme.Look for the Nike KD 9 LeBron 11 “Oreo” to release on December 15th, 2016 at select Nike Basketball retail stores. The retail price tag is Nike Roshe Flyknit set at $150 USD.
    The Nike Air Presto Greedy is the latest model to receive the “What The” Nike Flyknit mismatched theme that uses classic colors from past Nike Air Presto releases.What was thought to be a BEAMS Nike Air Max Zero collaboration, this Nike Air Presto has no affiliations with the shop. Instead, it’s Nike Sportswear’s newest “Greedy” release Nike Air Huarache as a follow up to the Air Max 95 from last year.Built with its usual neoprene panels, overlays, kyrie shoes tongue and interior lining sporting a range of vibrant colors. This Nike Air Presto is a nod to Nike Flyknit Chukka

  2. [url=][b]adidas yeezy boost 350[/b][/url]
    [url=][b]coach outlet online[/b][/url]
    [url=][b]moncler jackets[/b][/url]
    [url=][b]stan smith adidas[/b][/url]
    [url=][b]adidas uk[/b][/url]
    [url=][b]toms outlet[/b][/url]
    [url=][b]pandora charms sale clearance[/b][/url]
    [url=][b]michael kors outlet clearance[/b][/url]
    [url=][b]coach outlet store online[/b][/url]

  3. Yeezy Boost 350 Air Max 2017 Nike Vapormax Yeezy Boost 350 V2 Spinner Fidget Ray Ban Outlet Yeezy Boost 350 V2 NFL Jerseys Longchamp Outlet Adidas Yeezy Adidas UK Yeezy shoes Ray-Ban Sunglasses Yeezys Yeezy Kade Sapde Adidas Outlet Nike Store Adidas Outlet Under Armour Outlet Timberland Jordan 12 Nike Air Max Toms Outlet Adidas Ultra Boost MLB Jerseys Adidas NMD R1 Yeezy Boost 350 Oakley Outlet Kate Spade Outlet Adidas Outlet Nike Air Max 90 Vapor Max Timberland Outlet Adidas NMD Adidas EQT Ralph Lauren Toms Outlet Jordan 4 Cheap Jordan Adidas NMD Jordan Shoes Kate Spade Outlet Longchamp Outlet Adidas Yeezy Adidas NMD R1 Nike Air Max 90 Running shoes Basketball Shoes Michael Kors bags Adidas Yeezy outlet Asics Coach Factory Outlet Cheap Nike Shoes 2017 Timberland outlet Air Jordan Adidas Yeezy Nike VaporMax Air Maxvapor Nike Store Longchamp Adidas Shoes Nike outlet adidas outlet online Jordna Shoes Yeezy 350 James Shoes flip flops Air Max 2017 Asics Ray Ban Ray Bans oakley sunglasses dresses for women oakley sunglasses ray ban sunglasses Adidas NMD Air Max 97 oakley sunglasses

  4. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free.
    littlewoods contact

  5. Its an incredible joy perusing your post.Its brimming with data I am searching for and I want to post a remark that "The substance of your post is magnificent" Great work. Celebrity net worth is a celebrity finance outlet that offers a comprehensive database of celebrity net worth's in the world.