HOW TO:取得伺服器總管的內建 SharePoint 節點資料
針對 [伺服器總管] 的每一個內建 SharePoint 節點,您都可以取得節點所代表之基礎 SharePoint 元件的資料。 如需詳細資訊,請參閱在伺服器總管中擴充 SharePoint 連線節點。
範例
下列程式碼範例示範如何取得 [伺服器總管] 中清單節點所代表之基礎 SharePoint 清單的資料。 根據預設,清單節點具有一個 [在瀏覽器中檢視] 內容功能表項目,您可以按一下該項目來開啟 Web 瀏覽器中的清單。 此範例藉由加入會在 Visual Studio 中直接開啟清單的 [在 Visual Studio 中檢視] 內容功能表項目,來擴充清單節點。 程式碼會存取節點的清單資料,以取得 Visual Studio 中所開啟清單的 URL。
Imports System.ComponentModel.Composition
Imports Microsoft.VisualStudio.SharePoint
Imports Microsoft.VisualStudio.SharePoint.Explorer
Imports Microsoft.VisualStudio.SharePoint.Explorer.Extensions
Namespace Contoso.ServerExplorerExtension
<Export(GetType(IExplorerNodeTypeExtension))> _
<ExplorerNodeType(ExtensionNodeTypes.ListNode)> _
Friend Class ListNodeExtension
Implements IExplorerNodeTypeExtension
Private projectService As ISharePointProjectService
Private dteObject As EnvDTE.DTE
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("View in Visual Studio")
AddHandler menuItem.Click, AddressOf MenuItemClick
End Sub
Private Sub MenuItemClick(ByVal Sender As Object, ByVal e As MenuItemEventArgs)
' Get the data for the list node.
Dim node As IExplorerNode = CType(e.Owner, IExplorerNode)
Dim nodeInfo As IListNodeInfo = node.Annotations.GetValue(Of IListNodeInfo)()
If dteObject Is Nothing Then
If projectService Is Nothing Then
projectService = CType(node.ServiceProvider.GetService(GetType(ISharePointProjectService)),
ISharePointProjectService)
End If
dteObject = CType(projectService.ServiceProvider.GetService(GetType(EnvDTE.DTE)), EnvDTE.DTE)
End If
dteObject.ItemOperations.Navigate(nodeInfo.DefaultViewUrl.ToString(),
EnvDTE.vsNavigateOptions.vsNavigateOptionsNewWindow)
End Sub
End Class
End Namespace
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Explorer;
using Microsoft.VisualStudio.SharePoint.Explorer.Extensions;
namespace Contoso.ServerExplorerExtension
{
[Export(typeof(IExplorerNodeTypeExtension))]
[ExplorerNodeType(ExtensionNodeTypes.ListNode)]
internal class ListNodeExtension : IExplorerNodeTypeExtension
{
private ISharePointProjectService projectService;
private EnvDTE.DTE dteObject;
public void Initialize(IExplorerNodeType nodeType)
{
nodeType.NodeMenuItemsRequested += nodeType_NodeMenuItemsRequested;
}
void nodeType_NodeMenuItemsRequested(object sender, ExplorerNodeMenuItemsRequestedEventArgs e)
{
IMenuItem menuItem = e.MenuItems.Add("View in Visual Studio");
menuItem.Click += menuItem_Click;
}
void menuItem_Click(object sender, MenuItemEventArgs e)
{
// Get the data for the list node.
IExplorerNode node = (IExplorerNode)e.Owner;
IListNodeInfo nodeInfo = node.Annotations.GetValue<IListNodeInfo>();
if (dteObject == null)
{
if (projectService == null)
{
projectService = (ISharePointProjectService)node.ServiceProvider.GetService(
typeof(ISharePointProjectService));
}
dteObject = (EnvDTE.DTE)projectService.ServiceProvider.GetService(typeof(EnvDTE.DTE));
}
dteObject.ItemOperations.Navigate(nodeInfo.DefaultViewUrl.ToString(),
EnvDTE.vsNavigateOptions.vsNavigateOptionsNewWindow);
}
}
}
此範例會使用 SharePoint 專案服務來取得用於開啟 Visual Studio 中清單的 DTE 物件。 如需 SharePoint 專案服務的詳細資訊,請參閱使用 SharePoint 專案服務。
如需建立 SharePoint 節點擴充功能之基本工作的詳細資訊,請參閱 HOW TO:在伺服器總管中擴充 SharePoint 節點。
編譯程式碼
這個範例需要參考下列組件:
EnvDTE
Microsoft.VisualStudio.SharePoint
Microsoft.VisualStudio.SharePoint.Explorer.Extensions
System.ComponentModel.Composition
部署擴充功能
若要部署 [伺服器總管] 擴充功能,請針對組件以及要與擴充功能一起散發的任何其他檔案建立 Visual Studio 擴充功能 (VSIX) 套件。 如需詳細資訊,請參閱部署 Visual Studio 中 SharePoint 工具的擴充功能。