关于SharePoint Explorer,是VS2010的一个新特性,可以让开发人员很方便地浏览到SharePoint站点的结构,并且做一些简单事情 

1. 首先来看一下SharePoint Explorer的样子,它实际上是Server Explorer的一个插件

MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer_ide

2. 好吧,我们如何开始来做扩展呢?

先想清楚吧,我们希望在ContentType上面有一个快捷菜单,然后点击之后,可以查看到它的属性吧。应该还是比较简单的

 

大致的步骤如下,我做了一个简单的翻译

To extend a SharePoint node in Server Explorer
  1. Create a class library project.(创建一个类库项目)
  2. Add references to the following assemblies:(添加如下的引用)
  • Microsoft.VisualStudio.SharePoint
  • Microsoft.VisualStudio.SharePoint.Explorer.Extensions
  • System.ComponentModel.Composition
  1. Create a class that implements the IExplorerNodeTypeExtension interface.(创建一个类,继承一个接口)
  2. 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)
  3. 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)
  1. 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:

 

那我们还等什么呢?

MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer_microsoft_02

将默认的类型名称修改为ContentTypeNodeExtension

MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer_html_03

【注意】需要确认框架版本为.NET Framework 4.0

MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer_d3_04

 

添加引用

MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer_ide_05

MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer_ide_06

MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer_扩展包_07

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


切换到全屏幕

MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer_ide_08

点击“Add Content”

MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer_microsoft_09

设置好之后,就可以编译该项目了。(以前我们要做一个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文件

MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer_html_10

5.安装该扩展

双击vsix文件

MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer_ide_11

MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer_扩展包_12

6. 使用该扩展。重新打开Visual Studio,可以通过Extension Manager看到我们安装好的扩展包

MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer_microsoft_13

MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer_html_14

激动人心的时刻到了

MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer_microsoft_15

我们可以点击“我的菜单”

MOSS 2010:Visual Studio 2010开发体验(11)——扩展SharePoint Explorer_microsoft_16

到这里为止,我们这个扩展程序就实现了,如果你有兴趣,请将它修改得更加实用些吧