Custom Actions Using CSOM For SharePoint Online - Part Two

This is the second post for the Custom Actions Using CSOM for SharePoint Online series.

In the previous post Custom Actions Using CSOM for SharePoint Online we saw how to create ECB menu for the lists in SharePoint online

In this post we will see how create Ribbon Custom actions for different types of lists (Custom List, Document Library and Calendar).

Create 3 lists of type Custom List, Document Library and Calendar each.

3lsits

Ribbon menus are different for Custom List, Document Library and Calendar.

Calendar

calander

Custom List

customlist

Document Library

library

So the implementation is slightly different for each and every type of list.

Custom List

Add a new button(btn_custlstRibbonCustomActions) to the web form(CustomActions.aspx) created in the first post

Add the below code in the button click,

  1. string Pwd = ConfigurationManager.AppSettings["Password"].ToString();  
  2. string UserName = ConfigurationManager.AppSettings["Username"].ToString();  
  3. string spsiteurl = ConfigurationManager.AppSettings["SPSiteUrl"].ToString();  
  4. var secure = new SecureString();  
  5. foreach(char c in Pwd) {  
  6.     secure.AppendChar(c);  
  7. }  
  8. using(var clientContext = new ClientContext(spsiteurl)) {  
  9.     clientContext.Credentials = new SharePointOnlineCredentials(UserName, secure);  
  10.     var customlist = clientContext.Web.Lists.GetByTitle("SampleCustomList");  
  11.     clientContext.Load(customlist);  
  12.     clientContext.ExecuteQuery();  
  13.     Microsoft.SharePoint.Client.UserCustomActionCollection collUserCustomAction = customlist.UserCustomActions;  
  14.     UserCustomAction PublishRibbonaction = collUserCustomAction.Add();  
  15.     PublishRibbonaction.Location = "CommandUI.Ribbon.ListView";  
  16.     PublishRibbonaction.Sequence = 10001;  
  17.     PublishRibbonaction.Title = "CustomListRibbonAction";  
  18.     PublishRibbonaction.CommandUIExtension = @ "<CommandUIExtension><CommandUIDefinitions>" + "<CommandUIDefinition Location=\"Ribbon.ListItem.Actions.Controls._children\">" + "<Button Id=\"InvokeAction.Button\" TemplateAlias=\"o1\" Command=\"EditFormButtonCommand\" CommandType=\"General\" LabelText=\"CustomListRibbonAction\" Image32by32=\"_layouts/15/images/placeholder32x32.png\" Image16by16=\"_layouts/15/images/placeholder16x16.png\" />" + "</CommandUIDefinition>" + "</CommandUIDefinitions>" + "<CommandUIHandlers>" + "<CommandUIHandler Command =\"EditFormButtonCommand\" CommandAction = \"javascript:OpenPopUpPageWithTitle('https://tenant.sharepoint.com/sites/sharepointmates/_layouts/15/viewlsts.aspx', RefreshOnDialogClose, 600, 400,'CustomList Ribbon');\" EnabledScript=\"javascript: SP.ListOperation.Selection.getSelectedItems().length >= 1\" /> " + "</CommandUIHandlers></CommandUIExtension>";  
  19.     PublishRibbonaction.Update();  
  20.     clientContext.ExecuteQuery();  
  21.     lbl_Success.Text = "Custom List Riboon Action Created Successfully";  
  22. }  

Now run the Web application and click on the btn_custlstRibbonCustomActions Button

customListRibbonAction.png

Ribbon Custom action will be created for the list

listRibbonAction

After selecting the list items(1 or more) the custom action will be enabled. Once we click on the action a pop will open with given url.

listRibbonActiondemo

Note

  1. Since we have defined the CommandAction as popup, we are getting the Popup. We need to define the action as per the requirement.
  2. We have to specify the location on the custom action using the LocationAttribute like

    1. Location=\"Ribbon.ListItem.Actions.Controls._children\">"

    To find the different types avaiable for the Ribbon action, please check the MSDN post Default Server Ribbon Customization Locations
  3. For the Document library and Calendar type lists we need to change only the Location attribute
  4. Custom action image can be defined by using the propety Image32by32and Image16by16. we can give the image from layouts folder or in base64 string format.
  5. Using the EnabledScript attribute we can enable the custom action based on the condition.

Document Library

Add a new button(btn_dlRibbonCustomActions) to the web form(CustomActions.aspx) created in the first post

Add the below code in the button click,

  1. string Pwd = ConfigurationManager.AppSettings["Password"].ToString();  
  2. string UserName = ConfigurationManager.AppSettings["Username"].ToString();  
  3. string spsiteurl = ConfigurationManager.AppSettings["SPSiteUrl"].ToString();  
  4. var secure = new SecureString();  
  5. foreach(char c in Pwd) {  
  6.     secure.AppendChar(c);  
  7. }  
  8. using(var clientContext = new ClientContext(spsiteurl)) {  
  9.     clientContext.Credentials = new SharePointOnlineCredentials(UserName, secure);  
  10.     var customlist = clientContext.Web.Lists.GetByTitle("SampleDocuments");  
  11.     clientContext.Load(customlist);  
  12.     clientContext.ExecuteQuery();  
  13.     Microsoft.SharePoint.Client.UserCustomActionCollection collUserCustomAction = customlist.UserCustomActions;  
  14.     UserCustomAction PublishRibbonaction = collUserCustomAction.Add();  
  15.     PublishRibbonaction.Location = "CommandUI.Ribbon.ListView";  
  16.     PublishRibbonaction.Sequence = 10001;  
  17.     PublishRibbonaction.Title = "CustomListRibbonAction";  
  18.     PublishRibbonaction.CommandUIExtension = @ "<CommandUIExtension><CommandUIDefinitions>" + "<CommandUIDefinition Location=\"Ribbon.Documents.Copies.Controls._children\">" + "<Button Id=\"InvokeAction.Button\" TemplateAlias=\"o1\" Command=\"EditFormButtonCommand\" CommandType=\"General\" LabelText=\"CustomListRibbonAction\" Image32by32=\"_layouts/15/images/placeholder32x32.png\" Image16by16=\"_layouts/15/images/placeholder16x16.png\" />" + "</CommandUIDefinition>" + "</CommandUIDefinitions>" + "<CommandUIHandlers>" + "<CommandUIHandler Command =\"EditFormButtonCommand\" CommandAction = \"javascript:OpenPopUpPageWithTitle('https://tarundev.sharepoint.com/sites/sharepointmates/_layouts/15/viewlsts.aspx', RefreshOnDialogClose, 600, 400,'CustomList Ribbon');\" EnabledScript=\"javascript: SP.ListOperation.Selection.getSelectedItems().length >= 1\" /> " + "</CommandUIHandlers></CommandUIExtension>";  
  19.     PublishRibbonaction.Update();  
  20.     clientContext.ExecuteQuery();  
  21.     lbl_Success.Text = "Document Library Ribbon Action Created Successfully";  
  22. }  
Now run the Web application and click on the btn_custlstRibbonCustomActions Button

 

DocLibform

Custom action will be created in the Document library.

DocLibform2

Calander

Add a new button(btn_calRibbonCustomActions) to the web form(CustomActions.aspx) created in the first post

Add the below code in the button click

  1. string Pwd = ConfigurationManager.AppSettings["Password"].ToString();  
  2. string UserName = ConfigurationManager.AppSettings["Username"].ToString();  
  3. string spsiteurl = ConfigurationManager.AppSettings["SPSiteUrl"].ToString();  
  4. var secure = new SecureString();  
  5. foreach(char c in Pwd) {  
  6.     secure.AppendChar(c);  
  7. }  
  8. using(var clientContext = new ClientContext(spsiteurl)) {  
  9.     clientContext.Credentials = new SharePointOnlineCredentials(UserName, secure);  
  10.     var customlist = clientContext.Web.Lists.GetByTitle("MyCalender");  
  11.     clientContext.Load(customlist);  
  12.     clientContext.ExecuteQuery();  
  13.     Microsoft.SharePoint.Client.UserCustomActionCollection collUserCustomAction = customlist.UserCustomActions;  
  14.     UserCustomAction PublishRibbonaction = collUserCustomAction.Add();  
  15.     PublishRibbonaction.Location = "CommandUI.Ribbon.ListView";  
  16.     PublishRibbonaction.Sequence = 10001;  
  17.     PublishRibbonaction.Title = "CustomListRibbonAction";  
  18.     PublishRibbonaction.CommandUIExtension = @ "<CommandUIExtension><CommandUIDefinitions>" + "<CommandUIDefinition Location=\"Ribbon.Calendar.Events.Actions.Controls._children\">" + "<Button Id=\"InvokeAction.Button\" TemplateAlias=\"o1\" Command=\"EditFormButtonCommand\" CommandType=\"General\" LabelText=\"CustomListRibbonAction\" Image32by32=\"_layouts/15/images/placeholder32x32.png\" Image16by16=\"_layouts/15/images/placeholder16x16.png\" />" + "</CommandUIDefinition>" + "</CommandUIDefinitions>" + "<CommandUIHandlers>" + "<CommandUIHandler Command =\"EditFormButtonCommand\" CommandAction = \"javascript:OpenPopUpPageWithTitle('https://tarundev.sharepoint.com/sites/sharepointmates/_layouts/15/viewlsts.aspx', RefreshOnDialogClose, 600, 400,'CustomList Ribbon');\" EnabledScript=\"javascript: SP.ListOperation.Selection.getSelectedItems().length >= 1\" /> " + "</CommandUIHandlers></CommandUIExtension>";  
  19.     PublishRibbonaction.Update();  
  20.     clientContext.ExecuteQuery();  
  21.     lbl_Success.Text = "Calander List Riboon Action Created Successfully";  
  22. }  
Now run the Web application and click on the btn_custlstRibbonCustomActions Button

 

calanderformaction

Custom action will be created in the Calendar list.

calanderaction

In the Next article we will see how to remove Custom Actions using CSOM.

Up Next
    Ebook Download
    View all
    Learn
    View all