Custom Actions Using CSOM For SharePoint Online

In this Series of posts  you will come to know about below two custom actions.

  • Ribbon custom action
  • Menu Item custom action

We have so many types of Custom Actions are there in SharePoint 2013

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

In this  Series of posts we 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 a empty web  asp.net 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.

customlist

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

webconfigsetting

5) Add a  asp button(btn_clEcbCustomActions) in  aspx form and also add the below code in the  click event


 try
            {
                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 newcustomaAction = collUserCustomAction.Add();
                    newcustomaAction.Location = "EditControlBlock";
                    newcustomaAction.Sequence = 100;
                    newcustomaAction.Title = "Custom List ECB Menu";
                    newcustomaAction.Url = "javascript:alert('Custom List ECB custom Action')";                  

                    clientContext.ExecuteQuery();
                    lbl_Success.Text = "Custom List ECB Menu Created Successfully";
                }
            }
            catch (Exception ex)
            {

                lbl_Error.Text = ex.Message.ToString();
            }

Note: please refer 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.

form1

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

customaction

8) When you click on the custom action we will get an alert as defined in the newcustomaAction.Url property

customaction2

9) For demo  i had given the alert message  for the URL property.

 newcustomaAction.Url = "javascript:alert('Custom List ECB custom Action')";

if we  want to open a page in the popup, update the url property as below

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 as below

popup

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

3 thoughts on “Custom Actions Using CSOM For SharePoint Online

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s