共用方式為


HOW TO:擷取 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);
        }
    }
    

    如需建立專案擴充功能的詳細資訊,請參閱 HOW TO:建立 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);
        }
    }
    

    如需建立專案項目擴充功能的詳細資訊,請參閱 HOW TO:建立 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);
        }
    }
    

    如需定義專案項目類型的詳細資訊,請參閱 HOW TO:定義 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 連接] 節點的詳細資訊,請參閱 HOW TO:在伺服器總管中擴充 SharePoint 節點

擷取其他 Visual Studio 擴充功能中的服務

您可以在 VSPackage 中或任何 Visual Studio 擴充功能中擷取專案服務,後者具有自動化物件模型中的 EnvDTE80.DTE2 物件,例如增益集或專案範本精靈,其會實作 Microsoft.VisualStudio.TemplateWizard.IWizard 介面。

在 VSPackage 中,您可以使用下列其中一個方法要求 ISharePointProjectService 物件:

在可以存取 EnvDTE80.DTE2 物件的 Visual Studio 擴充功能中,您可以使用 Microsoft.VisualStudio.Shell.ServiceProvider 物件的 GetService() 方法,要求 ISharePointProjectService 物件。如需詳細資訊,請參閱 How to: 從 DTE 物件中取得服務

Ee471432.collapse_all(zh-tw,VS.110).gif範例

下列程式碼範例示範如何在 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 增益集專案。如需詳細資訊,請參閱 HOW TO:建立增益集

  • Microsoft.VisualStudio.OLE.Interop、Microsoft.VisualStudio.Shell 和 Microsoft.VisualStudio.SharePoint 組件的參考資料。

請參閱

工作

HOW TO:建立增益集

How to: 使用服務

How to: 從 DTE 物件中取得服務

HOW TO:搭配專案範本使用精靈

概念

使用 SharePoint 專案服務