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 :)
发表评论
-
Creating Hierarchical Menus with a CustomAction in SharePoint
2011-11-17 14:11 920原文链接 http://weblogs.asp.net/j ... -
SharePoint Custom Action Identifiers
2011-11-16 13:48 1011原文链接 http://johnholliday.net/ ... -
Filtering with SPGridView
2011-10-12 15:23 772原文链接 http://vspug.com/bobsbon ... -
SPGridView: Adding paging to SharePoint when using custom data sources
2011-10-11 16:55 1097原文链接 http://blogs.msdn.com/b/ ... -
SPGridView and SPMenuField: Displaying custom data through SharePoint lists
2011-10-11 16:14 1295原文链接 http://blogs.msdn.com/b/ ... -
How to work around bugs in the SPGridView control
2011-10-11 16:10 831原文链接 http://blogs.msdn.com/b/ ... -
SharePoint – Adding ECB Menu Item for Specific Custom List
2011-10-11 10:52 950原文链接 http://www.csharpest.net ... -
Adding CheckBoxes in SharePoint GridView (SPGridView)
2011-10-10 17:01 971原文链接 http://www.c-sharpcorner ... -
SharePoint的列级安全性
2011-10-10 11:25 1161原文链接 http://www.infoq.com/cn/ ... -
在 SharePoint 中自定义审核
2011-10-09 16:59 1737原文链接 http://msdn.micr ... -
SharePoint 2007 and Windows WorkFlow Foundation: Integrating Divergent Worlds
2011-10-09 16:32 1430原文链接 http://www.developer.com ... -
通过 STSDEV 简化 SharePoint 开发
2011-09-30 15:58 1299原文链接 http://msdn.microsoft.co ... -
计算字段公式
2011-09-30 15:47 899原文链接 http://msdn.microsoft ...
相关推荐
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" 是一个专为KDE桌面环境设计的应用程序,旨在提供一个简洁、易用的方式来管理和执行常用的操作或脚本。这个工具的主要目的是简化用户界面,让用户能够快速访问和执行自定义的命令或者...
自定义操作(Custom Actions)是SharePoint中的一个重要特性,它允许开发者添加额外的功能到SharePoint网站、列表、库或项目。这些操作可以是按钮、链接、下拉菜单等,用户通过这些操作执行特定任务,如启动工作流、...
### 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.
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
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 开发 Custom Workflow Activity(Develop Custom Workflow Activity for SharePoint 2010 Workflow). SharePoint2010提供了很多有用的开箱即用的Activity(活动action),我们可以在...
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 ...
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 ...
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代码的时候会用到很多类对吧,那些是系统自带的,有这个工具你可以编写自己的类,然跟使用系统的一样。
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...
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 ...
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 ...
**Custom Action** 是SharePoint中用于向系统添加自定义行为的一种机制。它可以用来添加新的菜单项、修改现有的行为或者是执行某些特定的脚本代码等。在XML配置文件中,`CustomAction` 元素是最主要的部分,它包含了...
- **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 ...
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 ...
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 ...