User Custom Action

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$RootUrl = "https://<PORTAL>.sharepoint.com" #Make sure no '/' at the end
$UrlCollection = "https://<PORTAL>.sharepoint.com","https://<PORTAL>.sharepoint.com/search"
$SiteAssetFolderPath = "$($PSScriptRoot)\SiteAssets"

$UserName = "O365 USERNAME/EMAIL"
$Password = "" #If not set, script will show dialog to enter password at runtime.


if($Password) {
    $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
}
else {
    $SecurePassword = Read-Host -Prompt "Enter the password" -AsSecureString
}

Function Activate-Script([string]$Title,[int]$Sequence,[string]$ScriptSrc)
{
    $SingleCustomAction = $CustomActions | ?{$_.Title -eq $Title }
    if($SingleCustomAction)
    {
        $cAction = $SingleCustomAction[0];
        $cAction.Location = "ScriptLink";
        $cAction.Sequence = $Sequence;
        $cAction.ScriptBlock = "";
        $cAction.ScriptSrc = $ScriptSrc;
        $cAction.Update();
        $CustomActions.Update();
        #$site.Update();
        $Context.ExecuteQuery();
        Write-Host "Applying $($ScriptSrc) to $($site.Url) as UserCustomAction $($Title) [Updating]";
Write-Host ""
    }
    else
    {
        $cAction = $CustomActions.Add();
        $cAction.Title = $Title;
        $cAction.Location = "ScriptLink";
        $cAction.Sequence = $Sequence;
        $cAction.ScriptBlock = "";
        $cAction.ScriptSrc = $ScriptSrc;
        $cAction.Update();
        #$site.Update();
        $CustomActions.Update();
        $Context.ExecuteQuery();
        Write-Host "Applying $($ScriptSrc) to $($site.Url) as UserCustomAction $($Title) [Adding]";
Write-Host ""
    }
}
Function Activate-ScriptBlock([string]$Title,[int]$Sequence,[string]$ScriptBlock)
{
    $SingleCustomAction = $CustomActions | ?{$_.Title -eq $Title }
    if($SingleCustomAction) # AB Dev note, looks to be duplication of code here, can we consolidate?
    {
        $cAction = $SingleCustomAction[0];
        $cAction.Location = "ScriptLink";
        $cAction.ScriptBlock = $ScriptBlock;     
        $cAction.Sequence = $Sequence;
        $cAction.Update();
        $CustomActions.Update();
        #$site.Update();
        $Context.ExecuteQuery();
        Write-Host "Applying $($ScriptBlock) to $($site.Url) as UserCustomAction $($Title) [Updating]";
Write-Host ""
    }
    else
    {
        $cAction = $CustomActions.Add();
        $cAction.Title = $Title;
        $cAction.Location = "ScriptLink";
        $cAction.Sequence = $Sequence;
        $cAction.ScriptBlock = $ScriptBlock;
        $cAction.Update();
        #$site.Update();
        $CustomActions.Update();
        $Context.ExecuteQuery();
        Write-Host "Applying $($ScriptBlock) to $($site.Url) as UserCustomAction $($Title) [Adding]";
Write-Host ""
    }
}

Function Upload-AllFiles([Microsoft.SharePoint.Client.Web] $SPWeb, [string]$ListName, [string]$FolderPath)
{
    $List = $SPWeb.Lists.GetByTitle($ListName)
    $Context.Load($List)
    $Context.ExecuteQuery()

    Foreach ($File in (dir $FolderPath -File))
    {
        $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
        $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
        $FileCreationInfo.Overwrite = $true
        $FileCreationInfo.ContentStream = $FileStream
        $FileCreationInfo.URL = $File
        $Upload = $List.RootFolder.Files.Add($FileCreationInfo)
        $Context.Load($Upload)
        $Context.ExecuteQuery()
        $FileStream.Close()
    }
}

# Upload files to root site's Site Assets Library.

Write-Host $RootUrl
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($RootUrl)
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$SecurePassword)
$Context.Credentials = $Credentials


$web = $Context.Web
 
$Context.Load($web);
$Context.ExecuteQuery();

Upload-AllFiles -SPWeb $web -ListName "Site Assets" -FolderPath $SiteAssetFolderPath

foreach($Url in $UrlCollection)
{
    Write-Host $Url
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$SecurePassword)
    $Context.Credentials = $Credentials

    $site = $Context.Site
    $CustomActions = $site.UserCustomActions

    $Context.Load($site);
    $Context.Load($CustomActions);
    $Context.ExecuteQuery();
 
    $dateStamp = Get-Date -format "ddMMMyyyyHHmmss"

    Activate-Script -Title "<PORTAL> JQuery" -Sequence 1000 -ScriptSrc "$($RootUrl)/SiteAssets/jquery-1.8.0.min.js?v=$($dateStamp)";
    Activate-Script -Title "<PORTAL> Navigation JS" -Sequence 1002 -ScriptSrc "$($RootUrl)/SiteAssets/Navigation.js?v=$($dateStamp)";
    Activate-Script -Title "<PORTAL> Google Analytics JS" -Sequence 1002 -ScriptSrc "$($RootUrl)/SiteAssets/GoogleAnalytics.js?v=$($dateStamp)";

    Activate-ScriptBlock -Title "<PORTAL> CSS" -Sequence 1001 -ScriptBlock "document.write('<link rel=""stylesheet"" href=""$($RootUrl)/SiteAssets/Styles.css?v=$($dateStamp)"" />');";
    Activate-ScriptBlock -Title "<PORTAL> Navigation CSS" -Sequence 1005 -ScriptBlock "document.write('<link rel=""stylesheet"" href=""$($RootUrl)/SiteAssets/Navigation.css?v=$($dateStamp)"" />');";

    $Context.Dispose();
}


Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#$RootUrl = "https://<PORTAL>.sharepoint.com" #Make sure no '/' at the end
$UrlCollection = "https://<PORTAL>.sharepoint.com","https://<PORTAL>.sharepoint.com/search"

$UserName = "O365 USERNAME/EMAIL"
$Password = ""

if($Password) {
    $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
}
else {
//If we set password as empty, it will prompt from here.
    $SecurePassword = Read-Host -Prompt "Enter the password" -AsSecureString
}

foreach($Url in $UrlCollection)
{
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$SecurePassword)
    $Context.Credentials = $Credentials

    $Context.Load($Context.Site.UserCustomActions)
    $Context.ExecuteQuery();

    $Context.Site.UserCustomActions | Select Title

    $Context.Site.UserCustomActions.Clear()
    $Context.ExecuteQuery();
}


-------

{"postalcodes":[{"adminCode2":"474","adminName3":"Gandhinagar","adminCode1":"09","adminName2":"Ahmedabad","lng":73.063043,"countryCode":"IN","postalcode":"382481","adminName1":"Gujarat","placeName":"Chandlodia","lat":23.994149},{"adminCode2":"473","adminName3":"Gandhi Nagar","adminCode1":"09","adminName2":"Gandhi Nagar","lng":73.063043,"countryCode":"IN","postalcode":"382481","adminName1":"Gujarat","placeName":"Dr.Baou","lat":23.994149},{"adminCode2":"474","adminName3":"Daskroi","adminCode1":"09","adminName2":"Ahmedabad","lng":72.1960228926951,"countryCode":"IN","postalcode":"382481","adminName1":"Gujarat","placeName":"Gota","lat":22.709981466664846},{"adminCode2":"474","adminName3":"Ahmadabad City","adminCode1":"09","adminName2":"Ahmedabad","lng":73.063043,"countryCode":"IN","postalcode":"382481","adminName1":"Gujarat","placeName":"Nirnaynagar","lat":23.994149}]}

Search This Blog

Link Within Related Posts Plugin for WordPress, Blogger...