SharePoint 2013:自定義ECB菜單項的添加

本文分別介紹了兩種經常使用的添加ECB菜單項的方式。javascript

聲明式建立

這也是微軟最佳實踐推薦的方式。在VS中建立一個SharePoint空解決方案,並添加一個「空元素」類型的SPI。java

在Elements.xml中,定義一個CustomAction,重點關注一下其中高亮部分的屬性(本例在文檔內容類型的項上添加了一個菜單項,點擊導航到一個自定義應用程序頁面,並傳遞項所在的列表的Id做爲參數):web

添加到Feature,並部署。效果以下:服務器

 

服務器對象模型建立

 這裏會用到Feature的事件處理程序。本例同時還演示瞭如何指定Url,而且用對話框的方式打開。同時,還會傳遞網站Url,所選列表項的ID給目標應用程序頁面。app

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{ }
 }

相應的,要在Feature關閉時移除咱們的ECB項:ide

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{ }
}

爲了看最終效果,添加了一個demo\page.aspx應用程序頁面。接收url參數,而後顯示相應的通知標題。代碼比較簡單,就不貼了。部署看效果:
網站

 

注意:與SharePoint 2010的ECB不一樣的是,SharePoint 2013的ECB會忽略ImageUrl這一屬性。由於從前面的圖中也能夠看出,2013的ECB項左側都是不帶圖標的。url

參考資料spa

how to apply custom action in ECB only for document itemcode

Add Custom Action To SharePoint Edit Control Block

相關文章
相關標籤/搜索