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

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

    SharePoint 2010 Workflow

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

    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 ...

    How To Cheat In Maya 2012 Tools And Techniques For Character Animation

    The Maya guide for animators, How to Cheat in Maya 2012 presents everything you need to know about character animation in Maya. Fully updated for the latest revision of Maya, this book provides you ...

    微软数据库考试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 ...

    Ansible.From.Beginner.to.Pro.1484216601

    You will learn to build your own modules to perform actions specific to your business. By the end you will create an entire cluster of virtualized machines, all of which have your applications and ...

    Advanced Apple Debugging & Reverse Engineering v0.9.5

    The LLDB command command regex acts much like command alias, except you can provide a regular expression for input which will be parsed and applied to the action part of the command. 10. Assembly ...

    CakePHP 1.3 Application Development Cookbook.pdf

    profiles, and how to build custom Route classes to obtain even more flexibility. Chapter 7, Creating and Consuming Web Services: Web services are essential when looking forward to expose application ...

    a project model for the FreeBSD Project.7z

    This project model is not meant to be a tool to justify creating impositions for developers, but as a tool to facilitate coordination. It is meant as a description of the project, with an overview of...

    The Essential Guide to HTML5

    specify events and event handling to produce animation and respond to user actions. You can include video and audio on your web site with standard controls, or place the video or audio in your ...

    Professional.Microsoft.SharePoint.2007.Workflow.Programming

    You’ll discover how to deploy workflow template files and features, program SharePoint external data exchange, send WCF messages from a SharePoint workflow, and develop custom Office SharePoint ...

    Android代码-Scorekeeper

    These actions are all fairly simple to implement because this app is built on a SQLite database. I made a 4-minute video describing the bascis of how the SQLite Database works in this app: ...

Global site tag (gtag.js) - Google Analytics