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 action in SharePoint Online is recommended to customize the user interface, such as adding a new link to the Site Settings page, adding menu items, ribbon buttons-groups, and injecting JavaScript or CSS to SharePoint. Here is an example of adding 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://Crescent-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:
PnP PowerShell to Add a Custom Action in SharePoint Online
Use the Add-PnPCustomAction cmdlet to add a custom action in SharePoint Online.
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Retail"
#Connect to SharePoint Online Site
Connect-PnPOnline -Url $SiteURL -Interactive
#Add Custom Action
Add-PnPCustomAction -Name 'SharePoint Admin Center' -Title 'SharePoint Admin Center' -Description "SPO Admin Center" -Location 'Microsoft.SharePoint.SiteSettings' `
-RegistrationType List -Sequence 10000 -Group "SiteTasks" -Url "https://crescent-admin.sharepoint.com/"