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 had see 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 slight different for each and every type of list.
Custom List:
1) Add a new button(btn_custlstRibbonCustomActions) to the web form(CustomActions.aspx) created in the first post
2)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"; }
3) Now run the Web application and click on the btn_custlstRibbonCustomActions Button
4) Ribbon Custom action will be created for the list
5) After selected the the list items(1 or more) the custom action will be enabled. once we click on the action a pop will opened with given url.
Note :
- Since we are defined the CommandAction as popup,we are getting the Popup. we need to the defined the action as per the requirement.
- We have to specify the location on the custom action using the Location Attribute 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 Image32by32 and 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:
1) Add a new button(btn_dlRibbonCustomActions) to the web form(CustomActions.aspx) created in the first post
2)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"; }
3) Now run the Web application and click on the btn_custlstRibbonCustomActions Button
4) Custom action will be created in the Document library.
Calander:
1) Add a new button(btn_calRibbonCustomActions) to the web form(CustomActions.aspx) created in the first post
2)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"; }
3) Now run the Web application and click on the btn_custlstRibbonCustomActions Button
4) Custom action will be created in the Calendar list.
[…] the previous posts Part1 and Part 2 we have how to creat the custom actions. In this post we will see how to delete the custom […]
LikeLike