am trying to create a custom action for document library for sharepoint 2013 online but am unable to get it,can any one help?
-
i tried to create using app for that i reffered this(dotnetmafia.com/blogs/dotnettipoftheday/archive/2012/12/11/…)– sharepoint2013Nov 22, 2016 at 9:04
-
but that is not creating any action in my library– sharepoint2013Nov 22, 2016 at 9:04
-
please share the answer if you have for the question..– TARUNMar 6, 2017 at 6:51
1 Answer
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:
Reference - UserCustomAction class
-
-
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..?– TARUNMar 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