次の方法で共有


方法: SharePoint プロジェクト サービスを取得する

SharePoint プロジェクト サービスには、次の種類のソリューションからアクセスできます。

  • SharePoint プロジェクト システムの拡張機能 (プロジェクトの拡張機能、プロジェクト項目の拡張機能、プロジェクト項目の種類の定義など)。 これらの拡張機能の種類の詳細については、「SharePoint プロジェクト システムの拡張」を参照してください。

  • サーバー エクスプローラー[SharePoint 接続] ノードの拡張 これらの拡張機能の種類の詳細については、「サーバー エクスプローラーの [SharePoint 接続] ノードの拡張」を参照してください。

  • その他の種類の Visual Studio 拡張機能 (アドイン、VSPackage など)

プロジェクト システムの拡張機能でのサービスの取得

SharePoint プロジェクト システムの拡張機能からプロジェクト サービスにアクセスするには、ISharePointProject オブジェクトの ProjectService プロパティを使用します。

また、プロジェクト サービスは、次の手順に従って取得することもできます。

プロジェクトの拡張機能でサービスを取得するには

  1. ISharePointProjectExtension インターフェイスの実装で、Initialize メソッドを探します。

  2. サービスにアクセスするには、projectService パラメーターを使用します。

    次のコード例は、プロジェクト サービスを使用して、単純なプロジェクト拡張機能でメッセージを [出力] ウィンドウおよび [エラー一覧] ウィンドウに書き込む方法を示します。

    <Export(GetType(ISharePointProjectExtension))> _
    Friend Class GetServiceInProject
        Implements ISharePointProjectExtension
    
        Private Sub Initialize(ByVal projectService As ISharePointProjectService) _
            Implements ISharePointProjectExtension.Initialize
            projectService.Logger.WriteLine("This message was written by using the " & _
                "project service in a project extension.", LogCategory.Message)
        End Sub
    End Class
    
    [Export(typeof(ISharePointProjectExtension))]
    internal class GetServiceInProject : ISharePointProjectExtension
    {
        public void Initialize(ISharePointProjectService projectService)
        {
            projectService.Logger.WriteLine("This message was written by using the " +
                "project service in a project extension.", LogCategory.Message);
        }
    }
    

    プロジェクトの拡張機能の作成の詳細については、「方法: SharePoint プロジェクトの拡張機能を作成する」を参照してください。

プロジェクト項目の拡張機能でサービスを取得するには

  1. ISharePointProjectItemTypeExtension インターフェイスの実装で、Initialize メソッドを探します。

  2. サービスを取得するには、projectItemType パラメーターの ProjectService プロパティを使用します。

    次のコード例は、プロジェクト サービスを使用して、リスト定義プロジェクト項目の単純な拡張機能でメッセージを [出力] ウィンドウおよび [エラー一覧] ウィンドウに書き込む方法を示します。

    <Export(GetType(ISharePointProjectItemTypeExtension))> _
    <SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.ListDefinition")> _
    Friend Class GetServiceInProjectItem
        Implements ISharePointProjectItemTypeExtension
    
        Private Sub Initialize(ByVal projectItemType As ISharePointProjectItemType) _
            Implements ISharePointProjectItemTypeExtension.Initialize
            projectItemType.ProjectService.Logger.WriteLine("This message was written " & _
                "by using the project service in an extension for the ListDefinition project item.", _
                LogCategory.Message)
        End Sub
    End Class
    
    [Export(typeof(ISharePointProjectItemTypeExtension))]
    [SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.ListDefinition")]
    internal class GetServiceInProjectItem : ISharePointProjectItemTypeExtension
    {
        public void Initialize(ISharePointProjectItemType projectItemType)
        {
            projectItemType.ProjectService.Logger.WriteLine("This message was written " +
                "by using the project service in an extension for the ListDefinition project item.", 
                LogCategory.Message);
        }
    }
    

    プロジェクト項目の拡張機能の作成の詳細については、「方法: SharePoint プロジェクト項目の拡張機能を作成する」を参照してください。

プロジェクト項目の種類の定義でサービスを取得するには

  1. ISharePointProjectItemTypeProvider インターフェイスの実装で、InitializeType メソッドを探します。

  2. サービスを取得するには、typeDefinition パラメーターの ProjectService プロパティを使用します。

    次のコード例は、プロジェクト サービスを使用して、単純なプロジェクト項目の種類の定義でメッセージを [出力] ウィンドウおよび [エラー一覧] ウィンドウに書き込む方法を示します。

    <Export(GetType(ISharePointProjectItemTypeProvider))> _
    <SharePointProjectItemType("Contoso.CustomAction")> _
    Friend Class CustomActionProvider
        Implements ISharePointProjectItemTypeProvider
    
        Private Sub InitializeType(ByVal projectItemTypeDefinition As ISharePointProjectItemTypeDefinition) _
            Implements ISharePointProjectItemTypeProvider.InitializeType
            projectItemTypeDefinition.ProjectService.Logger.WriteLine("This message was written " & _
                "by using the project service in the Custom Action project item type.", _
                LogCategory.Message)
        End Sub
    End Class
    
    [Export(typeof(ISharePointProjectItemTypeProvider))]
    [SharePointProjectItemType("Contoso.CustomAction")]
    internal class CustomActionProvider : ISharePointProjectItemTypeProvider
    {
        public void InitializeType(ISharePointProjectItemTypeDefinition projectItemTypeDefinition)
        {
            projectItemTypeDefinition.ProjectService.Logger.WriteLine("This message was written " +
                "by using the project service in the Custom Action project item type definition.",
                LogCategory.Message);
        }
    }
    

    プロジェクト項目の種類の詳細については、「方法: SharePoint プロジェクト項目の種類を定義する」を参照してください。

サーバー エクスプローラーの拡張機能でのサービスの取得

サーバー エクスプローラー[SharePoint 接続] ノードの拡張機能からプロジェクト サービスにアクセスするには、IExplorerNode オブジェクトの ServiceProvider プロパティを使用します。

サーバー エクスプローラーの拡張機能でサービスを取得するには

  1. 拡張機能の IExplorerNode オブジェクトの ServiceProvider プロパティから IServiceProvider オブジェクトを取得します。

  2. GetService メソッドを使用して、ISharePointProjectService オブジェクトを要求します。

    次のコード例は、プロジェクト サービスを使用して、拡張機能によってサーバー エクスプローラーのリストのノードに追加されるショートカット メニューでメッセージを [出力] ウィンドウおよび [エラー一覧] ウィンドウに書き込む方法を示します。

    <Export(GetType(IExplorerNodeTypeExtension))> _
    <ExplorerNodeType(ExtensionNodeTypes.ListNode)> _
    Friend Class ListNodeExtension
        Implements IExplorerNodeTypeExtension
    
        Private projectService As ISharePointProjectService
    
        Private Sub Initialize(ByVal nodeType As IExplorerNodeType) _
            Implements IExplorerNodeTypeExtension.Initialize
            AddHandler nodeType.NodeMenuItemsRequested, AddressOf nodeType_NodeMenuItemsRequested
        End Sub
    
        Private Sub nodeType_NodeMenuItemsRequested(ByVal Sender As Object, ByVal e As ExplorerNodeMenuItemsRequestedEventArgs)
            Dim writeMessageMenuItem As IMenuItem = e.MenuItems.Add("Write Message to Output Window and Error List Window")
            AddHandler writeMessageMenuItem.Click, AddressOf writeMessageMenuItem_Click
        End Sub
    
        Private Sub writeMessageMenuItem_Click(ByVal Sender As Object, ByVal e As MenuItemEventArgs)
            Dim node As IExplorerNode = CType(e.Owner, IExplorerNode)
            If projectService Is Nothing Then
                projectService = CType(node.ServiceProvider.GetService(GetType(ISharePointProjectService)), ISharePointProjectService)
            End If
            projectService.Logger.WriteLine("Clicked the menu item for " + node.Text, LogCategory.Message)
        End Sub
    End Class
    
    [Export(typeof(IExplorerNodeTypeExtension))]
    [ExplorerNodeType(ExtensionNodeTypes.ListNode)]
    internal class ListNodeExtension : IExplorerNodeTypeExtension
    {
        private ISharePointProjectService projectService;
    
        public void Initialize(IExplorerNodeType nodeType)
        {
            nodeType.NodeMenuItemsRequested += nodeType_NodeMenuItemsRequested;
        }
    
        void nodeType_NodeMenuItemsRequested(object sender, ExplorerNodeMenuItemsRequestedEventArgs e)
        {
            IMenuItem writeMessageMenuItem = e.MenuItems.Add("Write Message to Output Window and Error List Window");
            writeMessageMenuItem.Click += writeMessageMenuItem_Click;
        }
    
        void writeMessageMenuItem_Click(object sender, MenuItemEventArgs e)
        {
            IExplorerNode node = (IExplorerNode)e.Owner;
            if (projectService == null)
            {
                projectService = (ISharePointProjectService)node.ServiceProvider.GetService(typeof(ISharePointProjectService));
            }
    
            projectService.Logger.WriteLine("Clicked the menu item for " + node.Text, LogCategory.Message);
        }
    }
    

    サーバー エクスプローラー[SharePoint 接続] ノードの拡張の詳細については、「方法: サーバー エクスプローラーの SharePoint ノードを拡張する」を参照してください。

他の Visual Studio 拡張機能でのサービスの取得

VSPackage で、またはオートメーション オブジェクト モデル (アドイン、Microsoft.VisualStudio.TemplateWizard.IWizard インターフェイスを実装するプロジェクト テンプレート ウィザードなど) の EnvDTE80.DTE2 オブジェクトにアクセスできる Visual Studio 拡張機能で、プロジェクト サービスを取得できます。

VSPackage で ISharePointProjectService オブジェクトを要求するには、次のいずれかのメソッドを使用します。

EnvDTE80.DTE2 オブジェクトにアクセスできる Visual Studio 拡張機能で、ISharePointProjectService オブジェクトを要求するには、Microsoft.VisualStudio.Shell.ServiceProvider オブジェクトの GetService() メソッドを使用します。 詳細については、「How to: Get a Service from the DTE Object」を参照してください。

Visual Studio アドインで、プロジェクト サービスを取得する方法を次のコード例に示します。 このコード例を使用するには、アドイン プロジェクトの Connect クラスから実行します。 _applicationObject オブジェクトがアドイン プロジェクトで自動的に生成されます。このオブジェクトは、EnvDTE80.DTE2 インターフェイスのインスタンスです。

Dim serviceProvider As Microsoft.VisualStudio.Shell.ServiceProvider = _
    New Microsoft.VisualStudio.Shell.ServiceProvider( _
        TryCast(_applicationObject, Microsoft.VisualStudio.OLE.Interop.IServiceProvider))

Dim projectService As Microsoft.VisualStudio.SharePoint.ISharePointProjectService = _
    TryCast(serviceProvider.GetService(GetType(Microsoft.VisualStudio.SharePoint.ISharePointProjectService)), _
        Microsoft.VisualStudio.SharePoint.ISharePointProjectService)

If projectService IsNot Nothing Then
    projectService.Logger.WriteLine("This message was written by using the SharePoint project service.", _
        Microsoft.VisualStudio.SharePoint.LogCategory.Message)
End If
Microsoft.VisualStudio.Shell.ServiceProvider serviceProvider =
    new Microsoft.VisualStudio.Shell.ServiceProvider(
    _applicationObject as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);

Microsoft.VisualStudio.SharePoint.ISharePointProjectService projectService = 
    serviceProvider.GetService(typeof(Microsoft.VisualStudio.SharePoint.ISharePointProjectService))
    as Microsoft.VisualStudio.SharePoint.ISharePointProjectService;

if (projectService != null)
{
    projectService.Logger.WriteLine("This message was written by using the SharePoint project service.",
        Microsoft.VisualStudio.SharePoint.LogCategory.Message);
}

この例で必要な要素は次のとおりです。

  • Visual Studio アドイン プロジェクト 詳細については、「方法 : アドインを作成する」を参照してください。

  • References to the Microsoft.VisualStudio.OLE.Interop, Microsoft.VisualStudio.Shell アセンブリおよび Microsoft.VisualStudio.SharePoint アセンブリへの参照

参照

処理手順

方法 : アドインを作成する

How to: Consume a Service

How to: Get a Service from the DTE Object

方法 : プロジェクト テンプレートを組み合わせたウィザードを使用する

その他の技術情報

SharePoint プロジェクト サービスの使用