SharePoint 2013 – Add Custom Action through CSOM with App

Custom Action is a good feature to add new user control to site / list of SharePoint. In SharePoint 2013, I tried to add a custom action to list ribbon referring the MSDN post How to: Create custom actions to deploy with apps for SharePoint. However, the approach(Element file) as usual only works for certain situation, which doesn’t satisfy my design. After couple hours searching and testing, I found and built a solution perfectly working for me.

Requirement:

  1. Create a custom list in App Install Event
  2. Attach a custom action to the list Items ribbon control
  3. The custom action will run a javascript function to call a web service

Failed Solution:

  • Add Ribbon Custom Action to App project
  • Try to edit XML definition to attach list instance, but the XML failed in App Install Validation.
    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <CustomAction Id="6e661d80-ffa2-4460-b4d9-5b88cd1cbdf6.RibbonCustomAction1"
                    RegistrationType="List"
                    RegistrationId="{$ListId:Lists/ListInstanceTitle;}"
                    Location="CommandUI.Ribbon"
                    Sequence="10001"
                    Title="Invoke &apos;RibbonCustomAction1&apos; action">
         <CommandUIExtension>
             <CommandUIDefinitions>
                 <CommandUIDefinition Location="Ribbon.ListItem.Actions.Controls._children">
                     <Button Id="Ribbon.ListItem.Actions.RibbonCustomAction1Button"
                             Alt="Request RibbonCustomAction1"
                             Sequence="100"
                             Command="Invoke_RibbonCustomAction1ButtonRequest"
                             LabelText="Request RibbonCustomAction1"
                             TemplateAlias="o1"
                             Image32by32="_layouts/15/images/placeholder32x32.png"
                             Image16by16="_layouts/15/images/placeholder16x16.png" />
                 </CommandUIDefinition>
            </CommandUIDefinitions>
            <CommandUIHandlers>
                <CommandUIHandler Command="Invoke_RibbonCustomAction1ButtonRequest"
                                  CommandAction="javascript:CallWebService();"/>
            </CommandUIHandlers>
        </CommandUIExtension >
      </CustomAction>
    </Elements>

Succeed Solution:

  • Attach Custom Action through CSOM in App install event (Note: clearCache() is a javascript function referenced in Master Page.)customAction
    public static void AddCustomListAction(ClientContext clientContext, string listTitle)
    {
        string title = "Publish New Items";
    
        List list = SPHelper.GetList(clientContext, clientContext.Site.RootWeb, listTitle);
        UserCustomActionCollection collUCA = list.UserCustomActions;
        clientContext.Load(collUCA);
        clientContext.ExecuteQuery();
    
        bool found = false;
        UserCustomAction newUCAToRemove = null;
        for (int i = 0; i < collUCA.Count; i++)
        {
            if (collUCA[i].Title == title)
            {
                newUCAToRemove = collUCA[i];
                found = true;
                break;
            }
         }
    
         if (found)
         {
             newUCAToRemove.DeleteObject();
         }
    
         // Always replace existing custom action
         UserCustomAction action = collUCA.Add();
         action.Location = "CommandUI.Ribbon.ListView";
         action.Sequence = 1000;
         action.Title = title;
         action.CommandUIExtension = @"<CommandUIExtension><CommandUIDefinitions>"
                + "<CommandUIDefinition Location=\"Ribbon.ListItem.Actions.Controls._children\">"
                + "<Button Id=\"InvokeAction.Button\" TemplateAlias=\"o1\" Command=\"Invoke_Command\" CommandType=\"General\" LabelText=\"Publish new items\" Image32by32=\"_layouts/15/images/placeholder32x32.png\" Image16by16=\"_layouts/15/images/placeholder16x16.png\" />"
                + "</CommandUIDefinition>"
                + "</CommandUIDefinitions>"
                + "<CommandUIHandlers>"
                + "<CommandUIHandler Command =\"Invoke_Command\" CommandAction=\"javascript:clearCache();\" />"
                + "</CommandUIHandlers></CommandUIExtension>";
    
         action.Update();
    
         clientContext.ExecuteQuery();
    
    }

MSDN UserCustomAction Code Sample

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s