A frequent issue with updates to code post deployment is handling updates to files across many sites. Below is code I devised to recurse all sites and update _catalogs folder on each root web.

Adjust according to your needs!

$webAppUrl = "http://mysharepoint.com:42388"

$SPWebApp = Get-SPWebApplication $webAppUrl

foreach ($SPSite in $SPWebApp.Sites)
{
    if ($SPSite -ne $null)
    {
        $web = $SPSite.RootWeb
        $folder = $web.GetFolder("_catalogs/masterPage/Display Templates/Content Web Parts")

        $web.Title
        
        #get files to upload
        $files = Get-ChildItem -Path "$PSScriptRoot/Updates" -Force -Recurse

        foreach($file in $files){
            #upload
            
            $stream = $file.OpenRead()
            $uploaded = $folder.Files.Add($file.Name, $stream, $true);

            Write-Host $uploaded.Name  "Uploaded into the Site" -BackgroundColor Green  

            if ($stream) {$stream.Dispose()}
        }

        $SPSite.Dispose()
    }
}

 

Hope this helps anyone in the same bind

No Comments

There are no comments related to this article.

Leave a Reply

Your email address will not be published. Required fields are marked *