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

转载自:http://www.cnblogs.com/chenxizhang/archive/2010/04/25/1719721.html

 

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

我在下面这篇文章中已经做过一些介绍

http://www.cnblogs.com/chenxizhang/archive/2010/04/05/1704680.html

上文中,我已经提到了,这个工具我认为还不是很完善。但它确实留出了定制的空间。那么今天我们就来试一下吧

为什么我会想做这个事情呢,就是因为我在本系列的第10篇中提到了内容类型的开发,我们需要查看某个内容类型的定义的话,比较费劲,需要特意去写代码。所以我就想,如果直接在这个Explorer中能做这个工作,当然比较好。然后就手痒无比,马上动手做了起来。

 

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

image

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

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

下面这个链接中有比较清楚的介绍,我今天基本上是依葫芦画瓢了

http://msdn.microsoft.com/en-us/library/ee471438(VS.100).aspx

 

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

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

  3. Create a class that implements the IExplorerNodeTypeExtension interface.(创建一个类,继承一个接口)

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

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

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

 

那我们还等什么呢?

image

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

image

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

image

 

添加引用

image

image

image

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));        }    }}
image 
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
 
image 
image 
image 
image 
image 
image 
image 
image 

切换到全屏幕

image

点击“Add Content”

image

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

image

5.安装该扩展

双击vsix文件

image

image

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

image

image

激动人心的时刻到了

image

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

image

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

posted on 2010-11-04 20:09  小斌斌  阅读(308)  评论(0编辑  收藏  举报

导航