SharePoint Online: Add Custom Action using PowerShell

Requirement: Create Custom Action in SharePoint Online using PowerShell

How to a Add Custom Action in SharePoint Online?
User custom actions in SharePoint Online is a recommended way to customizing user interface, such as adding new link to Site Settings page, add menu items, ribbon buttons-groups, inject JavaScript or CSS to SharePoint. Here is an example to add custom action in SharePoint Online.

SharePoint Online Add Custom Action using PowerShell
Here is the PowerShell to Create Custom Action in SharePoint Online.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/"
$CustomActionTitle = "SharePoint Admin Center"
 
Try{
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get Existing Custom Actions
    $Web = $Ctx.Web
    $UserCustomActions= $Web.UserCustomActions
    $Ctx.Load($UserCustomActions)
    $Ctx.ExecuteQuery()

    #Check if the CustomAction Exists already
    $CustomAction = $UserCustomActions | Where { $_.Title -eq $CustomActionTitle } | Select -First 1

    If($CustomAction -eq $Null)
    {
        #Add new custom action
        $UserCustomAction = $Ctx.Web.UserCustomActions.Add()

        #Set the Properties of the custom action
        $UserCustomAction.Name = $CustomActionTitle
        $UserCustomAction.Title = $CustomActionTitle
        $UserCustomAction.Location = "Microsoft.SharePoint.SiteSettings"
        $UserCustomAction.Group = "SiteTasks" #Site Actions
        $UserCustomAction.Sequence = 1000
        $UserCustomAction.Url = "https://crescenttech-admin.sharepoint.com/_layouts/15/online/SiteCollections.aspx"    
        $UserCustomAction.Update()

        $Ctx.ExecuteQuery()
        Write-Host -f Green "Custom Action Added Successfully!"
    }
    Else
    {
         write-host -f Yellow "Custom Action Already Exists!"
    }
}
Catch {
        write-host -f Red "Error Adding Custom Action!" $_.Exception.Message
}

This Adds a custom action to create a link under site settings in SharePoint Online
sharepoint online add custom action powershell
SharePoint Online: Add Custom Action using PowerShell SharePoint Online: Add Custom Action using PowerShell Reviewed by Salaudeen Rajack on July 08, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.