1

I am trying to add custom ribbon on the default home page of the sharepoint online site. For some of the site collection the home page 'default.aspx' shows the custom ribbon but for some of other site collections the home page'default.aspx' doesn't show the custom ribbon although if I am going to list/libarry of this site collection the custom ribbon appears.

I am using below CSOM PowerShell to add custom ribbon. Please suggest me If anything I am missing:

#Import these dlls for CSOM powershell
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"

#Tenant admin URL
$AdminURL = "https://test-admin.sharepoint.com"

#Credentials required for connecting admin tenant
$UserName = "abc@abc.com"
$Password = ConvertTo-SecureString "abcd" –asplaintext –force
#$SecurePWD = $Password | ConvertTo-SecureString -AsPlainText -Force
$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Password)  

#Path for csv file
$ConnectionsPath = "D:\AKARi\Migration\Connections\Connections.csv"

#Import csv file
$connections = Import-Csv -Path $ConnectionsPath

#Iterate through all items in csv file
foreach($connection in $connections)
{
#Creating context by connecting to a destination URL
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($connection.DestinationURL)

$ctx.credentials = $SPOCredentials  
$web = $ctx.Site.RootWeb  
$userActions = $web.UserCustomActions  
$ctx.Load($userActions)   
$ctx.ExecuteQuery() 

#If any custom action is already created, it will be deleted
$itemsToDelete = @()  
if($userActions.Count -le 0){  
    Write-Host "No Ribbon Items found to delete"  
}  
else{  
    foreach($userAction in $userActions){  
        $itemsToDelete += $userAction                  
    }  
    foreach($item in $itemsToDelete){  
        Write-Host "Deleting Ribbon Item : " $item.Title  
        $item.DeleteObject()  
    }  
    $ctx.ExecuteQuery()  
}   

#code for adding a new custom action to ribbon container  
$newRibbonItem = $userActions.Add()  
$newRibbonItem.RegistrationId = "0x01"  
$newRibbonItem.Title = "Custom Ribbon Tab"  
$newRibbonItem.RegistrationType = [Microsoft.SharePoint.Client.UserCustomActionRegistrationType]::ContentType  
$newRibbonItem.Location = "CommandUI.Ribbon" 

#Variables for image path 
$varImageUrlInternal ="https://test1.sharepoint.com/SiteAssets/akari_new%20logo_64x64.png"
$varImageUrlExternal ="https://test1.sharepoint.com/SiteAssets/akari_new%20logo_external_64x64.png"

#Flag which states that the script will be executed for Internal sites, change it to External if it is to be executed for External sites
$varSiteCategory = "Internal"

if($varSiteCategory -eq "Internal")  
{
$ribbonUI = '<CommandUIExtension>  
                    <CommandUIDefinitions>  
                        <CommandUIDefinition Location="Ribbon.Tabs._children">  
                           <Tab Id="Ribbon.Tab" Title="AKARi" Description="Custom Tab with groups and user action" Sequence="10" >  
                                <Scaling Id="Ribbon.Tab.Scaling">  
                                    <MaxSize Id="Ribbon.Tab.CustomGroup1.MinSize"  
                                        GroupId="Ribbon.Tab.CustomGroup1"  
                                        Size="LargeLayout" />  
                                </Scaling>  
                                <Groups Id="Ribbon.Tab.CustomGroups"> 
                                    <Group Id="Ribbon.Tab.CustomGroup1"  
                                        Description="Collection of user actions"  
                                        Title="Information"  
                                        Sequence="20"  
                                        Template="Ribbon.Templates.LargeLayout">  
                                        <Controls Id="Ribbon.CustomTab.CustomGroup1.Controls">

                                            <Button Id="Ribbon.Tab.CustomGroup1.ShowAlert" Alt="Show Alert" Sequence="10000"  
                                                 Command="ShowRibbonAlert"  
                                                 LabelText="This is an AKARi internal site"  
                                                 TemplateAlias="ButtonTemplate" 
                                                 Image32by32="' + $varImageUrlInternal + '" 

                                                 />  
                                        </Controls>  
                                    </Group>  
                                </Groups>  
                            </Tab> 

                       </CommandUIDefinition>  
                        <CommandUIDefinition Location="Ribbon.Templates._children">   
                            <GroupTemplate Id="Ribbon.Templates.LargeLayout">   
                                <Layout Title="LargeLayout" LayoutTitle="LargeLayout">   
                                    <Section Alignment="Top" Type="OneRow">   
                                        <Row>   
                                            <ControlRef DisplayMode="Large" TemplateAlias="ButtonTemplate" />   
                                        </Row>   
                                    </Section>   
                                </Layout>   
                            </GroupTemplate>   
                        </CommandUIDefinition>  
                    </CommandUIDefinitions>  
                    <CommandUIHandlers>  
                        <CommandUIHandler Command="ShowRibbonAlert"  
                             CommandAction="" />  
                    </CommandUIHandlers>  
                </CommandUIExtension >'
}
else
{
                $ribbonUI = '<CommandUIExtension>  
                    <CommandUIDefinitions>  
                        <CommandUIDefinition Location="Ribbon.Tabs._children">  
                           <Tab Id="Ribbon.Tab" Title="AKARi External" Description="Custom Tab with groups and user action" Sequence="10" >  
                                <Scaling Id="Ribbon.Tab.Scaling">  
                                    <MaxSize Id="Ribbon.Tab.CustomGroup1.MinSize"  
                                        GroupId="Ribbon.Tab.CustomGroup1"  
                                        Size="LargeLayout" />  
                                </Scaling>  
                                <Groups Id="Ribbon.Tab.CustomGroups"> 
                                    <Group Id="Ribbon.Tab.CustomGroup1"  
                                        Description="Collection of user actions"  
                                        Title="Information"  
                                        Sequence="20"  
                                        Template="Ribbon.Templates.LargeLayout">  
                                        <Controls Id="Ribbon.CustomTab.CustomGroup1.Controls">

                                            <Button Id="Ribbon.Tab.CustomGroup1.ShowAlert" Alt="Show Alert" Sequence="10000"  
                                                 Command="ShowRibbonAlert"  
                                                 LabelText="This is an AKARi external site"  
                                                 TemplateAlias="ButtonTemplate" 
                                                 Image32by32="' + $varImageUrlExternal + '"
                                                 />  
                                        </Controls>  
                                    </Group>  
                                </Groups>  
                            </Tab> 

                       </CommandUIDefinition>  
                        <CommandUIDefinition Location="Ribbon.Templates._children">   
                            <GroupTemplate Id="Ribbon.Templates.LargeLayout">   
                                <Layout Title="LargeLayout" LayoutTitle="LargeLayout">   
                                    <Section Alignment="Top" Type="OneRow">   
                                        <Row>   
                                            <ControlRef DisplayMode="Large" TemplateAlias="ButtonTemplate" />   
                                        </Row>   
                                    </Section>   
                                </Layout>   
                            </GroupTemplate>   
                        </CommandUIDefinition>  
                    </CommandUIDefinitions>  
                    <CommandUIHandlers>  
                        <CommandUIHandler Command="ShowRibbonAlert"  
                             CommandAction="" />  
                    </CommandUIHandlers>  
                </CommandUIExtension >'
}  
$newRibbonItem.CommandUIExtension = $ribbonUI  
$newRibbonItem.Update()  
$ctx.Load($newRibbonItem)  
$ctx.ExecuteQuery()  
Write-Output "Branding applied"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.