HOW TO:在伺服器總管中擴充 SharePoint 節點
您可以在 [伺服器總管] 的 [SharePoint 連接] 節點下方擴充節點。 當您希望將新的子節點、捷徑功能表或屬性加入至現有節點時,這樣做將會很有用。 如需詳細資訊,請參閱在伺服器總管中擴充 SharePoint 連線節點。
若要在 [伺服器總管] 中擴充 SharePoint 節點
建立類別庫專案。
加入下列組件的參考:
Microsoft.VisualStudio.SharePoint
Microsoft.VisualStudio.SharePoint.Explorer.Extensions
System.ComponentModel.Composition
建立實作 IExplorerNodeTypeExtension 介面的類別。
請將 System.ComponentModel.Composition.ExportAttribute 屬性加入至類別。 此屬性可讓 Visual Studio 探索並載入 IExplorerNodeTypeExtension 實作。 將 IExplorerNodeTypeExtension 型別傳遞至屬性建構函式。
請將 ExplorerNodeTypeAttribute 屬性加入至類別。 此屬性會指定要擴充之節點型別的字串識別碼。
若要指定 Visual Studio 所提供的內建節點型別,請將下列其中一個列舉值傳遞至屬性建構函式:
ExplorerNodeTypes:使用這些值以指定網站連接節點 (即顯示網站 URL 的節點)、網站節點或 [伺服器總管] 中的所有其他父節點。
ExtensionNodeTypes:使用這些值以指定其中一個內建節點,這些節點表示 SharePoint 網站上的個別元件,例如表示清單、欄位或內容類型的節點。
在 IExplorerNodeTypeExtension.Initialize 方法的實作中,使用 nodeType 參數的成員,將功能加入至節點。 這個參數是 IExplorerNodeType 物件,可用來存取 IExplorerNodeEvents 介面中定義的事件。 例如,您可以處理下列事件:
IExplorerNodeEvents.NodeChildrenRequested:處理此事件,以將新的子節點加入至節點。 如需詳細資訊,請參閱 HOW TO:在伺服器總管中新增自訂 SharePoint 節點。
IExplorerNodeEvents.NodeMenuItemsRequested:處理此事件,以將自訂捷徑功能表項目加入至節點。
IExplorerNodeEvents.NodePropertiesRequested:處理此事件,以將自訂屬性加入至節點。 當選取節點時,屬性會顯示在 [屬性] 視窗中。
範例
下列程式碼範例示範如何建立兩個不同類型的節點擴充功能:
可將內容功能表項目加入至 SharePoint 網站節點的擴充功能。 當您按一下功能表項目時,它會顯示所按節點的名稱。
可將 [ContosoExampleProperty] 自訂屬性加入至每個代表 [Body] 欄位之節點的擴充功能。
Imports System.ComponentModel
Imports System.ComponentModel.Composition
Imports System.Windows.Forms
Imports Microsoft.VisualStudio.SharePoint
Imports Microsoft.VisualStudio.SharePoint.Explorer
Imports Microsoft.VisualStudio.SharePoint.Explorer.Extensions
Namespace Contoso.ServerExplorerExtension
<Export(GetType(IExplorerNodeTypeExtension))> _
<ExplorerNodeType(ExplorerNodeTypes.SiteNode)> _
Friend Class SiteNodeExtensionWithContextMenu
Implements IExplorerNodeTypeExtension
Private Sub Initialize(ByVal nodeType As IExplorerNodeType) _
Implements IExplorerNodeTypeExtension.Initialize
AddHandler nodeType.NodeMenuItemsRequested, AddressOf NodeMenuItemsRequested
End Sub
Private Sub NodeMenuItemsRequested(ByVal Sender As Object, ByVal e As ExplorerNodeMenuItemsRequestedEventArgs)
Dim menuItem = e.MenuItems.Add("Display Message")
AddHandler menuItem.Click, AddressOf MenuItemClick
End Sub
Private Sub MenuItemClick(ByVal Sender As Object, ByVal e As MenuItemEventArgs)
Dim node As IExplorerNode = CType(e.Owner, IExplorerNode)
MessageBox.Show(String.Format("Clicked the menu item for the '{0}' node.", node.Text))
End Sub
End Class
<Export(GetType(IExplorerNodeTypeExtension))> _
<ExplorerNodeType(ExtensionNodeTypes.FieldNode)> _
Friend Class FieldNodeExtensionWithProperty
Implements IExplorerNodeTypeExtension
Private Sub Initialize(ByVal nodeType As IExplorerNodeType) _
Implements IExplorerNodeTypeExtension.Initialize
AddHandler nodeType.NodePropertiesRequested, AddressOf NodePropertiesRequested
End Sub
Private Sub NodePropertiesRequested(ByVal Sender As Object, ByVal e As ExplorerNodePropertiesRequestedEventArgs)
Dim propertyObject As ExampleProperty = Nothing
' Only add the property to "Body" fields.
If e.Node.Text = "Body" Then
' If the properties object already exists for this node, get it from the node's annotations.
If False = e.Node.Annotations.TryGetValue(propertyObject) Then
' Otherwise, create a new properties object and add it to the annotations.
propertyObject = New ExampleProperty(e.Node)
e.Node.Annotations.Add(propertyObject)
End If
e.PropertySources.Add(propertyObject)
End If
End Sub
End Class
Friend Class ExampleProperty
Private node As IExplorerNode
Private Const propertyId As String = "Contoso.CustomActionTestProperty"
Private Const propertyDefaultValue As String = "This is a test value."
Friend Sub New(ByVal node As IExplorerNode)
Me.node = node
End Sub
' Gets or sets a simple string property.
<DisplayName("ContosoExampleProperty")> _
<DescriptionAttribute("This is an example property for field nodes.")> _
<DefaultValue(propertyDefaultValue)> _
Public Property TestProperty As String
Get
Dim propertyValue As String = Nothing
' Get the current property value if it already exists; otherwise, return a default value.
If False = node.Annotations.TryGetValue(propertyId, propertyValue) Then
propertyValue = propertyDefaultValue
End If
Return propertyValue
End Get
Set(ByVal value As String)
If value <> propertyDefaultValue Then
' Store the property value in the Annotations property of the node.
' Data in the Annotations property does not persist when Visual Studio exits.
node.Annotations(propertyId) = value
Else
' Do not save the default value.
node.Annotations.Values.Remove(propertyId)
End If
End Set
End Property
End Class
End Namespace
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Windows.Forms;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Explorer;
using Microsoft.VisualStudio.SharePoint.Explorer.Extensions;
namespace Contoso.ServerExplorerExtension
{
[Export(typeof(IExplorerNodeTypeExtension))]
[ExplorerNodeType(ExplorerNodeTypes.SiteNode)]
internal class SiteNodeExtensionWithContextMenu : IExplorerNodeTypeExtension
{
public void Initialize(IExplorerNodeType nodeType)
{
nodeType.NodeMenuItemsRequested += nodeType_NodeMenuItemsRequested;
}
void nodeType_NodeMenuItemsRequested(object sender, ExplorerNodeMenuItemsRequestedEventArgs e)
{
IMenuItem menuItem = e.MenuItems.Add("Display Message");
menuItem.Click += menuItem_Click;
}
void menuItem_Click(object sender, MenuItemEventArgs e)
{
IExplorerNode node = (IExplorerNode)e.Owner;
MessageBox.Show(string.Format("Clicked the menu item for the '{0}' node.", node.Text));
}
}
[Export(typeof(IExplorerNodeTypeExtension))]
[ExplorerNodeType(ExtensionNodeTypes.FieldNode)]
internal class FieldNodeExtensionWithProperty : IExplorerNodeTypeExtension
{
public void Initialize(IExplorerNodeType nodeType)
{
nodeType.NodePropertiesRequested += nodeType_NodePropertiesRequested;
}
void nodeType_NodePropertiesRequested(object sender, ExplorerNodePropertiesRequestedEventArgs e)
{
// Only add the property to "Body" fields.
if (e.Node.Text == "Body")
{
ExampleProperty propertyObject;
// If the properties object already exists for this node, get it from the node's annotations.
if (!e.Node.Annotations.TryGetValue(out propertyObject))
{
// Otherwise, create a new properties object and add it to the annotations.
propertyObject = new ExampleProperty(e.Node);
e.Node.Annotations.Add(propertyObject);
}
e.PropertySources.Add(propertyObject);
}
}
}
internal class ExampleProperty
{
private IExplorerNode node;
private const string propertyId = "Contoso.ExampleProperty";
private const string propertyDefaultValue = "This is an example property.";
internal ExampleProperty(IExplorerNode node)
{
this.node = node;
}
// Gets or sets a simple string property.
[DisplayName("ContosoExampleProperty")]
[DescriptionAttribute("This is an example property for field nodes.")]
[DefaultValue(propertyDefaultValue)]
public string TestProperty
{
get
{
string propertyValue;
// Get the current property value if it already exists; otherwise, return a default value.
if (!node.Annotations.TryGetValue(propertyId, out propertyValue))
{
propertyValue = propertyDefaultValue;
}
return propertyValue;
}
set
{
if (value != propertyDefaultValue)
{
// Store the property value in the Annotations property of the node.
// Data in the Annotations property does not persist when Visual Studio exits.
node.Annotations[propertyId] = value;
}
else
{
// Do not save the default value.
node.Annotations.Remove(propertyId);
}
}
}
}
}
此擴充功能會將可編輯字串屬性加入至節點。 您也可以建立自訂屬性,顯示來自 SharePoint 伺服器的唯讀資料。 如需示範這個作法的範例,請參閱逐步解說:擴充伺服器總管以顯示 Web 組件。
編譯程式碼
這個範例需要參考下列組件:
Microsoft.VisualStudio.SharePoint
Microsoft.VisualStudio.SharePoint.Explorer.Extensions
System.ComponentModel.Composition
System.Windows.Forms
部署擴充功能
若要部署 [伺服器總管] 擴充功能,請針對組件以及要與擴充功能一起散發的任何其他檔案建立 Visual Studio 擴充功能 (VSIX) 套件。 如需詳細資訊,請參閱部署 Visual Studio 中 SharePoint 工具的擴充功能。
請參閱
工作
HOW TO:在伺服器總管中新增自訂 SharePoint 節點