Subscribe:

Labels

Thursday, September 27, 2018

3. Update Logo image to All Site collection under one Web Application


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 $Site=Get-SPSite http://test:2222
 $SPWebApp = $Site.WebApplication
 #$SPWebApp = Get-SPWebApplication

foreach ($SPSite in $SPWebApp.Sites)
{
 $siteurl = Get-SPWeb $SPSite.Url
 $weburl=$SPSite.Url+ "/PublishingImages"
  write-host "$weburl"
#call the upload function
uploadimagefile $weburl "C:\Logo.GIF" "Background Process Upload" $true $true "Approved by background process"
#write-host "$siteurl"
}

# Note: for testing purposes, simply set $listUrl to a valid SharePoint Document Library and $file to the path of a local file to upload and then run the script

function uploadimagefile($listUrl,$file,$checkInComment,$publishMajorVersion,$approve,$approvalComment)
{
$destUri = $null
$localFile = get-item -LiteralPath $file
$site = $null
try {
# can we open the SharePoint site from the URL?
$destUri = [System.Uri]($listUrl)
write-output "Opening: $listUrl"
$site = [Microsoft.SharePoint.SPSite]($listUrl)
} catch {
$msg = $error[0].Exception.Message
write-output "Unable to open site '$ListUrl' - $msg"
exit 6
}
$queryParams = [System.Web.HttpUtility]::ParseQueryString($destUri.Query)
$folderPath = $queryParams["RootFolder"]
if ($folderPath -eq $null) {
$folderPath = $destUri.LocalPath
}
$web = $site.OpenWeb()
$folder = $null
$list = $null
$file = $null
$exitCode = 0
try {
$folder = $web.GetFolder($folderPath)
$list = $web.GetList($listUrl)
$bRequiresCheckIn = $false
if ($list -eq $null) { throw "Unable to open list" }
if ($folder -eq $null -or $folder.Exists -eq $false) { throw "Unable to open folder '$folderPath'" }
#if the library requires a check out, we must do that first
if ($list.ForceCheckout -eq $true) {
$folder.Files | %{
if ($_.Name -eq $localFile.Name) {
#if we have a match, check it out
if ($_.CheckOutType -ne "None") {
$_.UndoCheckOut()
}
$_.CheckOut()
$bRequiresCheckIn = $true
}
}
}
$stream = $localFile.OpenRead()
$newFile = $folder.Files.Add($folder.Url + "/" + $localFile.Name, $stream, $true, $checkInComment, $false)
$stream.Close()
if ($bRequiresCheckIn -eq $true -or $publishMajorVersion -eq $true) {
$revType = 0 # minor rev
if ($list.EnableVersioning -eq $true -and $publishMajorVersion -eq $true) { $revType = 1 }
if ($list.EnableVersioning -eq $false) { $revType = 2 }
if ($newFile.CheckOutType -eq "None") { $newFile.CheckOut() }
$newFile.CheckIn($checkInComment, $revType)
}
if ($list.EnableModeration -eq $true -and $approve -eq $true) {
$newFile.Approve($approvalComment)
}
Write-output "File Uploaded successfully"
} catch {
$msg = $error[0].Exception.Message
write-output "Unable to open list Url '$ListUrl' - $msg"
}

}


No comments:

Post a Comment