演练:扩展服务器资源管理器以显示 Web 部件

在 Visual Studio 中,可以使用 服务器资源管理器SharePoint 连接 节点来查看 SharePoint 网站的组件。默认情况下,每隔 服务器资源管理器 不显示某些元素。在本演练中,将扩展 服务器资源管理器,以使其显示在每个连接的 SharePoint 网站的 Web 部件库。

本演练将演示以下任务:

  • 创建一个 Visual Studio 扩展,此扩展可通过下列方式扩展**“服务器资源管理器”**:

    • 该扩展添加一个 Web 部件库 节点在 服务器资源管理器的每个 SharePoint 网站节点下。该新节点包含的每个子节点均表示网站上的 Web 部件库中的一个 Web 部件。

    • 扩展定义表示 Web 部件实例新的节点。该新节点类型是新**“Web 部件库”节点下的子节点的基础。该新 Web 部件节点类型在“属性”**窗口中显示有关其表示的 Web 部件的信息。节点类型还包括可以使用的自定义快捷菜单项,以用作执行与 Web 部件相关的其他任务的起点。

  • 创建扩展程序集调用的两个自定义 SharePoint 命令。SharePoint 命令是在服务器对象模型可由扩展程序集调用使用 API 为 SharePoint 的方法。在本演练中,您可以创建一些命令,这些命令会从开发计算机上的本地 SharePoint 网站中检索 Web 部件信息。有关更多信息,请参见调入 SharePoint 对象模型

  • 生成 Visual Studio 扩展 (VSIX) 包以部署扩展。

  • 调试并测试扩展。

说明说明

有关使用 SharePoint 客户端对象模型而不是其服务器对象模型本演练的替代版本,请参见 演练:在服务器资源管理器扩展中调入 SharePoint 客户端对象模型

系统必备

您需要在开发计算机上安装以下组件才能完成本演练:

了解以下概念很有用,但对于完成本演练并不是必需的:

创建项目

若要完成本演练,您必须创建三项:

  • 一个用于创建 VSIX 包以部署扩展的 VSIX 项目。

  • 一个用于实现扩展的类库项目。此项目必须面向 .NET Framework 4.5。

  • 一个用于定义自定义 SharePoint 命令的类库项目。此项目必须面向 .NET Framework 3.5。

从创建项目开始本演练。

创建 VSIX 项目

  1. 启动 Visual Studio。

  2. 在菜单栏上,选择**“文件”“新建**、“项目”

  3. 新建项目 对话框中,展开 visual C#Visual Basic 节点,然后选择 扩展性 节点。

    说明说明

    只有在安装 Visual Studio SDK,扩展性 节点可用。有关更多信息,请参见本主题前面的系统必备部分。

  4. 在对话框顶部,选择在 .NET Framework 的版本列表的 .NET Framework 4.5

  5. 选择 VSIX 项目 模板,将项目命名为 WebPartNode,然后选择 确定 按钮。

    Visual Studio 将**“WebPartNode”项目添加到“解决方案资源管理器”**中。

创建扩展项目

  1. 解决方案资源管理器,请打开解决方案节点的快捷菜单上,选择 添加,然后选择 新建项目

    说明说明

    在 Visual Basic 项目中,仅当在General, Projects and Solutions, Options Dialog Box中选中“总是显示解决方案”复选框时,解决方案节点才会出现在“解决方案资源管理器”中。

  2. 新建项目 对话框中,展开 visual C# 节点或 Visual Basic 节点,然后选择 Windows 节点。

  3. 在对话框顶部,选择在 .NET Framework 的版本列表的 .NET Framework 4.5

  4. 在项目模板列表中,选择 类库,将项目命名为 WebPartNodeExtension,然后选择 确定 按钮。

    Visual Studio 将**“WebPartNodeExtension”**项目添加到解决方案中,并打开默认的 Class1 代码文件。

  5. 从项目中删除 Class1 代码文件。

创建 SharePoint 命令项目

  1. 解决方案资源管理器,请打开解决方案节点的快捷菜单上,选择 添加,然后选择 新建项目

    说明说明

    在 Visual Basic 项目中,仅当在General, Projects and Solutions, Options Dialog Box中选中“总是显示解决方案”复选框时,解决方案节点才会出现在“解决方案资源管理器”中。

  2. 新建项目 对话框中,展开 visual C# 节点或 Visual Basic 节点,然后选择 Windows 节点。

  3. 在对话框顶部,选择在 .NET Framework 的版本列表的 .NET Framework 3.5

  4. 在项目模板列表中,选择 类库,将项目命名为 WebPartCommands,然后选择 确定 按钮。

    Visual Studio 将**“WebPartCommands”**项目添加到解决方案中,并打开默认的 Class1 代码文件。

  5. 从项目中删除 Class1 代码文件。

配置项目

在创建扩展编写代码,您必须先添加代码文件和程序集引用,并配置项目设置。

配置 WebPartNodeExtension 项目

  1. 在 WebPartNodeExtension 项目中,添加四个具有下列名称的代码文件:

    • SiteNodeExtension

    • WebPartNodeTypeProvider

    • WebPartNodeInfo

    • WebPartCommandIds

  2. 打开 WebPartNodeExtension 项目的快捷菜单,然后选择 添加引用

  3. 引用管理器– WebPartNodeExtension 对话框中,选择 框架 选项,以下程序集中的每个然后选中复选框:

    • System.ComponentModel.Composition

    • System.Windows.Forms

  4. 选择 扩展 选项,为 Microsoft.VisualStudio.SharePoint 程序集的复选框,然后选择 确定 按钮。

  5. 解决方案资源管理器,打开 WebPartNodeExtension 项目节点的快捷菜单,然后选择 属性

    随即打开**“项目设计器”**。

  6. 选择 应用程序 选项。

  7. 默认命名空间 框 (C#) 或 根命名空间 框 (Visual Basic),输入 ServerExplorer.SharePointConnections.WebPartNode。

配置 WebPartCommands 项目

  1. 在 WebPartCommands 项目中,添加名为 WebPartCommands 的代码文件。

  2. 解决方案资源管理器,打开 WebPartCommands 项目节点的快捷菜单,选择 添加,然后选择 现有项

  3. 添加现有项 对话框中,浏览到包含 WebPartNodeExtension 项目的代码文件的文件夹,然后选择 WebPartNodeInfo 和 WebPartCommandIds 代码文件。

  4. 添加 按钮旁边的下箭头,然后在出现的菜单中的 添加为链接

    Visual Studio 将代码文件作为链接添加到 WebPartCommands 项目中。因此,代码文件位于 WebPartNodeExtension 项目中,但文件中的代码还是在 WebPartCommands 项目中进行编译。

  5. 再次打开 WebPartCommands 项目的快捷菜单,并选择 添加引用

  6. 引用管理器– WebPartCommands 对话框中,选择 扩展 选项,以下程序集中的每个复选框,然后选择 确定 按钮:

    • Microsoft.SharePoint

    • Microsoft.VisualStudio.SharePoint.Commands

  7. 解决方案资源管理器,请再次打开 WebPartCommands 项目的快捷菜单,然后选择 属性

    随即打开**“项目设计器”**。

  8. 选择 应用程序 选项。

  9. 默认命名空间 框 (C#) 或 根命名空间 框 (Visual Basic),输入 ServerExplorer.SharePointConnections.WebPartNode。

为新节点创建图标

为**“服务器资源管理器”扩展创建两个图标:一个图标用于新的“Web 部件库”节点,另一个图标用于“Web 部件库”**节点下的每个子 Web 部件节点。在本演练后面的部分中,您将编写代码以将这些图标与节点相关联。

为节点创建图标

  1. 解决方案资源管理器,打开 WebPartNodeExtension 项目的快捷菜单,然后选择 属性

  2. 随即打开**“项目设计器”**。

  3. 选择资源选项卡,然后选择该此项目中不包含默认资源文件。单击此处创建 链接。

    Visual Studio 创建一个资源文件并在设计器中打开该文件。

  4. 在设计器的顶部,请在 添加资源 菜单命令旁边的下箭头,然后在出现的菜单中的 添加新图标

  5. 添加新资源 对话框,则为新图标 WebPartsNode,然后选择 添加 按钮。

    新图标将在**“图像编辑器”**中打开。

  6. 编辑 16x16 版本的图标,使其具有您可以轻松地识别的设计。

  7. 打开 32x32 版本的快捷菜单,然后选择 删除图像类型

  8. 重复添加第二个图标的第 5 步到第 8 步到项目资源,并将此图标 WebPart。

  9. 解决方案资源管理器,在 WebPartNodeExtension 项目的 资源 文件夹下,打开 WebPartsNode.ico的快捷菜单。

  10. 属性 窗口,请在 生成操作旁边的下箭头,然后在出现的菜单中的 嵌入的资源

  11. 对于**“WebPart.ico”**,重复最后两个步骤。

向服务器资源管理器中添加 Web 部件库节点

创建一个类,此类可将新的**“Web 部件库”**节点添加到每个 SharePoint 网站节点中。若要添加新节点,此类应实现 IExplorerNodeTypeExtension 接口。时,就要实现此接口来扩展现有节点的行为。服务器资源管理器(例如,添加子节点为节点。

向服务器资源管理器中添加 Web 部件库节点

  • 在 WebPartNodeExtension 项目中,打开 SiteNodeExtension 代码文件,然后将下面的代码粘贴到它。

    说明说明

    在添加此代码后,项目将会出现一些编译错误,但是,它们将消失,当您将在后面的步骤中代码。

    Imports System.Collections.Generic
    Imports System.ComponentModel.Composition
    Imports Microsoft.VisualStudio.SharePoint.Explorer
    
    Namespace ServerExplorer.SharePointConnections.WebPartNode
    
        ' Export attribute: Enables Visual Studio to discover and load this extension.
        ' ExplorerNodeType attribute:  Indicates that this class extends SharePoint site nodes in Server Explorer.
        ' WebPartNodeTypeProvider class: Represents an extension of SharePoint site nodes in Server Explorer.
        <Export(GetType(IExplorerNodeTypeExtension))> _
        <ExplorerNodeType(ExplorerNodeTypes.SiteNode)> _
        Friend Class SiteNodeExtension
            Implements IExplorerNodeTypeExtension
    
            Private Sub Initialize(ByVal nodeType As IExplorerNodeType) _
                Implements IExplorerNodeTypeExtension.Initialize
    
                ' The NodeChildrenRequested event is raised when the user expands the
                ' SharePoint site node in Server Explorer.
                AddHandler nodeType.NodeChildrenRequested, AddressOf NodeChildrenRequested
            End Sub
    
            ' Creates the new Web Part Gallery node with the specified icon.
            Private Sub NodeChildrenRequested(ByVal Sender As Object, ByVal e As ExplorerNodeEventArgs)
    
                ' The CreateWebPartNodes argument is a delegate that Visual Studio calls 
                ' to create the child nodes under the Web Part Gallery node.
                e.Node.ChildNodes.AddFolder("Web Part Gallery", My.Resources.WebPartsNode.ToBitmap(), _
                    AddressOf CreateWebPartNodes)
            End Sub
    
            ' Creates all of the individual Web Part nodes under the new Web Part Gallery node.
            Private Sub CreateWebPartNodes(ByVal parentNode As IExplorerNode)
    
                ' Call the custom SharePoint command to get items from the Web Part gallery.
                Dim webParts = parentNode.Context.SharePointConnection.ExecuteCommand(Of WebPartNodeInfo())( _
                    WebPartCommandIds.GetWebParts)
                If webParts IsNot Nothing Then
                    For Each webPart As WebPartNodeInfo In webParts
    
                        ' Create a new annotation object to store the current Web Part item with the new node.
                        Dim annotations = New Dictionary(Of Object, Object)()
                        annotations.Add(GetType(WebPartNodeInfo), webPart)
    
                        ' Create the new node for the current Web Part item.
                        parentNode.ChildNodes.Add(WebPartNodeTypeProvider.WebPartNodeTypeId, _
                            webPart.Name, annotations)
                    Next
                End If
            End Sub
    
        End Class
    End Namespace
    
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    using Microsoft.VisualStudio.SharePoint.Explorer;
    
    namespace ServerExplorer.SharePointConnections.WebPartNode
    {
        // Enables Visual Studio to discover and load this extension.
        [Export(typeof(IExplorerNodeTypeExtension))]        
    
        // Indicates that this class extends SharePoint site nodes in Server Explorer.
        [ExplorerNodeType(ExplorerNodeTypes.SiteNode)]
    
        // Represents an extension of SharePoint site nodes in Server Explorer.
        internal class SiteNodeExtension : IExplorerNodeTypeExtension
        {
            public void Initialize(IExplorerNodeType nodeType)
            {
                // The NodeChildrenRequested event is raised when the user expands the
                // SharePoint site node in Server Explorer.
                nodeType.NodeChildrenRequested += NodeChildrenRequested;
            }
    
            // Creates the new Web Part Gallery node with the specified icon.
            private void NodeChildrenRequested(object sender, ExplorerNodeEventArgs e)
            {
                // The CreateWebPartNodes argument is a delegate that Visual Studio calls 
                // to create the child nodes under the Web Part Gallery node.
                e.Node.ChildNodes.AddFolder("Web Part Gallery",
                    Properties.Resources.WebPartsNode.ToBitmap(), CreateWebPartNodes);
            }
    
            // Creates all of the individual Web Part nodes under the new Web Part Gallery node.
            private void CreateWebPartNodes(IExplorerNode parentNode)
            {
                // Call the custom SharePoint command to get items from the Web Part gallery.
                var webParts = parentNode.Context.SharePointConnection.ExecuteCommand<WebPartNodeInfo[]>(
                    WebPartCommandIds.GetWebParts);
    
                if (webParts != null)
                {
                    foreach (WebPartNodeInfo webPart in webParts)
                    {
                        // Create a new annotation object to store the current Web Part item with the new node.
                        var annotations = new Dictionary<object, object>() 
                        { 
                            { typeof(WebPartNodeInfo), webPart } 
                        };
    
                        // Create the new node for the current Web Part item.
                        parentNode.ChildNodes.Add(WebPartNodeTypeProvider.WebPartNodeTypeId,
                            webPart.Name, annotations);
                    }
                }
            }
        }
    }
    

定义表示 Web 部件的节点类型

创建一个类,此类定义表示 Web 部件的新节点类型。Visual Studio 使用此新节点类型来显示节点下的子节点。Web 部件库。每个子节点表示 SharePoint 网站的 Web 部件。

若要定义新节点类型,此类应实现 IExplorerNodeTypeProvider 接口。每当需要在**“服务器资源管理器”**中定义新的节点类型时,就要实现此接口。

定义 Web 部件节点类型

  • 在 WebPartNodeExtension 项目中,打开 WebPartNodeTypeProvder 代码文件,然后将下面的代码粘贴到它。

    Imports System
    Imports System.Collections.Generic
    Imports System.Windows.Forms
    Imports System.ComponentModel.Composition
    Imports Microsoft.VisualStudio.SharePoint
    Imports Microsoft.VisualStudio.SharePoint.Explorer
    
    Namespace ServerExplorer.SharePointConnections.WebPartNode
    
        ' Export attribute: Enables Visual Studio to discover and load this extension.
        ' ExplorerNodeType attribute: Specifies the ID for this new node type.
        ' WebPartNodeTypeProvider class: Defines a new node type that represents a Web Part on a SharePoint site.
        <Export(GetType(IExplorerNodeTypeProvider))> _
        <ExplorerNodeType(WebPartNodeTypeProvider.WebPartNodeTypeId)> _
        Friend Class WebPartNodeTypeProvider
            Implements IExplorerNodeTypeProvider
    
            Friend Const WebPartNodeTypeId As String = "Contoso.WebPart"
    
            Private Sub InitializeType(ByVal typeDefinition As IExplorerNodeTypeDefinition) _
                Implements IExplorerNodeTypeProvider.InitializeType
    
                typeDefinition.DefaultIcon = My.Resources.WebPart.ToBitmap()
                typeDefinition.IsAlwaysLeaf = True
                AddHandler typeDefinition.NodePropertiesRequested, AddressOf NodePropertiesRequested
            End Sub
    
            ' Retrieves properties that are displayed in the Properties window when
            ' a Web Part node is selected.
            Private Sub NodePropertiesRequested(ByVal Sernder As Object, _
                ByVal e As ExplorerNodePropertiesRequestedEventArgs)
    
                Dim nodeInfo = e.Node.Annotations.GetValue(Of WebPartNodeInfo)()
    
                ' Call the custom SharePoint command to get the Web Part properties.
                Dim properties As Dictionary(Of String, String) = _
                    e.Node.Context.SharePointConnection.ExecuteCommand( _
                    Of WebPartNodeInfo, Dictionary(Of String, String))(
                    WebPartCommandIds.GetWebPartProperties, nodeInfo)
                Dim propertySource As Object = e.Node.Context.CreatePropertySourceObject(properties)
                e.PropertySources.Add(propertySource)
            End Sub
    
        End Class
    End Namespace
    
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.ComponentModel.Composition;
    using Microsoft.VisualStudio.SharePoint;
    using Microsoft.VisualStudio.SharePoint.Explorer;
    
    namespace ServerExplorer.SharePointConnections.WebPartNode
    {
        // Enables Visual Studio to discover and load this extension.
        [Export(typeof(IExplorerNodeTypeProvider))]
    
        // Specifies the ID for this new node type.
        [ExplorerNodeType(WebPartNodeTypeProvider.WebPartNodeTypeId)]
    
        // Defines a new node type that represents a Web Part on a SharePoint site.
        internal class WebPartNodeTypeProvider : IExplorerNodeTypeProvider
        {
            internal const string WebPartNodeTypeId = "Contoso.WebPart";
    
            public void InitializeType(IExplorerNodeTypeDefinition typeDefinition)
            {
                typeDefinition.DefaultIcon = Properties.Resources.WebPart.ToBitmap();
                typeDefinition.IsAlwaysLeaf = true;
                typeDefinition.NodePropertiesRequested += NodePropertiesRequested;
            }
    
            // Retrieves properties that are displayed in the Properties window when
            // a Web Part node is selected.
            private void NodePropertiesRequested(object sender,
                ExplorerNodePropertiesRequestedEventArgs e)
            {
                var webPartNodeInfo = e.Node.Annotations.GetValue<WebPartNodeInfo>();
    
                // Call the custom SharePoint command to get the Web Part properties.
                Dictionary<string, string> properties =
                    e.Node.Context.SharePointConnection.ExecuteCommand<
                    WebPartNodeInfo, Dictionary<string, string>>(
                    WebPartCommandIds.GetWebPartProperties, webPartNodeInfo);
    
                object propertySource = e.Node.Context.CreatePropertySourceObject(properties);
                e.PropertySources.Add(propertySource);
            }
        }
    }
    

定义 Web 部件数据类

定义一个类,此类包含有关 SharePoint 网站上的单个 Web 部件的数据。在本演练中,您将创建检索有关每个 Web 部件的数据在网站中将数据分配给此选件类实例的自定义 SharePoint 命令。

定义 Web 部件数据类

  • 在 WebPartNodeExtension 项目中,打开 WebPartNodeInfo 代码文件,然后将下面的代码粘贴到它。

    Imports System
    
    Namespace ServerExplorer.SharePointConnections.WebPartNode
    
        ' Contains basic data about a single Web Part on the SharePoint site. This class is 
        ' serializable so that instances of it can be sent between the WebPartNode and 
        ' WebPartCommands assemblies.
        <Serializable()> _
        Public Class WebPartNodeInfo
    
            Private siteIdValue As Guid
            Public Property SiteId As Guid
                Get
                    Return siteIdValue
                End Get
                Set(ByVal value As Guid)
                    siteIdValue = value
                End Set
            End Property
    
            Private idValue As Integer
            Public Property Id As Integer
                Get
                    Return idValue
                End Get
                Set(ByVal value As Integer)
                    idValue = value
                End Set
            End Property
    
            Private uniqueIdValue As Guid
            Public Property UniqueId As Guid
                Get
                    Return uniqueIdValue
                End Get
                Set(ByVal value As Guid)
                    uniqueIdValue = value
                End Set
            End Property
    
            Private nameValue As String
            Public Property Name As String
                Get
                    Return nameValue
                End Get
                Set(ByVal value As String)
                    nameValue = value
                End Set
            End Property
    
            Private imageUrlValue As String
            Public Property ImageUrl As String
                Get
                    Return imageUrlValue
                End Get
                Set(ByVal value As String)
                    imageUrlValue = value
                End Set
            End Property
    
        End Class
    End Namespace
    
    using System;
    
    namespace ServerExplorer.SharePointConnections.WebPartNode
    {
        // Contains basic data about a single Web Part on the SharePoint site. This class is 
        // serializable so that instances of it can be sent between the WebPartNode and 
        // WebPartCommands assemblies.
        [Serializable]
        public class WebPartNodeInfo
        {
            public Guid SiteId { get; set; }
            public int Id { get; set; }
            public Guid UniqueId { get; set; }
            public string Name { get; set; }
            public string ImageUrl { get; set; }
        }
    }
    

定义 SharePoint 命令的 ID

定义若干个标识自定义 SharePoint 命令的字符串。您将在本演练后面的部分中实现这些命令。

定义命令 ID

  • 在 WebPartNodeExtension 项目中,打开 WebPartCommandIds 代码文件,然后将下面的代码粘贴到它。

    Namespace ServerExplorer.SharePointConnections.WebPartNode
    
        Public Class WebPartCommandIds
            Public Const GetWebParts As String = "WebPart.GetWebParts"
            Public Const GetWebPartProperties As String = "WebPart.GetProperties"
        End Class
    
    End Namespace
    
    namespace ServerExplorer.SharePointConnections.WebPartNode
    {
        public static class WebPartCommandIds
        {
            public const string GetWebParts = "WebPart.GetWebParts";
            public const string GetWebPartProperties = "WebPart.GetProperties";
        }
    }
    

创建自定义 SharePoint 命令

创建调入 SharePoint 服务器对象模型的可以检索有关 Web 部件的数据在 SharePoint 网站的自定义命令。每条命令均是应用了 SharePointCommandAttribute 的方法。

定义 SharePoint 命令

  • 在 WebPartCommands 项目中,打开 WebPartCommands 代码文件,然后将下面的代码粘贴到它。

    Imports System.Collections.Generic
    Imports Microsoft.SharePoint
    Imports Microsoft.VisualStudio.SharePoint.Commands
    
    Namespace ServerExplorer.SharePointConnections.WebPartNode
    
        Friend Class WebPartsCommands
    
            ' Gets data for each Web Part on the SharePoint site, and returns an array of 
            ' serializable objects that contain the data.
            <SharePointCommand(WebPartCommandIds.GetWebParts)> _
            Private Shared Function GetWebParts(ByVal context As ISharePointCommandContext) As WebPartNodeInfo()
    
                Dim nodeInfos = New List(Of WebPartNodeInfo)()
                Dim webParts As SPListItemCollection = context.Site.GetCatalog( _
                    SPListTemplateType.WebPartCatalog).Items
    
                For Each webPart As SPListItem In webParts
                    Dim nodeInfo As WebPartNodeInfo = New WebPartNodeInfo()
                    With nodeInfo
                        .Id = webPart.ID
                        .SiteId = webPart.ParentList.ParentWeb.ID
                        .Name = webPart.Title
                        .UniqueId = webPart.UniqueId
                        .ImageUrl = webPart.ParentList.ImageUrl
                    End With
                    nodeInfos.Add(nodeInfo)
                Next
                Return nodeInfos.ToArray()
            End Function
    
            ' Gets additional property data for a specific Web Part.
            <SharePointCommand(WebPartCommandIds.GetWebPartProperties)> _
            Private Shared Function GetWebPartProperties(ByVal context As ISharePointCommandContext, _
                ByVal webPartNodeInfo As WebPartNodeInfo) As Dictionary(Of String, String)
    
                Dim webParts As SPList = context.Site.GetCatalog(SPListTemplateType.WebPartCatalog)
                Dim webPart As SPListItem = webParts.Items(webPartNodeInfo.UniqueId)
                Return SharePointCommandServices.GetProperties(webPart)
            End Function
        End Class
    End Namespace
    
    using System.Collections.Generic;
    using Microsoft.SharePoint;
    using Microsoft.VisualStudio.SharePoint.Commands;
    
    namespace ServerExplorer.SharePointConnections.WebPartNode
    {
        internal class WebPartsCommands
        {
            // Gets data for each Web Part on the SharePoint site, and returns an array of 
            // serializable objects that contain the data.
            [SharePointCommand(WebPartCommandIds.GetWebParts)]
            private static WebPartNodeInfo[] GetWebParts(ISharePointCommandContext context)
            {
                var nodeInfos = new List<WebPartNodeInfo>();
                SPListItemCollection webParts = context.Site.GetCatalog(
                    SPListTemplateType.WebPartCatalog).Items;
    
                foreach (SPListItem webPart in webParts)
                {
                    WebPartNodeInfo nodeInfo = new WebPartNodeInfo
                    {
                        Id = webPart.ID,
                        SiteId = webPart.ParentList.ParentWeb.ID,
                        Name = webPart.Title,
                        UniqueId = webPart.UniqueId,
                        ImageUrl = webPart.ParentList.ImageUrl
                    };
                    nodeInfos.Add(nodeInfo);
                }
    
                return nodeInfos.ToArray();
            }
    
            // Gets additional property data for a specific Web Part.
            [SharePointCommand(WebPartCommandIds.GetWebPartProperties)]
            private static Dictionary<string, string> GetWebPartProperties(ISharePointCommandContext context, 
                WebPartNodeInfo nodeInfo)
            {
                SPList webParts = context.Site.GetCatalog(SPListTemplateType.WebPartCatalog);
                SPListItem webPart = webParts.Items[nodeInfo.UniqueId];
    
                return SharePointCommandServices.GetProperties(webPart);
            }
        }
    }
    

检查点

演练进行到此时,**“Web 部件库”**节点和 SharePoint 命令的所有代码现在都位于项目中。生成解决方案以确保项目在编译时不会出错。

生成解决方案

  • 在菜单栏上,依次选择 Build生成解决方案

    警告说明警告

    此时,因为 VSIX 清单文件没有作者的,一个值 WebPartNode 项目可能出现生成错误。当您将在后面的步骤值时,此错误将消失。

创建 VSIX 包以部署扩展

若要部署扩展,请使用解决方案中的 VSIX 项目来创建 VSIX 包。首先,通过修改 VSIX 项目中的 source.extension.vsixmanifest 文件来配置 VSIX 包。然后,通过生成解决方案来创建 VSIX 包。

配置 VSIX 包

  1. 解决方案资源管理器,在 WebPartNode 项目中,打开在清单编辑器中 source.extension.vsixmanifest 文件。

    source.extension.vsixmanifest 文件是所有 VSIX 包必需的 extension.vsixmanifest 文件的基础。有关此文件的更多信息,请参见VSIX 扩展架构参考

  2. 产品名称 框中,输入 Web 部件服务器资源管理器的库节点。

  3. 作者 框中,输入 Contoso。

  4. 声明框中,输入添加自定义 Web 部件库节点到在服务器资源管理器中的“SharePoint 连接”节点。此扩展使用自定义 SharePoint 命令调入服务器对象模型。”。

  5. 选择编辑器的 资产 选项卡,然后选择 新建 按钮。

    添加新资产 出现对话框。

  6. 类型 列表中,选择 Microsoft.VisualStudio.MefComponent

    说明说明

    此值对应于 extension.vsixmanifest 文件中的 MefComponent 元素。此元素指定 VSIX 包中的扩展程序集的名称。有关更多信息,请参见MEFComponent Element

  7. 列表中,选择 当前解决方案中的项目

  8. 项目 列表中,选择 WebPartNodeExtension 然后选择 确定 按钮。

  9. 在清单编辑器,请选择 新建 按钮。

    添加新资产 出现对话框。

  10. 类型 框中,输入 SharePoint.Commands.v4。

    说明说明

    此元素指定要包括在 Visual Studio 扩展中的自定义扩展。有关更多信息,请参见属性元素 (VSX 架构)

  11. 列表中,选择 当前解决方案中的项目 列表项。

  12. 项目 列表中,选择 WebPartCommands,然后选择 确定 按钮。

  13. 在菜单栏上,依次选择 Build生成解决方案,然后确保解决方案已生成且未发生错误。

  14. 确保 WebPartNode 项目的生成输出文件夹现在包含 WebPartNode.vsix 文件。

    默认情况下,生成输出文件夹为包含项目文件的文件夹下的 ..\bin\Debug 文件夹。

测试扩展

您现在可以测试在 服务器资源管理器的新 Web 部件库 节点。首先,在 Visual Studio 的实验实例中开始调试扩展。然后,在 Visual Studio 的实验实例中使用新的**“Web 部件”**节点。

开始调试扩展

  1. 重新启动使用管理凭据的 Visual Studio,然后打开 WebPartNode 解决方案。

  2. 在 WebPartNodeExtension 项目中,打开 SiteNodeExtension 代码文件,然后将断点添加到第一个代码行中 NodeChildrenRequested 和 CreateWebPartNodes 方法的。

  3. 选择 F5 键开始调试。

    Visual Studio 将扩展安装到 %UserProfile% \ AppData \ local \ Microsoft \ VisualStudio \ 11.0Exp \ extensions \ Contoso \ Web 部件库节点扩展服务器资源管理器\ 1.0 中并启动 Visual Studio 的实验实例。您将在此 Visual Studio 实例中测试项目项。

测试扩展

  1. 在 Visual Studio的实验实例中,在菜单栏上,依次选择 视图服务器资源管理器

  2. 请执行以下步骤,如果要用于测试的 SharePoint 网站未出现在 服务器资源管理器SharePoint 连接 节点中:

    1. 服务器资源管理器,打开 SharePoint 连接的快捷菜单,然后选择 添加连接

    2. 添加 SharePoint 连接 对话框中,输入要连接到的 SharePoint 网站的 URL,然后选择 确定 按钮。

      若要指定开发计算机上的 SharePoint 网站,输入 https://localhost。

  3. 展开显示网站的 URL 的节点) (,然后展开子网站节点 (例如,团队网站)。

  4. 验证在 Visual Studio 另一个实例中的代码在 NodeChildrenRequested 方法设置的断点处停止,然后选择 F5 继续调试项目。

  5. 在 Visual Studio 的实验实例中,验证名为 Web 部件库 的新节点是否显示在首要网站节点下,再展开 Web 部件库 节点。

  6. 验证另一个 Visual Studio 实例的代码在 CreateWebPartNodes 方法设置的断点处停止,然后选择 F5 键继续调试项目。

  7. 在 Visual Studio 的实验实例中,验证已连接网站上的所有 Web 部件以显示在 服务器资源管理器Web 部件库 节点下。

  8. 服务器资源管理器,打开之一的快捷菜单 Web 部件,然后选择 属性

  9. 在调试 Visual Studio 实例中,验证有关 Web 部件的详细信息显示在 属性 窗口。

从 Visual Studio 中卸载扩展

测试完扩展后,请从 Visual Studio 卸载扩展。

卸载扩展

  1. 在 Visual Studio的实验实例中,在菜单栏上,依次选择 工具扩展和更新

    扩展和更新 对话框打开。

  2. 在扩展列表中,选择 Web 部件库服务器资源管理器的节点扩展,然后选择 卸载 按钮。

  3. 在出现的对话框中,选择 按钮以确认您要卸载该扩展,然后选择 立即重新启动 按钮来卸载。

  4. 关闭 Visual Studio 的实验实例 (和 WebPartNode 解决方案处于打开状态的实例) 两个实例 Visual Studio。

请参见

任务

演练:在服务器资源管理器扩展中调入 SharePoint 客户端对象模型

创建图标或其他图像(图标的图像编辑器)

参考

图标的图像编辑器

其他资源

扩展服务器资源管理器中的“SharePoint 连接”节点