Question

I have two different web application with one site collection each. One Site collection is privat and the other on is public inside the intranet. The public site links to the private site which link is visible only if you have access to the private site.

This setup have been copied to a test environment where the URL have changed from company.se to companytest.se. One custom property of the list item in the Pages library still points to the old URL and needs to be updated.

I've tried to build a powershell script which would iterate through the pages library and update the property accordingly. While testing I've selected only one page to see if I get the desired result. But I don't. Probably I'm only missing some of the logics but I can't seem to get it right.

The Update-Property Script

$site = Get-SPSite https://intranettest.company.se/

$web = $site.OpenWeb("Workplace/Cooperation")

$list = $web.Lists["Pages"]

foreach ($item in $list.Items)
{
    if ($item.Name -ne "default.aspx")
    {
        if ($item.Name -eq "Intranetpage.aspx")
        {
            Write-Host $item.Name

            $file = $item.File

            $file.CheckOut()

            Write-Host "    Before change: " $item.Properties["PrivateSiteCollection"]

            $item.Properties["PrivateSiteCollection"] = $item.Properties["PrivateSiteCollection"] -replace "cooperation", "cooperationtest"

            Write-Host "    After replace: " $item.Properties["PrivateSiteCollection"]

            $item.Update()

            Write-Host "After item update: " $item.Properties["PrivateSiteCollection"]

            $file.Update()

            Write-Host "After file update: " $item.Properties["PrivateSiteCollection"]

            $file.CheckIn("Updated property")

            Write-Host "    After checkin: " $item.Properties["PrivateSiteCollection"]
        }        
    }
}

The Powershell output

PS D:\Install\Script> .\Update-Cooperation-Property-PrivateSiteCollection.ps1
Intranetpage.aspx
    Before change:  https://cooperation.company.se/Sites/Intranetpage
https://cooperationtest.company.se/Sites/Intranetpage
    After replace:  https://cooperation.company.se/Sites/Intranetpage
After item update:  https://cooperation.company.se/Sites/Intranetpage
After file update:  https://cooperation.company.se/Sites/Intranetpage
    After checkin:  https://cooperation.company.se/Sites/Intranetpage
PS D:\Install\Script>
Was it helpful?

Solution

In order to make the change persistant I needed to use the $web.GetFile to make this work properly. When I have the file, I can check out, edit properties and check in again. The answer to this question was (for me) this code block:

$site = Get-SPSite https://intranettest.company.se/

$web = $site.OpenWeb("Workplace/Cooperation")

$list = $web.Lists["Pages"]

foreach ($item in $list.Items)
{
    if ($item.Name -ne "default.aspx")
    {
        $page = $web.GetFile("Pages/" +   $item.Name)

        if ($page.Properties["PrivateSiteCollection"] -notlike "*cooperationtest*")
        {
            $page.CheckOut()

            Write-Host "    Before change: " $page.Properties["PrivateSiteCollection"]  

            $page.Properties["PrivateSiteCollection"] = $page.Properties["PrivateSiteCollection"] -replace "cooperation", "cooperationtest"

            $page.Update()

            $page.CheckIn("Updated property")

            Write-Host "    After checkin: " $page.Properties["PrivateSiteCollection"]
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top