here am trying to open new page by clicking on custom action(Ribbon) in document library using sharepoint designer here am able to open page but without passing any meta data of library item. am bit curius about it's feasibility?
1 Answer
It is possible. You can add a custom action using CSOM
as below and call a javascript
function passing the selected item ID when the custom action
button is clicked.
Have a look at the code below specially the <CommandUIExtension>
definition- you can specify your javascript
function in the CommandAction
attribute. In your function you can use the javascript CSOM
to get item details from item ID and pass those as (perhaps query strings) to the page you intend to open.
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({SelectedItemId});\" />"
+ "</CommandUIHandlers></CommandUIExtension>";
action.Update();
clientContext.ExecuteQuery();
}
Useful Links: https://lixuan0125.wordpress.com/2014/03/04/add-custom-action-through-csom-with-app/
https://msdn.microsoft.com/en-us/library/office/jj246019.aspx
-
does the same thing is possible with JSOM only?because i want to implement this in sharepoint online,sorry i forgot to mension in question.– MadhavCommented Nov 21, 2016 at 4:19
-
The above code written in CSOM will work for SharePoint online. CSOM, JSOM are both valid choices for O365. Official guidance for adding custom actions in SharePoint online is here: msdn.microsoft.com/en-us/pnp_articles/…– HarryBCommented Nov 21, 2016 at 10:44
-
so how can i add this CSOM code to so;tion if i suppose to use apps?– MadhavCommented Nov 22, 2016 at 10:10
-
Doing this via an app is one option and the guideline are there on MSDN but you can connect to your sharepoint online site remotely and execute this code to provision a custom action. The related javascript code can be added to the page using a script editor webpart.– HarryBCommented Nov 23, 2016 at 13:11