Tue. Jun 7th, 2022
Function Add_Site_ReadOnly_Banner([String]$srcURL, [String]$dstURL, $Credentials)
{
    Write-Host "Adding ReadOnly Banner to Source Site homepage post migration" -ForegroundColor Yellow
      try{
        $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($srcURL)
        $Ctx.Credentials = $Credentials
        Write-host "Connected to $srcURL" -ForegroundColor Green
        #Get the Web
        $rootweb = $Ctx.Web
        $Ctx.Load($rootweb)
        $Ctx.ExecuteQuery()


        $SiteRelativeURL = $srcURL.Replace("https://sp.demeter.zeus.gsi.gov.uk","")
        $HomePageRelativeURL = "SitePages/home.aspx"
        $PageURL = $SiteRelativeURL + $HomePageRelativeURL
        $Page = $Ctx.Web.GetFileByServerRelativeUrl($PageURL)
        $Ctx.Load($Page)
        $Ctx.ExecuteQuery()

        #Check if page already checked out. If so, undo check out  
        if($Page.CheckOutType -eq [Microsoft.SharePoint.Client.CheckOutType]::Online){  
            try{  
            Write-Host "Undo Checkout"  
            $Page.UndoCheckOut()  
            $ctx.load($Page)  
            $ctx.ExecuteQuery()              
            }  
            catch{  
                write-host "Error in checkout.. $($_.Exception.Message)" -foregroundcolor red  
            }  
        }
        $NewSiteURL = '<a href="' + $dstURL + '">' + $dstURL +'</a>'
        $XmlDocument = [IO.File]::ReadAllText("C:\PSScripts\Post Migration\New Site Migrated Read-Only Banner.dwp") -replace 'NewSiteLink', $NewSiteURL
        $webpartXml = [xml]$XmlDocument;
        #page.CheckOut() 
 		$ctx.ExecuteQuery() 
 		#Get the webpart manager from the page, to handle the webparts 
 		$webpartManager =                                                                                                                                           $page.GetLimitedWebPartManager([Microsoft.Sharepoint.Client.WebParts.PersonalizationScope]::Shared); 
  
		#Load and execute the query to get the data in the webparts 
 		$ctx.Load($webpartManager); 
 		$ctx.ExecuteQuery(); 
 		#Import the webpart 
 		$wp = $webpartManager.ImportWebPart($webpartXml.OuterXml) 

 	        $wpZoneID = "Left" 
 	        $wpZoneOrder= 2 
 		#Add the webpart to the page 
 		$webPartToAdd = $webpartManager.AddWebPart($wp.WebPart, $wpZoneID, $wpZoneOrder) 

 		$ctx.Load($webPartToAdd); 
 		$ctx.ExecuteQuery() 

        Write-host "Read Only now banner added to Source Site!" -ForegroundColor Green

    }catch
    {
        write-host -f Red "Error Adding Webpage" $_.Exception.Message
    }
}

This website uses cookies. By continuing to use this site, you accept our use of cookies.