SharePoint Online: Add Custom Action using PowerShell

Requirement: Create Custom Action in SharePoint Online using PowerShell

How to Add Custom Action in SharePoint Online?

User custom actions in SharePoint Online is a recommended way to customizing the user interface, such as adding a new link to the Site Settings page, add menu items, ribbon buttons-groups, inject JavaScript or CSS to SharePoint. Here is an example to add a 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

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply