共用方式為


HOW TO:在伺服器總管中新增自訂 SharePoint 節點

您可以在 [伺服器總管] 的 [SharePoint 連接] 節點下方加入自訂節點。 當您要顯示不是 [伺服器總管] 中預設顯示的其他 SharePoint 元件時,這會相當有用。 如需詳細資訊,請參閱在伺服器總管中擴充 SharePoint 連線節點

若要加入自訂節點,請先建立定義新節點的類別。 然後建立擴充功能,以加入節點做為現有節點的子節點。

若要定義新節點

  1. 建立類別庫專案。

  2. 加入下列組件的參考:

    • Microsoft.VisualStudio.SharePoint

    • Microsoft.VisualStudio.SharePoint.Explorer.Extensions

    • System.ComponentModel.Composition

    • System.Drawing

  3. 建立實作 IExplorerNodeTypeProvider 介面的類別。

  4. 將下列屬性加入到類別:

  5. 在您的 IExplorerNodeTypeProvider.InitializeType 方法實作中,使用 typeDefinition 參數的成員來設定新節點的行為。 這個參數是 IExplorerNodeTypeDefinition 物件,可用來存取 IExplorerNodeEvents 介面中定義的事件。

    下列程式碼範例將示範如何定義新的節點。 此範例假設您的專案包含名為 CustomChildNodeIcon 的圖示做為內嵌資源。

    <Export(GetType(IExplorerNodeTypeProvider))> _
    <ExplorerNodeType(ExampleNodeTypeProvider.NodeTypeId)> _
    Friend Class ExampleNodeTypeProvider
        Implements IExplorerNodeTypeProvider
        Friend Const NodeTypeId As String = "Contoso.ServerExplorerNodeExample"
    
        Private Sub InitializeType(ByVal typeDefinition As IExplorerNodeTypeDefinition) _
            Implements IExplorerNodeTypeProvider.InitializeType
            typeDefinition.DefaultIcon = _
                My.Resources.CustomChildNodeIcon.ToBitmap()
            typeDefinition.IsAlwaysLeaf = True
        End Sub
    End Class
    
    [Export(typeof(IExplorerNodeTypeProvider))]
    [ExplorerNodeType(ExampleNodeTypeProvider.NodeTypeId)]
    internal class ExampleNodeTypeProvider : IExplorerNodeTypeProvider
    {
        internal const string NodeTypeId = "Contoso.ServerExplorerNodeExample";
    
        public void InitializeType(IExplorerNodeTypeDefinition typeDefinition)
        {
            typeDefinition.DefaultIcon =
                Properties.Resources.CustomChildNodeIcon.ToBitmap();
            typeDefinition.IsAlwaysLeaf = true;
        }
    }
    

若要加入新節點做為現有節點的子節點

  1. 在節點定義所在的相同專案中,建立實作 IExplorerNodeTypeExtension 介面的類別。

  2. 請將 System.ComponentModel.Composition.ExportAttribute 屬性加入至類別。 此屬性可讓 Visual Studio 探索並載入 IExplorerNodeTypeExtension 實作。 將 IExplorerNodeTypeExtension 型別傳遞至屬性建構函式。

  3. 請將 ExplorerNodeTypeAttribute 屬性加入至類別。 在節點擴充功能中,此屬性會指定要擴充之節點型別的字串識別項。

    若要指定 Visual Studio 所提供的內建節點型別,請將下列其中一個列舉值傳遞至屬性建構函式:

    • ExplorerNodeTypes:使用這些值以指定網站連接節點 (即顯示網站 URL 的節點)、網站節點或 [伺服器總管] 中的所有其他父節點。

    • ExtensionNodeTypes:使用這些值以指定其中一個內建節點,這些節點表示 SharePoint 網站上的個別元件,例如表示清單、欄位或內容類型的節點。

  4. Initialize 方法的實作中,處理 IExplorerNodeType 參數的 NodeChildrenRequested 事件。

  5. NodeChildrenRequested 事件處理常式中,將新節點加入至事件引數參數所公開之 Node 物件的子節點集合。

    下列程式碼範例示範如何在 [伺服器總管] 中加入新節點做為 SharePoint 網站節點的子節點。

    <Export(GetType(IExplorerNodeTypeExtension))> _
    <ExplorerNodeType(ExplorerNodeTypes.SiteNode)> _
    Friend Class SiteNodeExtension
        Implements IExplorerNodeTypeExtension
    
        Private Sub Initialize(ByVal nodeType As IExplorerNodeType) _
            Implements IExplorerNodeTypeExtension.Initialize
            AddHandler nodeType.NodeChildrenRequested, AddressOf NodeChildrenRequested
        End Sub
    
        Private Sub NodeChildrenRequested(ByVal Sender As Object, ByVal e As ExplorerNodeEventArgs)
            e.Node.ChildNodes.Add(ExampleNodeTypeProvider.NodeTypeId, _
                "Custom Node", Nothing)
        End Sub
    End Class
    
    [Export(typeof(IExplorerNodeTypeExtension))]
    [ExplorerNodeType(ExplorerNodeTypes.SiteNode)]
    internal class SiteNodeExtension : IExplorerNodeTypeExtension
    {
        public void Initialize(IExplorerNodeType nodeType)
        {
            nodeType.NodeChildrenRequested += NodeChildrenRequested;
        }
    
        private void NodeChildrenRequested(object sender, ExplorerNodeEventArgs e)
        {
            e.Node.ChildNodes.Add(ExampleNodeTypeProvider.NodeTypeId,
                "Custom Node", null);
        }
    }
    

完整範例

下列程式碼範例提供完整的程式碼,首先定義簡單的節點,再將它加入 [伺服器總管] 中做為 SharePoint 網站節點的子節點。

Imports System.ComponentModel.Composition
Imports Microsoft.VisualStudio.SharePoint
Imports Microsoft.VisualStudio.SharePoint.Explorer

Namespace Contoso.ServerExplorerExtension

    <Export(GetType(IExplorerNodeTypeProvider))> _
    <ExplorerNodeType(ExampleNodeTypeProvider.NodeTypeId)> _
    Friend Class ExampleNodeTypeProvider
        Implements IExplorerNodeTypeProvider
        Friend Const NodeTypeId As String = "Contoso.ServerExplorerNodeExample"

        Private Sub InitializeType(ByVal typeDefinition As IExplorerNodeTypeDefinition) _
            Implements IExplorerNodeTypeProvider.InitializeType
            typeDefinition.DefaultIcon = _
                My.Resources.CustomChildNodeIcon.ToBitmap()
            typeDefinition.IsAlwaysLeaf = True
        End Sub
    End Class

    <Export(GetType(IExplorerNodeTypeExtension))> _
    <ExplorerNodeType(ExplorerNodeTypes.SiteNode)> _
    Friend Class SiteNodeExtension
        Implements IExplorerNodeTypeExtension

        Private Sub Initialize(ByVal nodeType As IExplorerNodeType) _
            Implements IExplorerNodeTypeExtension.Initialize
            AddHandler nodeType.NodeChildrenRequested, AddressOf NodeChildrenRequested
        End Sub

        Private Sub NodeChildrenRequested(ByVal Sender As Object, ByVal e As ExplorerNodeEventArgs)
            e.Node.ChildNodes.Add(ExampleNodeTypeProvider.NodeTypeId, _
                "Custom Node", Nothing)
        End Sub
    End Class
End Namespace
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Explorer;

namespace Contoso.ServerExplorerExtension
{
    [Export(typeof(IExplorerNodeTypeProvider))]
    [ExplorerNodeType(ExampleNodeTypeProvider.NodeTypeId)]
    internal class ExampleNodeTypeProvider : IExplorerNodeTypeProvider
    {
        internal const string NodeTypeId = "Contoso.ServerExplorerNodeExample";

        public void InitializeType(IExplorerNodeTypeDefinition typeDefinition)
        {
            typeDefinition.DefaultIcon =
                Properties.Resources.CustomChildNodeIcon.ToBitmap();
            typeDefinition.IsAlwaysLeaf = true;
        }
    }

    [Export(typeof(IExplorerNodeTypeExtension))]
    [ExplorerNodeType(ExplorerNodeTypes.SiteNode)]
    internal class SiteNodeExtension : IExplorerNodeTypeExtension
    {
        public void Initialize(IExplorerNodeType nodeType)
        {
            nodeType.NodeChildrenRequested += NodeChildrenRequested;
        }

        private void NodeChildrenRequested(object sender, ExplorerNodeEventArgs e)
        {
            e.Node.ChildNodes.Add(ExampleNodeTypeProvider.NodeTypeId,
                "Custom Node", null);
        }
    }
}

編譯程式碼

此範例假設您的專案包含名為 CustomChildNodeIcon 的圖示做為內嵌資源。 此外,這個範例還需要參考下列組件:

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

  • System.Drawing

部署擴充功能

若要部署 [伺服器總管] 擴充功能,請針對組件以及要與擴充功能一起散發的任何其他檔案建立 Visual Studio 擴充功能 (VSIX) 套件。 如需詳細資訊,請參閱部署 Visual Studio 中 SharePoint 工具的擴充功能

請參閱

工作

逐步解說:擴充伺服器總管以顯示 Web 組件

其他資源

在伺服器總管中擴充 SharePoint 連線節點

HOW TO:在伺服器總管中擴充 SharePoint 節點