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.
Ribbon menus are different for Custom List, Document Library and Calendar.
Calendar
Custom List
Document 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,
- string Pwd = ConfigurationManager.AppSettings["Password"].ToString();
- string UserName = ConfigurationManager.AppSettings["Username"].ToString();
- string spsiteurl = ConfigurationManager.AppSettings["SPSiteUrl"].ToString();
- var secure = new SecureString();
- foreach(char c in Pwd) {
- secure.AppendChar(c);
- }
- using(var clientContext = new ClientContext(spsiteurl)) {
- clientContext.Credentials = new SharePointOnlineCredentials(UserName, secure);
- var customlist = clientContext.Web.Lists.GetByTitle("SampleCustomList");
- clientContext.Load(customlist);
- clientContext.ExecuteQuery();
- Microsoft.SharePoint.Client.UserCustomActionCollection collUserCustomAction = customlist.UserCustomActions;
- UserCustomAction PublishRibbonaction = collUserCustomAction.Add();
- PublishRibbonaction.Location = "CommandUI.Ribbon.ListView";
- PublishRibbonaction.Sequence = 10001;
- PublishRibbonaction.Title = "CustomListRibbonAction";
- 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>";
- PublishRibbonaction.Update();
- clientContext.ExecuteQuery();
- lbl_Success.Text = "Custom List Riboon Action Created Successfully";
- }
Now run the Web application and click on the btn_custlstRibbonCustomActions Button
Ribbon Custom action will be created for the list
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.
Note
- Since we have defined the CommandAction as popup, we are getting the Popup. We need to define the action as per the requirement.
- We have to specify the location on the custom action using the LocationAttribute like
- 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
- For the Document library and Calendar type lists we need to change only the Location attribute
- 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.
- 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,
- string Pwd = ConfigurationManager.AppSettings["Password"].ToString();
- string UserName = ConfigurationManager.AppSettings["Username"].ToString();
- string spsiteurl = ConfigurationManager.AppSettings["SPSiteUrl"].ToString();
- var secure = new SecureString();
- foreach(char c in Pwd) {
- secure.AppendChar(c);
- }
- using(var clientContext = new ClientContext(spsiteurl)) {
- clientContext.Credentials = new SharePointOnlineCredentials(UserName, secure);
- var customlist = clientContext.Web.Lists.GetByTitle("SampleDocuments");
- clientContext.Load(customlist);
- clientContext.ExecuteQuery();
- Microsoft.SharePoint.Client.UserCustomActionCollection collUserCustomAction = customlist.UserCustomActions;
- UserCustomAction PublishRibbonaction = collUserCustomAction.Add();
- PublishRibbonaction.Location = "CommandUI.Ribbon.ListView";
- PublishRibbonaction.Sequence = 10001;
- PublishRibbonaction.Title = "CustomListRibbonAction";
- 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>";
- PublishRibbonaction.Update();
- clientContext.ExecuteQuery();
- lbl_Success.Text = "Document Library Ribbon Action Created Successfully";
- }
Now run the Web application and click on the btn_custlstRibbonCustomActions Button
Custom action will be created in the Document library.
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
- string Pwd = ConfigurationManager.AppSettings["Password"].ToString();
- string UserName = ConfigurationManager.AppSettings["Username"].ToString();
- string spsiteurl = ConfigurationManager.AppSettings["SPSiteUrl"].ToString();
- var secure = new SecureString();
- foreach(char c in Pwd) {
- secure.AppendChar(c);
- }
- using(var clientContext = new ClientContext(spsiteurl)) {
- clientContext.Credentials = new SharePointOnlineCredentials(UserName, secure);
- var customlist = clientContext.Web.Lists.GetByTitle("MyCalender");
- clientContext.Load(customlist);
- clientContext.ExecuteQuery();
- Microsoft.SharePoint.Client.UserCustomActionCollection collUserCustomAction = customlist.UserCustomActions;
- UserCustomAction PublishRibbonaction = collUserCustomAction.Add();
- PublishRibbonaction.Location = "CommandUI.Ribbon.ListView";
- PublishRibbonaction.Sequence = 10001;
- PublishRibbonaction.Title = "CustomListRibbonAction";
- 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>";
- PublishRibbonaction.Update();
- clientContext.ExecuteQuery();
- lbl_Success.Text = "Calander List Riboon Action Created Successfully";
- }
Now run the Web application and click on the btn_custlstRibbonCustomActions Button
Custom action will be created in the Calendar list.
In the Next article we will see how to remove Custom Actions using CSOM.