Convert between SharePoint project system types and other Visual Studio project types
Applies to: Visual Studio Visual Studio for Mac
Note
This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
In some cases you might have an object in the SharePoint project system and you want to use features of a corresponding object in the Visual Studio automation object model or integration object model, or vice versa. In these cases, you can use the Convert method of the SharePoint project service to convert the object to a different object model.
For example, you might have an ISharePointProject object, but you want to use methods that are only available on an Project or IVsProject object. In this case, you can use the Convert method to convert the ISharePointProject to an Project or IVsProject.
For more information about the Visual Studio automation object model and the Visual Studio integration object model, see Overview of the programming model of SharePoint tools extensions.
Types of conversions
The following table lists the types that this method can convert between the SharePoint project system and the other Visual Studio object models.
SharePoint project system type | Corresponding types in the automation and integration object models |
---|---|
ISharePointProject | Project or Any interface in the Visual Studio integration object model that is implemented by the underlying COM object for the project. These interfaces include IVsHierarchy, IVsProject (or a derived interface), and IVsBuildPropertyStorage. For a list of the main interfaces that are implemented by projects, see Project Model Core Components. |
IMappedFolder ISharePointProjectItem ISharePointProjectItemFile ISharePointProjectFeature ISharePointProjectFeatureResourceFile ISharePointProjectPackage |
ProjectItem or AUInt32 value (also called a VSITEMID) that identifies the project member in the IVsHierarchy that contains it. This value can be passed to the itemid parameter of some IVsHierarchy methods. |
Example
The following code example demonstrates how to use the Convert method to convert an ISharePointProject object to an Project.
void projectService_ProjectAdded(object sender, Microsoft.VisualStudio.SharePoint.SharePointProjectEventArgs e)
{
EnvDTE.Project dteProject = e.Project.ProjectService.Convert<
Microsoft.VisualStudio.SharePoint.ISharePointProject, EnvDTE.Project>(e.Project);
if (dteProject != null)
{
// Use the Visual Studio automation object model to add a folder to the project.
dteProject.ProjectItems.AddFolder("Data");
}
}
Private Sub projectService_ProjectAdded(ByVal sender As Object, _
ByVal e As Microsoft.VisualStudio.SharePoint.SharePointProjectEventArgs)
Dim dteProject As EnvDTE.Project = e.Project.ProjectService.Convert( _
Of Microsoft.VisualStudio.SharePoint.ISharePointProject, EnvDTE.Project)(e.Project)
If dteProject IsNot Nothing Then
' Use the Visual Studio automation object model to add a folder to the project.
dteProject.ProjectItems.AddFolder("Data")
End If
End Sub
This example requires:
An extension of the SharePoint project system that has a reference to the EnvDTE.dll assembly. For more information, see Extend the SharePoint project system.
Code that registers the
projectService_ProjectAdded
method to handle the ProjectAdded event of an ISharePointProjectService object. For an example, see How to: Create a SharePoint project extension.