Adding Custom Actions to SharePoint using CSOM

So I’ve been working with Office 365, and deploying site collections and their contents can be … fun. I’ve found that the best approach seems to be to use the Client-side Object Model (CSOM) – the C# API – to deploy and configure my content.

On thing I didn’t think would be easy was adding Custom Actions – but this turned out to be pretty easy, to be honest. Below are examples of adding custom actions to the ribbon on a View form, on a List View, and in the Enterprise Control Block (ECB) – the drop down menu for a list item:

List docLib = web.Lists.GetByTitle(documentLibraryName);
UserCustomActionCollection uca = docLib.UserCustomActions;

string cmd = "Edited for brevity - see below";

//  Adding ListView Ribbon Action
UserCustomAction ribbonAction = uca.Add();
ribbonAction.Location = "CommandUI.Ribbon.ListView";
ribbonAction.CommandUIExtension = cmd;
ribbonAction.Title = "Add Diary Entry";


string cmd2 = "Edited for brevity - see below";

//  Adding DisplayForm Ribbon Action
UserCustomAction ribbonDisplayAction = uca.Add();
ribbonDisplayAction.Location = "CommandUI.Ribbon.DisplayForm";
ribbonDisplayAction.CommandUIExtension = cmd2;
ribbonDisplayAction.Title = "Add Diary Entry";


///  Adding ECB Action
UserCustomAction ecbAction = uca.Add();
ecbAction.Location = "EditControlBlock";
ecbAction.Sequence = 295;
ecbAction.Title = "Add Diary Entry";
ecbAction.Url = webUrl + "/Lists/Diary/NewForm.aspx?DocId={ItemId}&ListId={ListId}&Source={Source}";


Pretty simple, huh? Like normal custom actions, you can set them up using the usual tokens – {ItemId}, etc..

The ribbon controls are a little more complex than the ECB example as you also have to define a CommandUIExtension, just like you would in CAML:

string cmd = @"<CommandUIExtension>
			<CommandUIDefinition Location=""Ribbon.Documents.Manage.Controls._children"">
				<Button Id=""DiaryAction.Button"" TemplateAlias=""o1"" Command=""DiaryCommand"" CommandType=""General"" LabelText=""Add Diary Entry"" Image32by32=""" + webUrl + @"/_layouts/15/images/DateRangeLast1Day_32x32.png""  />
			<CommandUIHandler Command =""DiaryCommand"" CommandAction=""" + webUrl + @"/Lists/Diary/NewForm.aspx?DocId={SelectedItemId}&amp;ListId={ListId}&amp;Source={Source}""
							  EnabledScript=""javascript:SP.ListOperation.Selection.getSelectedItems().length == 1;"" />

And yes, these custom actions are designed that they’ll take you to the new form for a different list, but passing the current list and selected item’s IDs. More on why in a future post.

Adding Custom Actions to SharePoint using CSOM

One thought on “Adding Custom Actions to SharePoint using CSOM

  1. Kiril says:

    Very nice article! But I was wondering if it is possible to add custom actions to content types as well in Office 365? It looks like the ContentType class is missing the UserCustomActions collection..



Leave a Reply

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

You are commenting using your 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