关于SharePoint Explorer,是VS2010的一个新特性,可以让开发人员很方便地浏览到SharePoint站点的结构,并且做一些简单事情
1. 首先来看一下SharePoint Explorer的样子,它实际上是Server Explorer的一个插件
2. 好吧,我们如何开始来做扩展呢?
先想清楚吧,我们希望在ContentType上面有一个快捷菜单,然后点击之后,可以查看到它的属性吧。应该还是比较简单的
大致的步骤如下,我做了一个简单的翻译
To extend a SharePoint node in Server Explorer
- Create a class library project.(创建一个类库项目)
- Add references to the following assemblies:(添加如下的引用)
- Microsoft.VisualStudio.SharePoint
- Microsoft.VisualStudio.SharePoint.Explorer.Extensions
- System.ComponentModel.Composition
- Create a class that implements the IExplorerNodeTypeExtension interface.(创建一个类,继承一个接口)
- Add the System.ComponentModel.Composition.ExportAttribute attribute to the class. This attribute enables Visual Studio to discover and load your IExplorerNodeTypeExtension implementation. Pass the IExplorerNodeTypeExtension type to the attribute constructor.(添加一个Attribute)
- Add the ExplorerNodeTypeAttribute attribute to the class. This attribute specifies the string identifier for the type of node that you want to extend. To specify built-in node types provided by Visual Studio, pass one of the following enumeration values to the attribute constructor:
- ExplorerNodeTypes: Use these values to specify site connection nodes (the nodes that display site URLs), site nodes, or all other parent nodes in Server Explorer.
- ExtensionNodeTypes: Use these values to specify one of the built-in nodes that represent an individual component on a SharePoint site, such as a node that represents a list, field, or content type.(继续添加Attribute)
- In your implementation of the IExplorerNodeTypeExtension.Initialize method, use members of the nodeType parameter to add features to the node. This parameter is an IExplorerNodeType object that provides access to the events defined in the IExplorerNodeEvents interface. For example, you can handle the following events:
- IExplorerNodeEvents.NodeChildrenRequested: Handle this event to add new child nodes to the node. For more information, see How to: Add a Custom SharePoint Node to Server Explorer.
- IExplorerNodeEvents.NodeMenuItemsRequested: Handle this event to add a custom shortcut menu item to the node.
- IExplorerNodeEvents.NodePropertiesRequested: Handle this event to add custom properties to the node. The properties appear in the Properties window when the node is selected.
那我们还等什么呢?
将默认的类型名称修改为ContentTypeNodeExtension
【注意】需要确认框架版本为.NET Framework 4.0
添加引用
3.修改一些简单代码
using System;
using Microsoft.VisualStudio.SharePoint.Explorer;
using Microsoft.VisualStudio.SharePoint.Explorer.Extensions;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;
using System.Windows.Forms;
namespace Xizhang.ServerExplorerExtension
{
[Export(typeof(IExplorerNodeTypeExtension))]
[ExplorerNodeType(ExtensionNodeTypes.ContentTypeNode)]
public class ContentTypeNodeExtension : IExplorerNodeTypeExtension
{
public void Initialize(IExplorerNodeType nodeType)
{
System.IO.File.AppendAllText("c:\\log.txt",DateTime.Now.ToString());
nodeType.NodeMenuItemsRequested += new EventHandler<ExplorerNodeMenuItemsRequestedEventArgs>(nodeType_NodeMenuItemsRequested);
}
void nodeType_NodeMenuItemsRequested(object sender, ExplorerNodeMenuItemsRequestedEventArgs e)
{
IMenuItem menuItem = e.MenuItems.Add("我的菜单");
menuItem.Click += new EventHandler<MenuItemEventArgs>(menuItem_Click);
}
void menuItem_Click(object sender, MenuItemEventArgs e)
{
IExplorerNode node = (IExplorerNode)e.Owner;
MessageBox.Show(string.Format("你点击了 '{0}' 节点,类型是'{1}'.", node.Text,node.NodeType.Name));
}
}
}
4. 部署该扩展
详细的步骤可以参考http://msdn.microsoft.com/en-us/library/ee513825.aspx
更加详细的一个介绍http://msdn.microsoft.com/en-us/library/dd393694(v=VS.100).aspx
简单来说,我们需要创建一个所谓VSIX的项目来定义部署的信息,不过在此之前,你需要安装VS2010 SDK
http://www.microsoft.com/downloads/details.aspx?familyid=4659F71D-4E58-4DCD-B755-127539E21147&displaylang=en
切换到全屏幕
点击“Add Content”
设置好之后,就可以编译该项目了。(以前我们要做一个VS的扩展包可远远没这么方便)
------ Rebuild All started: Project: Xizhang.ServerExplorerExtension, Configuration: Debug Any CPU ------
Xizhang.ServerExplorerExtension -> C:\Users\Administrator\Documents\Visual Studio 2010\Projects\OrderListSolution\Xizhang.ServerExplorerExtension\bin\Debug\Xizhang.ServerExplorerExtension.dll
------ Rebuild All started: Project: Xizhang.ServerExplorer.Extension.Package, Configuration: Debug Any CPU ------
Xizhang.ServerExplorer.Extension.Package ->
Xizhang.ServerExplorer.Extension.Package ->
Xizhang.ServerExplorer.Extension.Package -> C:\Users\Administrator\Documents\Visual Studio 2010\Projects\OrderListSolution\Xizhang.ServerExplorer.Extension.Package\bin\Debug\Xizhang.ServerExplorer.Extension.Package.dll
Creating intermediate PkgDef file.
Creating VSIX Container...
Xizhang.ServerExplorer.Extension.Package -> C:\Users\Administrator\Documents\Visual Studio 2010\Projects\OrderListSolution\Xizhang.ServerExplorer.Extension.Package\bin\Debug\Xizhang.ServerExplorer.Extension.Package.vsix
Setting up Visual Studio for debugging extensions. This one-time operation may take a minute or more.
========== Rebuild All: 2 succeeded, 0 failed, 0 skipped ==========
【注意】它生成了一个vsix文件
5.安装该扩展
双击vsix文件
6. 使用该扩展。重新打开Visual Studio,可以通过Extension Manager看到我们安装好的扩展包
激动人心的时刻到了
我们可以点击“我的菜单”
到这里为止,我们这个扩展程序就实现了,如果你有兴趣,请将它修改得更加实用些吧