2

am trying to create a custom action for document library for sharepoint 2013 online but am unable to get it,can any one help?

3

1 Answer 1

1

You can use Client object model csom c# to create custom action. Try below code:

public static void AddCustomListAction(ClientContext clientContext)
{
    string title = "Test Ribbon Action";

    List list = clientContext.Web.Lists.GetByTitle("Test"); 
    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:customFunction();\" />"
            + "</CommandUIHandlers></CommandUIExtension>";

     action.Update();

     clientContext.ExecuteQuery();

}

Here, using the above code, a custom javascript method, javascript:customFunction() ,would be called which would be defined on the page/masterpage/page layout etc.

Output would be similar to below screengrab:

enter image description here

Reference - UserCustomAction class

UCA using powershell

UCA using CSOM C#

4
  • where should i suppose to put this c# code in Apps solution? Nov 22, 2016 at 9:43
  • This is a console application which uses Microsoft.SharePoint.Client.dll . So, you can put it anywhere you want in the solution. In my environment, i use git repository , but you can use TFS or any other code repository and use it. Nov 22, 2016 at 9:47
  • @Gautham Shett I had tried the above code and it was working fine only for custom lists. If i replace custom list name with Document Library, its not working.. can you help me on this please..?
    – TARUN
    Mar 6, 2017 at 6:40
  • action.Location = "CommandUI.Ribbon.ListView"; change this line, it will be different in case of doc lib Mar 6, 2017 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.