How to: Retrieve the SharePoint Project Service
You can access the SharePoint project service in the following types of solutions:
An extension of the SharePoint project system, such as a project extension, project item extension, or project item type definition. For more information about these types of extensions, see Extending the SharePoint Project System.
An extension of the SharePoint Connections node in Server Explorer. For more information about these types of extensions, see Extending the SharePoint Connections Node in Server Explorer.
Another type of Visual Studio extension, such as an add-in or a VSPackage.
Retrieving the Service in Project System Extensions
In any extension of the SharePoint project system, you can access the project service by using the ProjectService property of an ISharePointProject object.
You can also retrieve the project service by using the following procedures.
To retrieve the service in a project extension
In your implementation of the ISharePointProjectExtension interface, locate the Initialize method.
Use the projectService parameter to access the service.
The following code example demonstrates how to use the project service to write a message to the Output window and Error List window in a simple project extension.
<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); } }
For more information about creating project extensions, see How to: Create a SharePoint Project Extension.
To retrieve the service in a project item extension
In your implementation of the ISharePointProjectItemTypeExtension interface, locate the Initialize method.
Use the ProjectService property of the projectItemType parameter to retrieve the service.
The following code example demonstrates how to use the project service to write a message to the Output window and Error List window in a simple extension of the List Definition project item.
<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); } }
For more information about creating project item extensions, see How to: Create a SharePoint Project Item Extension.
To retrieve the service in a project item type definition
In your implementation of the ISharePointProjectItemTypeProvider interface, locate the InitializeType method.
Use the ProjectService property of the typeDefinition parameter to retrieve the service.
The following code example demonstrates how to use the project service to write a message to the Output window and Error List window in a simple project item type definition.
<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); } }
For more information about defining project item types, see How to: Define a SharePoint Project Item Type.
Retrieving the Service in Server Explorer Extensions
In an extension of the SharePoint Connections node in Server Explorer, you can access the project service by using the ServiceProvider property of an IExplorerNode object.
To retrieve the service in a Server Explorer extension
Get an IServiceProvider object from the ServiceProvider property of an IExplorerNode object in your extension.
Use the GetService method to request an ISharePointProjectService object.
The following code example demonstrates how to use the project service to write a message to the Output window and Error List window from a shortcut menu that the extension adds to list nodes in Server Explorer.
<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); } }
For more information about extending the SharePoint Connections node in Server Explorer, see How to: Extend a SharePoint Node in Server Explorer.
Retrieving the Service in Other Visual Studio Extensions
You can retrieve the project service in a VSPackage, or in any Visual Studio extension that has access to a EnvDTE80.DTE2 object in the automation object model, such as an add-in or a project template wizard that implements the Microsoft.VisualStudio.TemplateWizard.IWizard interface.
In a VSPackage, you can request an ISharePointProjectService object by using one of the following methods:
The GetService method of a managed VSPackage that derives from the Microsoft.VisualStudio.Shell.Package class. For more information, see How to: Consume a Service.
The static Package.GetGlobalService method. For more information, see How to: Use GetGlobalService.
In a Visual Studio extension that has access to a EnvDTE80.DTE2 object, you can request an ISharePointProjectService object by using the GetService() method of a Microsoft.VisualStudio.Shell.ServiceProvider object. For more information, see How to: Get a Service from the DTE Object.
Example
The following code example demonstrates how to retrieve the project service in a Visual Studio add-in. To use this code, run it from the Connect class in an add-in project. The _applicationObject object is automatically generated in add-in projects; this object is an instance of the EnvDTE80.DTE2 interface.
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);
}
This example requires:
A Visual Studio add-in project. For more information, see How to: Create an Add-In.
References to the Microsoft.VisualStudio.OLE.Interop, Microsoft.VisualStudio.Shell and Microsoft.VisualStudio.SharePoint assemblies.
See Also
Tasks
How to: Get a Service from the DTE Object
How to: Use Wizards with Project Templates