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; clientContext.Load(docLib); clientContext.Load(uca); clientContext.ExecuteQuery(); 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"; ribbonAction.Update(); clientContext.ExecuteQuery(); 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"; ribbonDisplayAction.Update(); clientContext.ExecuteQuery(); /// 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}"; ecbAction.Update(); clientContext.ExecuteQuery();
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> <CommandUIDefinitions> <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"" /> </CommandUIDefinition> </CommandUIDefinitions> <CommandUIHandlers> <CommandUIHandler Command =""DiaryCommand"" CommandAction=""" + webUrl + @"/Lists/Diary/NewForm.aspx?DocId={SelectedItemId}&ListId={ListId}&Source={Source}"" EnabledScript=""javascript:SP.ListOperation.Selection.getSelectedItems().length == 1;"" /> </CommandUIHandlers> </CommandUIExtension>";
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.
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..
Thanks!