用PowerShell脚本实现对SharePoint页面Title的修改

存在这样一种情况,对应的page已经部署到product的SharePoint环境中,那么在部署下一个版本的时候就不允许把已经创建好的page删除再创建,因此page中修改过的属性就不能再次部署到SharePoint中。

鉴于这种情况,我们可以手动的在SharePoint环境中修改page的属性(例如:title),也可以编写feature或者PowerShell脚本处理这种修改。

如下便是通过PowerShell脚本修改page的title属性:

#webapp url
$webAppUrl = http://url.../

#web urls
$rootWebUrl = $webAppUrl + "haha"
$rttaWebUrl = $webAppUrl + "haha/rtta"
$supportWebUrl = $webAppUrl + "haha/support"

#web pages arrays
$rootWebPages = @("Pages/rootpage1.aspx", "Pages/rootpage2.aspx", "Pages/rootpage3.aspx", "Pages/rootpage4.aspx")
$rttaWebPages = @("Pages/rttapage1.aspx", "Pages/rttapage2.aspx", "Pages/rttapage3.aspx")
$supportWebPages = @("Pages/supportpage1.aspx", "Pages/supportpage2.aspx", "Pages/supportpage3.aspx")

#If you don't want to change any page's title in target web. Define variables as following.
#$rootWebPages = ""
#$rttaWebPages = ""
#$supportWebPages = ""


#title arrays
$rootWebPagesTitle = @("My Root Page1", "My Root Page2", "My Root Page3", "My Root Page4")
$rttaWebPagesTitle = @("My Page1", "My Page2", "My Page3")
$supportWebPagesTitle = @("Your Page1", "Your Page2", "Your Page3")


#
#change target web pages title
#
function ChangeTargetWebPagesTitle([string]$pageType)
{
    Write-Host -BackgroundColor DarkGreen -ForegroundColor White ("Start change target web pages title logic ");
    
    if($pageType -eq "root")
    {
        $webUrl = $rootWebUrl
        $webPagesArray = $rootWebPages
        $webPagesTitleArray = $rootWebPagesTitle
    }
    if($pageType -eq "rtta")
    {
        $webUrl = $rttaWebUrl
        $webPagesArray = $rttaWebPages
        $webPagesTitleArray = $rttaWebPagesTitle
    }
    if($pageType -eq "support")
    {
        $webUrl = $supportWebUrl
        $webPagesArray = $supportWebPages
        $webPagesTitleArray = $supportWebPagesTitle
    }

    if($webPagesArray.Length -gt 0)
    {
        $spWeb = Get-SPWeb -Identity $webUrl
        if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($spWeb))
        {
            $spPubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($spWeb)
            $pages = $spPubWeb.PagesList
            foreach($item in $pages.Items)
            {
                $pubPage = [Microsoft.SharePoint.Publishing.PublishingPage]::GetPublishingPage($item)
                for($i = 0; $i -lt $webPagesArray.Length; $i++)
                {
                    if (($pubPage.Url -eq $webPagesArray[$i]) -and ($pubPage.Title -ne $webPagesTitleArray[$i]))
                    {     
                        $pubPage.CheckOut()
                        $pubPage.Title = $webPagesTitleArray[$i]
                        $pubPage.Update();
                        $pubPage.CheckIn("")
                        $pageFile = $pubPage.ListItem.File;
                        $pageFile.Publish("");
                        #pageFile.Approve(checkInComment); 
                        
                        Write-Host -BackgroundColor DarkGreen -ForegroundColor White ("Change title to: " + $webPagesTitleArray[$i]);
                        break
                     }
                }        
            }
        }
        $spWeb.Dispose()
    }
    
    Write-Host -BackgroundColor DarkGreen -ForegroundColor White ("Finish change target web pages title logic ");
}

#
#change root web pages title
#
function ChangeRootWebPagesTitle
{
    Write-Host -BackgroundColor DarkGreen -ForegroundColor White ("Start ChangeRootWebPagesTitle Function");
    if($rootWebPages.Length -gt 0)
    {
        ChangeTargetWebPagesTitle("root")
    }
    Write-Host -BackgroundColor DarkGreen -ForegroundColor White ("Finish ChangeRootWebPagesTitle Function");
}

#
#change rtta web pages title
#
function ChangeRTTAWebPagesTitle
{
    Write-Host -BackgroundColor DarkGreen -ForegroundColor White ("Start ChangeRTTAWebPagesTitle Function");
    if($rttaWebPages.Length -gt 0)
    {
        ChangeTargetWebPagesTitle("rtta")
    }
    Write-Host -BackgroundColor DarkGreen -ForegroundColor White ("Finish ChangeRTTAWebPagesTitle Function");
}

#
#change support web pages title
#
function ChangeSupportWebPagesTitle
{
    Write-Host -BackgroundColor DarkGreen -ForegroundColor White ("Start ChangeSupportWebPagesTitle Function");
    if($supportWebPages.Length -gt 0)
    {
        ChangeTargetWebPagesTitle("support")
    }
    Write-Host -BackgroundColor DarkGreen -ForegroundColor White ("Finish ChangeSupportWebPagesTitle Function");
}

#call function to change root web pages title
ChangeRootWebPagesTitle
#call function to change rtta web pages title
ChangeRTTAWebPagesTitle
#call function to change support web pages title
ChangeSupportWebPagesTitle

Write-Host -BackgroundColor DarkGreen -ForegroundColor White ("================ Finish change the pages title ==================");

 

。。。。。。。。。。

posted @ 2013-09-06 15:59  Eric Sun  阅读(1208)  评论(0编辑  收藏  举报