Link to home
Start Free TrialLog in
Avatar of Howard Bash
Howard BashFlag for United States of America

asked on

Need to add custom button to SharePoint List View Ribbon

I have this pnp script which adds a button to a list view ribbon.

It runs and I can delete the button in script and know that is was there and deleted by count.

But, the pnp script to add the button doesn't work until after I had run a console app to add a button to the list.  

It is as though some structure needs to be setup that the pnp script isn't doing.

Here is the pnp script

$cUIExtn = "<CommandUIExtension><CommandUIDefinitions><CommandUIDefinition Location=""Ribbon.ListItem.Actions.Controls._children"">
<Button Id=""Ribbon.List.Items.Action.MyButton"" Alt=""Promote Request"" 
Sequence=""03"" Command=""ShowPromoMoreButtons"" 
LabelText=""Promote Selected"" 
TemplateAlias=""o1"" 
Image32by32=""/sites/Practice/images/Perspective-Button-Reboot-icon-32.png"" 
Image16by16=""/sites/Practice/images/Perspective-Button-Reboot-icon-16.png"" />
</CommandUIDefinition></CommandUIDefinitions>
<CommandUIHandlers><CommandUIHandler Command=""ShowPromoMoreButtons"" 
CommandAction=""javascript: PROMOTION.clickMethod();""/></CommandUIHandlers>
</CommandUIExtension>"

Add-PnPCustomAction -Name "MoreButtonsReq" -Title "Promote This" -Description "MoreButtonsReq" -Location "CommandUI.Ribbon" -Group "SiteActions" -RegistrationType List -registrationId "{gggggggg-gggg-421a-gggg-246b00ef7a82}" -Rights EditListItems -Scope Web -CommandUiExtension $cUIExtn

Open in new window



Here is the console app c#

                SPSite osite = new SPSite(SiteURL);

                SPWeb oweb = osite.OpenWeb();
                SPList olist = oweb.Lists[ListName];

                //Add New SPUserCustomAction            
                SPUserCustomAction action = olist.UserCustomActions.Add();

                foreach (var customAction in olist.UserCustomActions)
                {
                    string extension = customAction.CommandUIExtension;
                    if (extension.IndexOf("PROMOTION") > 0)
                    {
                        Console.WriteLine("Found PROMOTION Button and removing before adding updated version");
                        customAction.Delete();
                        olist.Update();
                        break;
                    }
                }

                //Location of the action should be ribbon..           
                action.Location = "CommandUI.Ribbon";

                //This one is Optional as we are adding the same later
                action.ImageUrl = Image32;

                //This is the major input..
                action.CommandUIExtension = @"<CommandUIExtension>" +
                        "<CommandUIDefinitions><CommandUIDefinition Location='Ribbon.ListItem.Actions.Controls._children'>" +
                            " <Button Id='Ribbon.List.Items.Action.MyButton' " +
                                    "Sequence='03' " +
                                    "Alt='MyCustom' " +
                                    "Command='testToShow' " +
                                    "Image16by16='" + Image16 + "' " +
                                    "Image32by32='" + Image32 + "' " +
                                    "LabelText='Promote It' " +
                                    "ToolTipTitle='Promote item' " +
                                    "ToolTipDescription='Click to Promote' " +
                                    "TemplateAlias='o1'></Button>" +
                        "</CommandUIDefinition></CommandUIDefinitions>" +
                        "<CommandUIHandlers>" +
                        "<CommandUIHandler Command='testToShow' " +
                                            "CommandAction='javascript: PROMOTION.clickMethod();'>" +
                            "</CommandUIHandler>" +
                        "</CommandUIHandlers>" +
                        "</CommandUIExtension>";

                action.Update();

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.