`
yake2011
  • 浏览: 18983 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

How to add a custom action to a SharePoint list actions menu for a specific list

 
阅读更多
原文链接

 

By Joe Ferner on May 10, 2009 12:35 PM

 

If you have ever tried adding a SharePoint custom action to the actions menu and tried using "List" or "ContentType" as the "RegistrationType" and then tried to specify a specific list or content type in the "RegistrationId" you know that it doesn't work. SharePoint will silently not render your custom action. If you try and target a generic list using a "RegistrationId" of "100" you will see that SharePoint will gladly render your action on every list in the site. I have found a rather kludgy work around to the problem.

First off in your element manifest file you need to use the "ControlAssembly" and "ControlClass" attributes off of CustomAction. MSDN doesn't say a whole lot about these attributes but essentially they allow you to specify a web control class in your assembly which can render the action. (If you need to create hierarchical menus check out this article.) Here is the XML to get you started:

 

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
    Id="MyCustomAction"
    RegistrationType="List"
    GroupId="ActionsMenu"
    Location="Microsoft.SharePoint.StandardMenu"
    Sequence="1000"
    ControlAssembly="[Fully qualified assembly name]"
    ControlClass="MyNamespace.MyCustomAction">
  </CustomAction>
</Elements>

 

 

Next you will need to create a web control class which renders the menu item, as seen below:

 

public class MyCustomAction : WebControl
{
    private MenuItemTemplate _action;

    protected override void CreateChildControls()
    {    
        SPWeb site = SPContext.Current.Web; 
      
        _action = new MenuItemTemplate {    
            Text = "My Action",     
            Description = "My Action",   
            ImageUrl = "/_layouts/images/NEWITEM.GIF",  
            ClientOnClickNavigateUrl = "http://www.nearinfinity.com"   
        };  
       
        Controls.Add(_action); 
    }
}

 

 

Compile and deploy your solution. Oh and make sure to add your assembly and namespace to the safe control list or you will end up pulling your hair out because once again SharePoint will silently not render your menu item. You should now see the "My Action" on every list in the site. We have now reproduced what a standard CustomAction element in your element manifest would do normally. Now we need to find a way to determine which list our control is being added to. The best way I found for doing that is to traverse the control hierarchy up the parent chain until I found the containing ListViewWebPart. The code is quite simple and looks like this:

 

    private ListViewWebPart GetParentListViewWebPart()
    {
        Control parent = Parent;
        while (parent != null)
        {
            if (parent is ListViewWebPart)
            {
                return (ListViewWebPart)parent;
            } parent = parent.Parent;
        }
        return null;
    }

 

 

You should know where I'm going with this now, but for completness I will show you the last piece of code below which restricts it to a single list instance:

 

 

    private Guid TARGET_LIST_ID = new Guid("0D9B9302-8599-4CE5-8695-1B95FE7378F1");
   
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if (!Page.IsPostBack)
        {
            EnsureChildControls();
            _action.Visible = false;
            ListViewWebPart listView = GetParentListViewWebPart();
            if (listView != null)
            {
                Guid listGuid = new Guid(listView.ListName);
                if (TARGET_LIST_ID == listGuid)
                {
                    _action.Visible = true;
                }
            }
        }
    }

 

 

Now that we have the list Guid that our control is being rendered for I will leave it as an exercise to the reader to restrict it to a given content type.

Why SharePoint doesn't provide this kind of functionality out of the box amazes me, but at least they provide a powerful API that you can get in there and work around it in a not so horribly kludgy way. Maybe SharePoint 2010 will include this :)

分享到:
评论

相关推荐

    ASP.NET Core 1.1 For Beginners: How to Build a MVC Website

    ASP.NET Core 1.1 For Beginners: How to Build a MVC Website by Jonas Fagerberg English | 19 May 2017 | ASIN: B071VX7KN4 | 411 Pages | PDF | 6.66 MB Want to learn how to build ASP.NET Core 1.1 MVC Web ...

    Simple Actions Menu for KDE

    "Simple Actions Menu for KDE" 是一个专为KDE桌面环境设计的应用程序,旨在提供一个简洁、易用的方式来管理和执行常用的操作或脚本。这个工具的主要目的是简化用户界面,让用户能够快速访问和执行自定义的命令或者...

    SharePoint Custom Action Identifiers

    自定义操作(Custom Actions)是SharePoint中的一个重要特性,它允许开发者添加额外的功能到SharePoint网站、列表、库或项目。这些操作可以是按钮、链接、下拉菜单等,用户通过这些操作执行特定任务,如启动工作流、...

    Microsoft SharePoint 2010 Developer Reference

    ### Microsoft SharePoint 2010 Developer Reference Key Points ... It serves as a valuable resource for developers looking to leverage the full potential of SharePoint in their projects.

    UE(官方下载)

    Add a language definition to your wordfile for use with UltraEdit and UEStudio's powerful syntax highlighting Syntax highlighting and code folding Explanation of highlighting and folding definitions ...

    A curated list of awesome actions to use on GitHub.zip

    A curated list of awesome actions to use on GitHub

    Foundations for Analytics with Python O-Reilly-2016-Clinton W. Brownley

    The example also shows how to store information separately in both a list and a dictionary in order to create the header row and the data rows for the output file. This is a reminder that you can ...

    SharePoint 2010 Workflow

    为SharePoint 2010 Workflow 开发 Custom Workflow Activity(Develop Custom Workflow Activity for SharePoint 2010 Workflow). SharePoint2010提供了很多有用的开箱即用的Activity(活动action),我们可以在...

    微软数据库考试70-768: Exam Ref 70-768 Developing SQL Data Models

    Module 8: Introduction to Data Analysis [removed]DAX)This module describes how to use DAX to create measures and calculated columns in a tabular data model.Lessons DAX Fundamentals Using DAX to ...

    Android代码-AndroidShortcuts

    To add or edit a new shotcut, go to /res/xml/shortcuts.xml : Handle Actions To handle shortcuts, just add new constant: private final static String CUSTOM_ACTION = "custom_action"; and check the ...

    EasyNSE 0.2

    I will show how to do this at a later date.CopyHook Handler - Hook in to Folder and Printer Moves, Copies, Deletes and Renames and either allow, deny any single action in multiple actions, or cancel ...

    As3.0 CustomActions

    在你编写as3代码的时候会用到很多类对吧,那些是系统自带的,有这个工具你可以编写自己的类,然跟使用系统的一样。

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    items a blank line is added to the list (EnumProp.Add('')). - ADD: Added the method TEnumProp.SetEnumType, setting all enumerable type items in compliance with the Delphi type: SetEnumType...

    3D.Printing.Designs.The.Sun.Puzzle.1785888897

    you'll explore various robust sculpting methods supported by Blender that allow you to edit objects with actions such as bends or curves, similar to drawing or building up a clay structure of ...

    Chucks_VBA_Reference_rev17.docx

    Methods for creating a list of open workbooks or worksheets in a ListBox control, which can be useful for presenting options to the user or organizing documents. **Change Standard Chart Colors for a ...

    SharePoint 2013 自定义扩展菜单(上)

    **Custom Action** 是SharePoint中用于向系统添加自定义行为的一种机制。它可以用来添加新的菜单项、修改现有的行为或者是执行某些特定的脚本代码等。在XML配置文件中,`CustomAction` 元素是最主要的部分,它包含了...

    Lerner -- Python Workout. 50 Essential Exercises -- 2020.pdf

    - **Objective:** Measure the time taken to execute a specific piece of code. - **Key Concepts:** - Using the `time` module to record start and end times. - Calculating elapsed time using ...

    GWT in Action

    This chapter explains how to implement event handling in GWT, including handling events for widgets and integrating event handling for actions like double-clicks. #### Chapter 7: Creating Composite ...

    Manning - Eclipse In Action.pdf

    The book titled "Eclipse in Action" provides a detailed guide for web developers on how to effectively use the Eclipse Integrated Development Environment (IDE). The authors, David Gallardo, Ed ...

Global site tag (gtag.js) - Google Analytics