如何:检索 SharePoint 项目服务

可以在以下类型的解决方案中访问 SharePoint 项目服务:

在项目系统扩展中检索服务

在任何 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 中检索项目服务,也可以在具有对自动化对象模型中的 EnvDTE80.DTE2 对象的访问权的任何 Visual Studio 扩展(如外接程序或实现 Microsoft.VisualStudio.TemplateWizard.IWizard 接口的项目模板向导)中检索项目服务。

在 VSPackage 中,可以通过使用以下方法之一来请求 ISharePointProjectService 对象:

在具有对 EnvDTE80.DTE2 对象的访问权的 Visual Studio 扩展中,可以通过使用 Microsoft.VisualStudio.Shell.ServiceProvider 对象的 GetService() 方法来请求 ISharePointProjectService 对象。 有关更多信息,请参见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 外接程序项目。 有关更多信息,请参见如何:创建外接程序

  • 对 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 项目服务