SharePoint 2013: add custom ECB menu items

Source: Internet
Author: User

This article describes two common ways to add ECB menu items.

Declarative Creation

This is also recommended by Microsoft best practices. Create a Sharepoint empty solution in Vs and add an "Empty element" type SPI.

In elements. in XML, define a customaction and focus on the highlighted attributes. (In this example, a menu item is added to the document content type, and you can click to navigate to a custom application page, and pass the ID of the list of items as the parameter ):

Add to feature and deploy. The effect is as follows:

 

Server Object Model Creation

Here the feature event handler is used. This example also demonstrates how to specify a URL and open it in a dialog box. At the same time, the website URL is passed, and the ID of the selected list item is sent to the target application page.

public override void FeatureActivated(SPFeatureReceiverProperties properties){    SPSite site = (SPSite)properties.Feature.Parent;    SPWeb web=site.RootWeb;    try{                           SPList list = web.Lists["Announcements"];                   web.AllowUnsafeUpdates = true;                    if (list.UserCustomActions.Count > 0)                    {                        foreach (SPUserCustomAction action in list.UserCustomActions)                        {                            if (action.Name == "ECBItemCustomization")                            {                                action.Delete();                                list.Update();                                break;                            }                        }                    }                    SPUserCustomAction customaction = list.UserCustomActions.Add();                    customaction.Name = "ECBItemCustomization";                    customaction.Location = "EditControlBlock";                                     //customaction.ImageUrl = "/_layouts/15/images/demo/workflows.gif";                     string cAction = @"javascript: var options = {                                                url: ‘{SiteUrl}‘ + ‘/_layouts/15/demo/page.aspx/?WorkItemID={ItemId}‘,                                                allowMaximize: false,                                                width: 500,                                                height: 440 };                                            SP.UI.ModalDialog.showModalDialog(options);";                    customaction.Url = cAction;                    customaction.Sequence = 106;                    customaction.Title = "Demo ECB Title";                    customaction.Update();                    list.Update();                    web.AllowUnsafeUpdates = false;                              }          catch{ } }

Correspondingly, remove our ECB item when feature is disabled:

public override void FeatureDeactivating(SPFeatureReceiverProperties properties){    SPSite site = (SPSite)properties.Feature.Parent;    SPWeb web=site.RootWeb;    try{                           SPList list = web.Lists["Announcements"];                   web.AllowUnsafeUpdates = true;                    if (list.UserCustomActions.Count > 0)                    {                        foreach (SPUserCustomAction action in list.UserCustomActions)                        {                            if (action.Name == "ECBItemCustomization")                            {                                action.Delete();                                list.Update();                                break;                            }                        }                    }                                        web.AllowUnsafeUpdates = false;                              }          catch{ }}

To see the final result, a demo \ page. aspx application page is added. Receives URL parameters and displays the corresponding notification title. The code is relatively simple and will not be pasted. Deployment results:

 

Note:Unlike the ECB of SharePoint 2010, the ECB of SharePoint 2013 ignores the imageurl attribute. As shown in the preceding figure, the left side of the ECB 2013 item does not contain icons.

References

How to apply custom action in ECB only for document item

Add Custom Action to SharePoint Edit Control Block

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.