Skip to content

Instantly share code, notes, and snippets.

@vikbehal
Created June 6, 2019 20:44
Show Gist options
  • Save vikbehal/27afd7aca17a11c37ae39e3773fc1ec9 to your computer and use it in GitHub Desktop.
Save vikbehal/27afd7aca17a11c37ae39e3773fc1ec9 to your computer and use it in GitHub Desktop.
#Load SharePoint CSOM Assemblies : Begin
$CSOMAssembliesPath = "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\"
$ClientDllPath = $CSOMAssembliesPath + "Microsoft.SharePoint.Client.dll"
$ClientRunTimeDllPath = $CSOMAssembliesPath + "Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path $ClientDllPath -ErrorAction SilentlyContinue
Add-Type -Path $ClientRunTimeDllPath -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
#Load SharePoint CSOM Assemblies : End
#Variable declaration: Begin
$Source_ListName = "SharePoint Migration Tracker"
$PATH = "C:\Users\my-machine\Desktop\BannerCode"
$SourceTenantUrl = "https://<your-tenant>.sharepoint.com/sites/<site-URL>"
$SourceRelativeUrl = ""
$SourceSite = "$SourceTenantUrl$SourceRelativeUrl"
#Setup Credentials to connect
$User = "username"
$Password="Password"
$Pass = $Password | ConvertTo-SecureString -Force -AsPlainText
$SPOCredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Pass
$SourceCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($SPOCredentials.UserName , $SPOCredentials.Password)
$AddBanner = $TRUE
#Variable declaration: End
function Add-SPBanner($Context, $site)
{
$UserCustomActions = $site.UserCustomActions
$context.Load($UserCustomActions)
$context.ExecuteQuery()
$UserCustomActions | Where-Object {$_.Title -Like "Banner*"} | Select-Object Title, Sequence
if($UserCustomActions.Count -gt 0)
{
Write-Host "Custom Action Already Added..."
}
else
{
$newAction = $UserCustomActions.Add()
$newAction.Location = "ScriptLink"
$newAction.scriptSrc = "~SiteCollection/SiteAssets/jquery.min.js"
$newAction.Sequence = 30000
$newAction.Title= "BannerJquery"
$newAction.Update()
$context.ExecuteQuery()
#add custom js injection action
$customJSAction = $UserCustomActions.Add()
$customJSAction.Location = "ScriptLink"
#reference to JS file
$customJSAction.ScriptSrc = "~SiteCollection/SiteAssets/banner.js"
#load it last
$customJSAction.Title= "BannerJS"
$customJSAction.Sequence = 30001
#make the changes
$customJSAction.Update()
$context.ExecuteQuery()
Write-Host "Banner has been Added…" -ForegroundColor Green
}
}
function Remove-SPBanner($context, $site)
{
$UserCustomActions = $site.UserCustomActions
$context.Load($UserCustomActions)
$context.ExecuteQuery()
#"? = Where-object"
#"% = foreach-object"
#$_ means the current object inside a foreach or where-object
$UserCustomActions | Where-Object {$_.Title -Like "Banner*"} | Select-Object Title, Sequence
if($UserCustomActions.Count -gt 0)
{
$CA = $UserCustomActions | ? Title -eq "BannerJquery"
$CA.DeleteObject()
$CA = $UserCustomActions | ? Title -eq "BannerJS"
$CA.DeleteObject()
$context.ExecuteQuery()
Write-Host "Banner has been Removed…" -ForegroundColor Green
}
}
function UploadFile($ctx, $libraryName, $fileName)
{
# Get the name of the file.
$UniqueFileName = [System.IO.Path]::GetFileName($fileName)
$File = dir $fileName -File
# Get the folder to upload into.
$Docs = $ctx.Site.RootWeb.Lists.GetByTitle($libraryName)
$ctx.Load($ctx.Site.RootWeb)
$ctx.Load($Docs)
$ctx.Load($Docs.RootFolder)
$ctx.ExecuteQuery()
$FileStream = New-Object IO.FileStream($FileName,[System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $UniqueFileName
$Upload = $Docs.RootFolder.Files.Add($FileCreationInfo)
$ctx.Load($Upload)
$ctx.ExecuteQuery()
return $Null
}
function isSiteCollectionReady($siteURL){
$allReady = $TRUE;
$allSites= [System.Collections.ArrayList]@()
$allTargetSites= [System.Collections.ArrayList]@()
#Set up the context
$siteSourceContext = New-Object Microsoft.SharePoint.Client.ClientContext($SourceSite)
$siteSourceContext.Credentials = $SourceCredentials
#Get the List
$siteMigrationList = $siteSourceContext.Web.Lists.GetByTitle($Source_ListName)
#Using CamlQuery to get relevant items
$siteCamlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$siteCamlQuery.ViewXml = "<View><Query><Where>
<Eq><FieldRef Name='SiteCollectionURL'/><Value Type='Text'>"+$siteURL+"</Value></Eq>
</Where></Query></View>"
$SubSites = $siteMigrationList.GetItems($siteCamlQuery)
$siteSourceContext.Load($SubSites)
$siteSourceContext.ExecuteQuery()
foreach($row in $SubSites)
{
if($row["MigrationStatus"] -ne 'Ready for Go Live'){
$allReady = $FALSE
break
}
else{
#Add sub-site & target URL to array
$allSites += $row["SiteURL"]
$allTargetSites += $row["SPOSiteURL"]
}
}
return $allReady, $allSites, $allTargetSites
}
function changeStatus($siteURL){
#Set up the context
$siteSourceContext = New-Object Microsoft.SharePoint.Client.ClientContext($SourceSite)
$siteSourceContext.Credentials = $SourceCredentials
#Get the List
$siteMigrationList = $siteSourceContext.Web.Lists.GetByTitle($Source_ListName)
#Using CamlQuery to get relevant items
$siteCamlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$siteCamlQuery.ViewXml = "<View><Query><Where>
<Eq><FieldRef Name='SiteCollectionURL'/><Value Type='Text'>"+$siteURL+"</Value></Eq>
</Where></Query></View>"
$SubSites = $siteMigrationList.GetItems($siteCamlQuery)
$siteSourceContext.Load($SubSites)
$siteSourceContext.ExecuteQuery()
foreach($row in $SubSites)
{
#Now update the SPO LIst item with STatus
$row["MigrationStatus"] = "Live"
$row.Update()
$siteSourceContext.ExecuteQuery()
}
}
#Set up the context
$SourceContext = New-Object Microsoft.SharePoint.Client.ClientContext($SourceSite)
$SourceContext.Credentials = $SourceCredentials
#Get the List
$MigrationList = $SourceContext.Web.Lists.GetByTitle($Source_ListName)
#Using CamlQuery to get relevant items
$camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$camlQuery.ViewXml = "<View><Query><Where><And>
<Eq><FieldRef Name='MigrationStatus'/><Value Type='Choice'>Ready for Go Live</Value></Eq>
<Eq><FieldRef Name='IsSiteCollection'/><Value Type='Boolean'>1</Value></Eq>
</And></Where></Query></View>"
$MigrationListItems = $MigrationList.GetItems($camlQuery)
$Sourcecontext.Load($MigrationListItems)
$Sourcecontext.ExecuteQuery()
write-host "Total Number of Ready site-collections found:"$MigrationListItems.Count
$AddBanner = $TRUE
#foreach
foreach($Item in $MigrationListItems)
{
$Id = $Item.Id
#Get value for each column of the list
#$Title = $Item[ "ADD YOUR COLUMNNAME"]
$OnPremUrl = $Item["SiteCollectionURL"]
$TargetUrl = $Item["SPOSiteURL"]
$checkSite = isSiteCollectionReady($OnPremUrl)
if($checkSite[0]){
#Get URLs
$allSubsitesURLs = $checkSite[1]
$allTargetURLs = $checkSite[2]
Write-host "*******"
Write-Host "Found" $checkSite[1].Count "sites for" $OnPremUrl
if(-NOT $AddBanner){
Set-SPSite -Identity $OnPremUrl -LockState "Unlock" -Verbose
}
for ($i=0; $i -lt $allSubsitesURLs.length; $i++) {
#Get Source and destination
$subsite = $allSubsitesURLs[$i]
$siteTargetURL = $allTargetURLs[$i]
Write-Host "Adding banner on:" $subsite "for with URL" $siteTargetURL -ForegroundColor Green
#foreach($subsite in $allSubsitesURLs){
#Write-Host $Id $OnPremUrl
#Now Set the Site to Read Only
$OnPremContext = New-Object Microsoft.SharePoint.Client.ClientContext($subsite)
$OnPremContext.Credentials = $OnPremCreds
$OnPremSite = $OnPremContext.Site
$OnPremContext.Load($OnPremSite)
$OnPremWeb = $OnPremContext.Web
$OnPremContext.Load($OnPremWeb)
$OnPremContext.Load($OnPremWeb.AllProperties)
$OnPremContext.ExecuteQuery()
if($AddBanner){
UploadFile -ctx $OnPremContext -libraryName "Site Assets" -fileName "$PATH\banner.js"
#Setting property of the sites....
$allProperties= $OnPremWeb.AllProperties
$allProperties["TargetUrl"] = $siteTargetURL
$OnPremWeb.Update()
$OnPremContext.ExecuteQuery()
#Add Banner
Add-SPBanner -Context $OnPremContext -site $OnPremSite
}
else{
#Just in Case you want to remove the banner
Remove-SPBanner -context $OnPremContext -site $OnPremSite
}
$OnPremContext.Dispose()
}
if($AddBanner){
#Set Site to Read Only
Set-SPSite -Identity $OnPremUrl -LockState "ReadOnly" -Verbose
}
Write-host "*******"
changeStatus($OnPremUrl)
Write-host "Processed" $OnPremUrl "successfully"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment