Custom Actions Using CSOM For SharePoint Online

In this post, you will learn about the below two custom actions.

  • Ribbon custom action
  • Menu Item custom action

We have so many types of Custom Actions in SharePoint 2013.

  1. Menu Custom Action
  2. Page Custom Action
  3. Central Administration Custom Action
  4. Server Ribbon Custom Action

In this post, we will see how to add and remove Menu(ECB) and Ribbon custom actions to different types of lists (Custom List, Document Library and Calendar) in SharePoint online using CSOM (.NET managed) code.

Menu Custom Actions(ECB )

  1. Take an empty ASP.NET web application and add the Microsoft.SharePointOnline.CSOM nuGet package to your solution using NuGet Package Manager.

  2. Add a web form (CustomActions.aspx) to the empty project.

  3. Create a custom list in SharePoint online Site Collection, (say SampleCustomList) and add some sample items in that.

    SharePoint
  4. Add the SharePoint online Site Collection URL, username, and password in the web.config as below.

    SharePoint

  5. Add an ASP button (btn_clEcbCustomActions) in ASPX form and also, add the below code in the click event.
    1. try  
    2.            {  
    3.                string Pwd = ConfigurationManager.AppSettings["Password"].ToString();  
    4.                string UserName = ConfigurationManager.AppSettings["Username"].ToString();  
    5.                string spsiteurl = ConfigurationManager.AppSettings["SPSiteUrl"].ToString();  
    6.                var secure = new SecureString();  
    7.                foreach (char c in Pwd)  
    8.                {  
    9.                    secure.AppendChar(c);  
    10.                }  
    11.                using (var clientContext = new ClientContext(spsiteurl))  
    12.                {  
    13.                    clientContext.Credentials = new SharePointOnlineCredentials(UserName, secure);  
    14.                    var customlist = clientContext.Web.Lists.GetByTitle("SampleCustomList");  
    15.                    clientContext.Load(customlist);  
    16.                    clientContext.ExecuteQuery();  
    17.                    Microsoft.SharePoint.Client.UserCustomActionCollection collUserCustomAction = customlist.UserCustomActions;  
    18.                    UserCustomAction newcustomaAction = collUserCustomAction.Add();  
    19.                    newcustomaAction.Location = "EditControlBlock";  
    20.                    newcustomaAction.Sequence = 100;  
    21.                    newcustomaAction.Title = "Custom List ECB Menu";  
    22.                    newcustomaAction.Url = "javascript:alert('Custom List ECB custom Action')";                    
    23.    
    24.                    clientContext.ExecuteQuery();  
    25.                    lbl_Success.Text = "Custom List ECB Menu Created Successfully";  
    26.                }  
    27.            }  
    28.            catch (Exception ex)  
    29.            {  
    30.    
    31.                lbl_Error.Text = ex.Message.ToString();  
    32.            }  
    Note

    Please refer to the MSDN link for more details on Custom Action Custom Action Locations and IDs.

  6. Now, run that web application and click on the btn_clEcbCustomActions button.

    SharePoint
  7. Custom action is created successfully for the list in ECB menu.

    SharePoint
  8. When clicking on the custom action, we will get an alert as defined in the newcustomaAction.Url property.

    SharePoint

  9. For the demo, I have given the alert message for the URL property.
    1. newcustomaAction.Url = "javascript:alert('Custom List ECB custom Action')";  
    If you want to open a page in the popup, update the url property as below.
    1. newcustomaAction.Url = "javascript:OpenPopUpPageWithTitle('https://tenant.sharepoint.com/sites/sharepointmates/Lists/SampleCustomList/AllItems.aspx', RefreshOnDialogClose, 600, 400,'SampleCustomList')";  
  10. Now, the popup will come with the specified URL.

    SharePoint

Like this, we can create ECB Menu custom actions for all type of lists in SharePoint online. In the next article, we will see how to create Ribbon action for lists in SharePoint Online.

Up Next
    Ebook Download
    View all
    Learn
    View all