Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Is it possible to change a property on a group of publishing pages on SharePoint 2010? We have a custom page field called "IntranetContactEmail" that we need to update across one web site. How do you do this without having to go to each page individually to edit it? thanks.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

You could use PowerShell for this. The most tricky part is checking in/out and approving depending on what type of publishing is enabled, and weather pages are already checked out by others.

Check out the stub code below. Included two methods to ensure that checkout and approve is done correctly depending on setup.

A couple of things to note:

  • BE AWARE that the code will undo checkout of pages that are not checked out by the user that is running the script
  • The user running the script should at least be site collection owner and have access to all sites
  • Run the script in a Microsoft SharePoint Management Shell, and use Run As Administrator
  • Save script in a .PS1 file

Script:

    function Set-PublishingPage
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$true, ValueFromPipeline = $true)]
        [Microsoft.SharePoint.PowerShell.SPWebPipeBind] $Web
    )
    process    
    {
        $w = $Web.Read()
        if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($w))
        {
            write-verbose "Site [$($w.Url)]"            
            $publishingSite = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($w)
            $pages = $publishingSite.GetPublishingPages()
            foreach ( $page in $pages )
            {
                write-verbose "`tPage [$($page.Url)]"


    # do something with your page




    # ensure that
    $file = $file | Ensure-Publish

    $w.Close()
            }
        }
    }
}

function Ensure-CheckOut
{
    [CmdletBinding(SupportsShouldProcess = $true)]
    param(
        [Parameter(Mandatory=$true, ValueFromPipeline = $true)]
        [Microsoft.SharePoint.SPFile] $File
    )

    process
    {
        $list = $File.DocumentLibrary

        if ($list.ForceCheckout)
        {
            $checkedOutByUser = $File.CheckedOutByUser
            if ($checkedOutByUser -ne $null)
            {
                if ($checkedOutByUser.Id -ne $File.Web.CurrentUser.Id)
                {
                    $File.UndoCheckOut()
                    $File.CheckOut()
                }
            }
            else
            {
                $File.CheckOut()
            }
        }

        return $File
    }
}

function Ensure-Publish
{
    [CmdletBinding(SupportsShouldProcess = $true)]
    param(
        [Parameter(Mandatory=$true, ValueFromPipeline = $true)]
        [Microsoft.SharePoint.SPFile] $File
    )

    process
    {
        $list = $File.DocumentLibrary

        $checkedOutByUser = $File.CheckedOutByUser
        if (($checkedOutByUser -ne $null) -and ($checkedOutByUser.Id -ne $File.Web.CurrentUser.Id))
        {
            throw "Failed to publish $($File.Name) (not checked out be me)"
        }

        if ($list.ForceCheckout -and $File.CheckOutType -ne "None")
        {
            $File.CheckIn("")
        }

        if ($list.EnableMinorVersions)
        {
            $File.Publish("")
        }

        if ($list.EnableModeration)
        {
            $File.Approve("")
        }

        return $File
    }
}


Get-PSSnapin Microsoft.SharePoint.PowerShell
$VerbosePreference = "Continue"
Get-SPWebApplication http://yourwebapp | Get-SPSite -Limit All  | Get-SPWeb -Limit All | Set-PublishingPage
share|improve this answer
    
Thanks, Anders. Very neat stuff. –  Alex C Jan 22 '12 at 12:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.