Visual Studioでは、SharePointサイトのコンポーネントを表示するために [サーバー エクスプローラ] の [SharePoint 接続] のノードを使用できます。ただし、[サーバー エクスプローラ] は、コンポーネントが既定では表示されません。このチュートリアルでは、接続されている各SharePointサイトのWebパーツ ギャラリーを表示できるように [サーバー エクスプローラ] を拡張します。
このチュートリアルでは、次のタスクを実行します。
サーバー エクスプローラーを拡張する Visual Studio 拡張機能を作成する。具体的には、次の点を拡張します。
拡張子は **[サーバー エクスプローラ]**の各SharePointサイト ノードに [Web パーツ ギャラリー] のノードを追加します。この新しいノードには、サイト上の Web パーツ ギャラリー内の個々の Web パーツを表す子ノードが表示されます。
拡張機能では、Webパーツのインスタンスを表す新しいノード型を定義します。新しい Web パーツ ギャラリー ノードの子ノードには、このノード型が使用されます。新しい Web パーツ ノード型が表す Web パーツについての情報は、[プロパティ] ウィンドウに表示されます。ノード型には、Webパーツに関連する他のタスクの開始点として使用できるカスタム ショートカット メニュー項目があります。
拡張機能アセンブリが呼び出す2とおりのカスタムSharePointコマンドを作成します。SharePointコマンドは、SharePointのサーバー オブジェクト モデルのAPIを使用する拡張機能のアセンブリから呼び出すことのできるメソッドです。このチュートリアルでは、開発コンピューター上のローカル SharePoint サイトから Web パーツ情報が取得するコマンドを作成します。詳細については、「SharePoint オブジェクト モデルの呼び出し」を参照してください。
拡張機能を配置するための Visual Studio Extension (VSIX) パッケージを構築する。
拡張機能をデバッグしてテストする。
[!メモ]
サーバー オブジェクト モデルの代わりにSharePointのクライアント オブジェクト モデルを使用して、このチュートリアルの代替バージョンについては、チュートリアル: サーバー エクスプローラーの拡張機能から SharePoint クライアント オブジェクト モデルを呼び出すを参照してください。
必須コンポーネント
このチュートリアルを実行するには、開発コンピューターに次のコンポーネントが必要です。
Windows SharePoint、およびサポートされるVisual Studioのエディション。詳細については、「SharePoint ソリューションの開発要件」を参照してください。
Visual Studio SDK。このチュートリアルでは、SDK の VSIX プロジェクト テンプレートを使用して、プロジェクト項目を配置するための VSIX パッケージを作成します。詳細については、「Visual Studio の SharePoint ツールの拡張」を参照してください。
次の概念に関する知識があると役に立ちますが、チュートリアルを実行するうえで必須というわけではありません。
SharePointのサーバー オブジェクト モデルの使用。詳細については、「Using the SharePoint Foundation Server-Side Object Model (SharePoint Foundation Server 側オブジェクト モデルの使用)」を参照してください。
SharePoint ソリューションの Web パーツ。詳細については、「Web Parts Overview (Web パーツの概要)」を参照してください。
プロジェクトの作成
このチュートリアルを完了するには、3種類のプロジェクトを作成する必要があります:
拡張機能を配置するために VSIX パッケージを作成する VSIX プロジェクト。
プロジェクトの拡張機能を実装するクラス ライブラリ プロジェクト。このプロジェクトが.NET Framework 4.5を対象とする必要があります。
カスタムの SharePoint コマンドを定義するクラス ライブラリ プロジェクト。このプロジェクトは .NET Framework 3.5 を対象にする必要があります。
この 2 つのプロジェクトを作成することから始めます。
VSIX プロジェクトを作成するには
Visual Studio を起動します。
メニュー バーで [ファイル]、[新規]、[プロジェクト] の順にクリックします。
[新しいプロジェクト] のダイアログ ボックスで、[Visual C#] または [Visual Basic] のノードを展開し、[機能拡張] のノードを選択します。
[!メモ]
[機能拡張] のノードは、Visual Studio SDKをインストールするときだけです。詳細については、このトピックで前に説明した「前提条件」を参照してください。
ダイアログ ボックスの上部に、.NET Frameworkのバージョンの [.NET Framework 4.5] リストのを選択します。
[VSIX プロジェクト] テンプレートを選択し、プロジェクト [WebPartNode]を付けておくと、[OK] のボタンをクリックします。
Visual Studio のソリューション エクスプローラーに WebPartNode プロジェクトが追加されます。
拡張機能プロジェクトを作成するには
**[ソリューション エクスプローラー]で、ソリューション ノードのショートカット メニューを開き、[追加]**を選択し、を **[新しいプロジェクト]**を選択します。
[!メモ]
Visual Basic プロジェクトでソリューション エクスプローラーにソリューション ノードが表示されるのは、General, Projects and Solutions, Options Dialog Boxの [常にソリューションを表示] チェック ボックスがオンになっている場合だけです。
次に [新しいプロジェクト] のダイアログ ボックスで、[Visual C#] のノードまたはノードを [Visual Basic]、およびオプションの [ウィンドウ] のノードを展開します。
ダイアログ ボックスの上部に、.NET Frameworkのバージョンの [.NET Framework 4.5] リストのを選択します。
プロジェクト テンプレートの一覧で、[クラス ライブラリ]を選択し、プロジェクト [WebPartNodeExtension]を付けておくと、[OK] のボタンをクリックします。
Visual Studio によって、WebPartNodeExtension プロジェクトがソリューションに追加され、既定の Class1 コード ファイルが開きます。
Class1 コード ファイルをプロジェクトから削除します。
SharePoint コマンド プロジェクトを作成するには
**[ソリューション エクスプローラー]で、ソリューション ノードのショートカット メニューを開き、[追加]**を選択し、を **[新しいプロジェクト]**を選択します。
[!メモ]
Visual Basic プロジェクトでソリューション エクスプローラーにソリューション ノードが表示されるのは、General, Projects and Solutions, Options Dialog Boxの [常にソリューションを表示] チェック ボックスがオンになっている場合だけです。
[新しいプロジェクト] のダイアログ ボックスで、[Visual C#] の [Visual Basic] のノードまたはノードを展開し、[ウィンドウ] のノードを選択します。
ダイアログ ボックスの上部に、.NET Frameworkのバージョンの [.NET Framework 3.5] リストのを選択します。
プロジェクト テンプレートの一覧で、[クラス ライブラリ]を選択し、プロジェクト [WebPartCommands]を付けておくと、[OK] のボタンをクリックします。
Visual Studio によって、WebPartCommands プロジェクトがソリューションに追加され、既定の Class1 コード ファイルが開きます。
Class1 コード ファイルをプロジェクトから削除します。
プロジェクトの構成
拡張機能を作成するためのコードを記述する前に、コード ファイルおよびアセンブリ参照を追加し、プロジェクトの設定を構成します。
WebPartNodeExtension プロジェクトを構成するには
WebPartNodeExtensionプロジェクトで、次の名前を持つ4種類のコード ファイルを追加します:
SiteNodeExtension
WebPartNodeTypeProvider
WebPartNodeInfo
WebPartCommandIds
[WebPartNodeExtension] のプロジェクトのショートカット メニューを開き、**[参照の追加]**を選択します。
[マネージャーの– WebPartNodeExtensionを参照してください。] のダイアログ ボックスで、[Framework] のタブをクリックし、次のアセンブリのチェック ボックスをオンにします:
System.ComponentModel.Composition
System.Windows.Forms
[拡張機能] のタブをクリックし、Microsoft.VisualStudio.SharePointアセンブリのチェック ボックスをオンに [OK] のボタンをクリックします。
[ソリューション エクスプローラー]では、[WebPartNodeExtension] のプロジェクト ノードのショートカット メニューを開き、**[プロパティ]**を選択します。
プロジェクト デザイナーが開きます。
[アプリケーション] のタブをクリックします。
[既定の名前空間] ボックス (C#)、または [ルート名前空間] ボックス (Visual Basic) では、ServerExplorer.SharePointConnections.WebPartNodeを入力します。
WebPartCommands プロジェクトを構成するには
WebPartCommandsでは、WebPartCommandsという名前のコード ファイルをプロジェクトを追加します。
[ソリューション エクスプローラー]では、[WebPartCommands] のプロジェクト ノードのショートカット メニューを開き、**[追加]**を選択し、を **[既存の項目]**を選択します。
[既存項目の追加] のダイアログ ボックスで、WebPartNodeExtensionプロジェクトのコード ファイルが格納されているフォルダーを参照し、WebPartNodeInfoおよびWebPartCommandIdsのコード ファイル フォルダーを選択します。
矢印を [追加] のボタンの横にあるを選択し、表示されるメニューの [リンクとして追加] を選択します。
Visual Studio により、WebPartCommands プロジェクトにリンクとしてコード ファイルが追加されます。その結果、コード ファイルはWebPartNodeExtensionプロジェクトにありますが、そのファイル内のコードは、WebPartCommandsプロジェクトでコンパイルされます。
[WebPartCommands] のプロジェクトのショートカット メニューを再度開き、**[参照の追加]**を選択します。
[マネージャーの– WebPartCommandsを参照してください。] のダイアログ ボックスで、[拡張機能] のタブをクリックし、次のアセンブリのチェック ボックスをオンに [OK] のボタンを選択する:
Microsoft.SharePoint
Microsoft.VisualStudio.SharePoint.Commands
[ソリューション エクスプローラー]では、[WebPartCommands] のプロジェクトのショートカット メニューを再度開き、**[プロパティ]**を選択します。
プロジェクト デザイナーが開きます。
[アプリケーション] のタブをクリックします。
[既定の名前空間] ボックス (C#)、または [ルート名前空間] ボックス (Visual Basic) では、ServerExplorer.SharePointConnections.WebPartNodeを入力します。
新しいノードのアイコンの作成
サーバー エクスプローラーの拡張機能に対して 2 つのアイコンを作成します。新しい Web パーツ ギャラリー ノードのアイコンと、Web パーツ ギャラリー ノード下に存在するそれぞれの子 Web パーツ ノードのアイコンです。これらのアイコンをノードに関連付けるコードは後で作成します。
ノードのアイコンを作成するには
[ソリューション エクスプローラー]では、[WebPartNodeExtension] のプロジェクトのショートカット メニューを開き、**[プロパティ]**を選択します。
プロジェクト デザイナーが開きます。
[リソース] タブをクリックし、このプロジェクトを含まない既定のリソース ファイルを選択します。1 種類のリンクを作成するには、ここをクリックしてください。
Visual Studio は、リソース ファイルと開き、デザイナーで開かれます作成します。
デザイナーの上部で、下向きの矢印を [リソースの追加] のメニュー コマンドの横にあるを選択し、表示されるメニューの [新しいアイコンの追加] を選択します。
[新しいリソースの追加] のダイアログ ボックスでは、新しいアイコンを WebPartsNodeし、[追加] のボタンをクリックします。
イメージ エディターに新しいアイコンが表示されます。
簡単に検証しやすいデザインとなるよう16x16版のアイコン ファイルを編集します。
32x32版のアイコンのショートカット メニューを開き、**[イメージ タイプの削除]**を選択します。
手順5からプロジェクト リソースに2番目のアイコンを追加する8を繰り返します。このアイコン [Web パーツ]を表示します。
[WebPartNodeExtension] のプロジェクトの [リソース] フォルダーの下の **[ソリューション エクスプローラー]**では、を開きます **[WebPartsNode.ico]**のショートカット メニュー。
[プロパティ] のペインで、の横の矢印を **[ビルド アクション]**選択し、表示されるメニューの [埋め込みリソース] を選択します。
WebPart.ico について最後の 2 つの手順を繰り返します。
サーバー エクスプローラーへの Web パーツ ギャラリー ノードの追加
各 SharePoint サイト ノードに新しい Web パーツ ギャラリー ノードを追加するクラスを作成します。新しいノードを追加するため、このクラスに 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 Namespaceusing 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 Namespaceusing 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 Namespaceusing 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 Namespacenamespace 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 Namespaceusing 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 コマンドに必要なすべてのコードがプロジェクトに揃ったことになります。エラーが発生することなく 2 つのプロジェクトをコンパイルできるかどうか、ソリューションをビルドして確認してください。
ソリューションをビルドするには
メニュー バーで、[ビルド]、**[ソリューションのビルド]**を選択します。
注意この時点で、WebPartNodeプロジェクトは、VSIXマニフェスト ファイルに作成者の値がないため、ビルド エラーがある場合があります。このエラーは、後の手順の値を追加すると解消されます。
拡張機能を配置するための VSIX パッケージの作成
拡張機能を配置するには、ソリューションで VSIX プロジェクトを使用して VSIX パッケージを作成します。まず、VSIXプロジェクトのsource.extension.vsixmanifestファイルを変更して、VSIXパッケージを構成します。次に、ソリューションをビルドして VSIX パッケージを作成します。
VSIX パッケージを構成するには
**[ソリューション エクスプローラー]**では、WebPartNodeプロジェクトの下で、を開きマニフェスト エディターでファイル [source.extension.vsixmanifest]。
source.extension.vsixmanifestファイルは、すべてのVSIXパッケージが必要とするextension.vsixmanifestファイルの基礎です。このファイルの詳細については、「VSIX 拡張機能のスキーマに関するリファレンス」を参照してください。
[製品名] ボックスに、[Web Part Gallery Node for Server Explorer]を入力します。
[作成者] ボックスに、[Contoso]を入力します。
[Description] ボックスで、追加するサーバー エクスプローラーの[SharePoint接続]ノードにカスタムWebパーツ ギャラリー ノードを入力します。This extension uses a custom SharePoint command to call into the server object model.」と入力します。
エディターの [資産] のタブをクリックし、[新規作成] のボタンをクリックします。
[新しい資産の追加] のダイアログ ボックスが表示されます。
[種類] の一覧で、**[Microsoft.VisualStudio.MefComponent]**を選択します。
[!メモ]
この値は、extension.vsixmanifest ファイル内の MefComponent 要素に対応します。この要素は、VSIX パッケージ内の拡張機能アセンブリの名前を指定します。詳細については、「MEFComponent Element」を参照してください。
[ソース] の一覧で、**[現在のソリューション内のプロジェクト]**を選択します。
[プロジェクト] の一覧で、[WebPartNodeExtension] を選択し、[OK] のボタンをクリックします。
マニフェスト エディターで、[新規作成] のボタンをもう一度クリックします。
[新しい資産の追加] のダイアログ ボックスが表示されます。
[種類] ボックスに、[SharePoint.Commands.v4]を入力します。
[!メモ]
Visual Studio の拡張機能に追加するカスタム拡張機能は、この要素によって指定されます。詳細については、「資産の要素 (VSX スキーマ)」を参照してください。
[ソース] の一覧で、[現在のソリューション内のプロジェクト] の一覧の項目を選択します。
[プロジェクト] の一覧で、[WebPartCommands]を選択し、[OK] のボタンをクリックします。
メニュー バーで、[ビルド]、**[ソリューションのビルド]**を選択し、次に、ソリューションのエラーなしでコンパイルしてください。
WebPartNodeプロジェクトのビルド出力フォルダーにWebPartNode.vsixファイルが含まれていることを確認します。
既定では、プロジェクト ファイルに格納されているフォルダーの ..\bin\Debug フォルダーがビルド出力フォルダーです。
拡張機能のテスト
**[サーバー エクスプローラ]**の [Web パーツ ギャラリー] の新しいノードをテストする準備ができました。まず、Visual Studio の実験用インスタンスで拡張機能のデバッグを開始します。次に、Visual Studio の実験用インスタンスで新しい [Web パーツ] ノードを使用します。
拡張機能のデバッグを開始するには
管理資格情報を使用してを Visual Studio を再起動し、WebPartNodeソリューションを開きます。
WebPartNodeExtensionプロジェクトで、SiteNodeExtensionコード ファイルを開き、NodeChildrenRequested と CreateWebPartNodes のメソッドのコードの先頭行にブレークポイントを追加します。
F5 キーを押してデバッグを開始します。
Visual Studioは、サーバーExplorer\1.0の%UserProfile%\AppData\Local\Microsoft\VisualStudio\11.0Exp\Extensions\Contoso\Webのパーツ ギャラリー ノードの拡張機能に拡張機能をインストール、Visual Studioの実験用インスタンスを起動します。このインスタンスの Visual Studio でプロジェクト項目をテストします。
拡張機能をテストするには
Visual Studioの実験用インスタンスで、メニュー バーで、[表示]、**[サーバー エクスプローラ]**を選択します。
まず、テストに使用するSharePointサイトが **[サーバー エクスプローラ]**の [SharePoint 接続] のノードの下に表示されていない場合は、次の手順を実行します:
**[サーバー エクスプローラ]では、[SharePoint 接続]のショートカット メニューを開き、[接続の追加]**を選択します。
[SharePoint 接続の追加] のダイアログ ボックスで、接続先の入力し、次に [OK] のボタンを選択します。SharePointサイトのURL。
開発用コンピューター上のSharePointサイトを指定するには、https://localhostを入力します。
(サイトのURLを表示する) サイト接続ノードを展開し、子サイト ノード (たとえば、[チーム サイト]) を展開します。
Visual Studio の他のインスタンスのコードがプロジェクトのデバッグが NodeChildrenRequested のメソッドの前に設定、つもりでF5キーを選択したブレークポイントで停止することを確認します。
Visual Studioの実験用インスタンスで、[Web パーツ ギャラリー] という名前の新しいノードが最上位のサイト ノードの下にある認識し、[Web パーツ ギャラリー] のノードを展開します。
Visual Studioのもう一方のインスタンスで、コードがプロジェクトのデバッグが CreateWebPartNodes のメソッドの前に設定、つもりでF5キーを選択したブレークポイントで停止することを確認します。
Visual Studioの実験用インスタンスで、接続先サイトのすべてのWebパーツが **[サーバー エクスプローラ]**の [Web パーツ ギャラリー] ノードの下にあることを確認します。
**[サーバー エクスプローラ]にWebパーツの1人のショートカット メニューを開き、[プロパティ]**を選択します。
デバッグ Visual Studio のインスタンスでWebパーツに関する詳細が [プロパティ] のペインに表示されることを確認します。
Visual Studio からの拡張機能のアンインストール
拡張機能のテストが完了したら、拡張機能を Visual Studio からアンインストールします。
拡張機能をアンインストールするには
Visual Studioの実験用インスタンスで、メニュー バーで、[ツール]、**[拡張機能と更新プログラム]**を選択します。
[拡張機能と更新プログラム] のダイアログ ボックスが表示されます。
拡張機能の一覧で、[Web Part Gallery Node Extension for Server Explorer]を選択し、[アンインストール] のボタンをクリックします。
表示されたダイアログ ボックスで、拡張機能をアンインストールする選択し、アンインストールを実行するに [今すぐ再起動] のボタンを選択します。ことを確認するために [○] のボタンをクリックします。
Visual Studioの実験用インスタンスとインスタンス (WebPartNodeソリューションが開いている) Visual Studioの両方のインスタンスのインスタンスを閉じます。
参照
処理手順
チュートリアル: サーバー エクスプローラーの拡張機能から SharePoint クライアント オブジェクト モデルを呼び出す
アイコンまたはその他のイメージの作成 (アイコン用イメージ エディター)