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