Walkthrough: Extending Server Explorer to Display Web Parts
In Visual Studio, you can use the SharePoint Connections node of Server Explorer to view components on SharePoint sites. However, Server Explorer doesn't display some components by default. In this walkthrough, you'll extend Server Explorer so that it displays the Web Part gallery on each connected SharePoint site.
This walkthrough demonstrates the following tasks:
Creating a Visual Studio extension that extends Server Explorer in the following ways:
The extension adds a Web Part Gallery node under each SharePoint site node in Server Explorer. This new node contains child nodes that represent each Web Part in the Web Part gallery on the site.
The extension defines a new type of node that represents a Web Part instance. This new node type is the basis for the child nodes under the new Web Part Gallery node. The new Web Part node type displays information in the Properties window about the Web Part that it represents. The node type also includes a custom shortcut menu item that you can use as a starting point for performing other tasks that relate to the Web Part.
Creating two custom SharePoint commands that the the extension assembly calls. SharePoint commands are methods that can be called by extension assemblies to use APIs in the server object model for SharePoint. In this walkthrough, you create commands that retrieve Web Part information from the local SharePoint site on the development computer. For more information, see Calling into the SharePoint Object Models.
Building a Visual Studio Extension (VSIX) package to deploy the extension.
Debugging and testing the extension.
|
You need the following components on the development computer to complete this walkthrough:
Supported editions of Windows, SharePoint and Visual Studio. For more information, see Requirements for Developing SharePoint Solutions.
The Visual Studio SDK. This walkthrough uses the VSIX Project template in the SDK to create a VSIX package to deploy the project item. For more information, see Extending the SharePoint Tools in Visual Studio.
Knowledge of the following concepts is helpful, but not required, to complete the walkthrough:
Using the server object model for SharePoint. For more information, see Using the SharePoint Foundation Server-Side Object Model.
Web Parts in SharePoint solutions. For more information, see Web Parts Overview.
To complete this walkthrough, you must create three projects:
A VSIX project to create the VSIX package to deploy the extension.
A class library project that implements the extension. This project must target the .NET Framework 4.5.
A class library project that defines the custom SharePoint commands. This project must target the.NET Framework 3.5.
Start the walkthrough by creating the projects.
To create the VSIX project
Start Visual Studio.
On the menu bar, choose File, New, Project.
In the New Project dialog box, expand the Visual C# or Visual Basic nodes, and then choose the Extensibility node.
Note
The Extensibility node is available only if you install the Visual Studio SDK. For more information, see the prerequisites section earlier in this topic.At the top of the dialog box, choose .NET Framework 4.5 in the list of versions of the .NET Framework.
Choose the VSIX Project template, name the project WebPartNode, and then choose the OK button.
Visual Studio adds the WebPartNode project to Solution Explorer.
To create the extension project
In Solution Explorer, open the shortcut menu for the solution node, choose Add, and then choose New Project.
Note
In Visual Basic projects, the solution node appears in Solution Explorer only when the Always show solution check box is selected in the NIB: General, Projects and Solutions, Options Dialog Box.In the New Project dialog box, expand the Visual C# node or Visual Basic node, and then the choose Windows node.
At the top of the dialog box, choose .NET Framework 4.5 in the list of versions of the .NET Framework.
In the list of project templates, choose Class Library, name the project WebPartNodeExtension, and then choose the OK button.
Visual Studio adds the WebPartNodeExtension project to the solution and opens the default Class1 code file.
Delete the Class1 code file from the project.
To create the SharePoint commands project
In Solution Explorer, open the shortcut menu for the solution node, choose Add, and then choose New Project.
Note
In Visual Basic projects, the solution node appears in Solution Explorer only when the Always show solution check box is selected in the NIB: General, Projects and Solutions, Options Dialog Box.In the New Project dialog box, expand the Visual C# node or Visual Basic node, and then choose the Windows node.
At the top of the dialog box, choose .NET Framework 3.5 in the list of versions of the .NET Framework.
In the list of project templates, choose Class Library, name the project WebPartCommands, and then choose the OK button.
Visual Studio adds the WebPartCommands project to the solution and opens the default Class1 code file.
Delete the Class1 code file from the project.
Before you write code to create the extension, you must add code files and assembly references, and configure the project settings.
To configure the WebPartNodeExtension project
In the WebPartNodeExtension project, add four code files that have the following names:
SiteNodeExtension
WebPartNodeTypeProvider
WebPartNodeInfo
WebPartCommandIds
Open the shortcut menu for the WebPartNodeExtension project, and then choose Add Reference.
In the Reference Manager – WebPartNodeExtension dialog box, choose the Framework tab, and then select the check box for each of the following assemblies:
System.ComponentModel.Composition
System.Windows.Forms
Choose the Extensions tab, select the check box for the Microsoft.VisualStudio.SharePoint assembly, and then choose the OK button.
In Solution Explorer, open the shortcut menu for the WebPartNodeExtension project node, and then choose Properties.
The Project Designer opens.
Choose the Application tab.
In the Default namespace box (C#) or Root namespace box (Visual Basic), enter ServerExplorer.SharePointConnections.WebPartNode.
To configure the WebPartCommands project
In the WebPartCommands project, add a code file that's named WebPartCommands.
In Solution Explorer, open the shortcut menu for the WebPartCommands project node, choose Add, and then choose Existing Item.
In the Add Existing Item dialog box, browse to the folder that contains the code files for the WebPartNodeExtension project, and then choose the WebPartNodeInfo and WebPartCommandIds code files.
Choose the arrow next to the Add button, and then choose Add As Link in the menu that appears.
Visual Studio adds the code files to the WebPartCommands project as links. As a result, the code files are located in the WebPartNodeExtension project, but the code in the files are also compiled in the WebPartCommands project.
Open the shortcut menu for the WebPartCommands project again, and choose Add Reference.
In the Reference Manager – WebPartCommands dialog box, choose the Extensions tab, select the check box for each of the following assemblies, and then choose the OK button:
Microsoft.SharePoint
Microsoft.VisualStudio.SharePoint.Commands
In Solution Explorer, open the shortcut menu for the WebPartCommands project again, and then choose Properties.
The Project Designer opens.
Choose the Application tab.
In the Default namespace box (C#) or Root namespace box (Visual Basic), enter ServerExplorer.SharePointConnections.WebPartNode.
Create two icons for the Server Explorer extension: an icon for the new Web Part Gallery node, and another icon for each child Web Part node under the Web Part Gallery node. Later in this walkthrough, you will write code that associates these icons with the nodes.
To create icons for the nodes
In Solution Explorer, open the shortcut menu for the WebPartNodeExtension project, and then choose Properties.
The Project Designer opens.
Choose the Resources tab, and then choose the This project does not contain a default resources file. Click here to create one link.
Visual Studio creates a resource file and opens it in the designer.
At the top of the designer, choose the arrow next to the Add Resource menu command, and then choose Add New Icon in the menu that appears.
In the Add New Resource dialog box, name the new icon WebPartsNode, and then choose the Add button.
The new icon opens in the Image Editor.
Edit the 16x16 version of the icon so that it has a design that you can easily recognize.
Open the shortcut menu for the 32x32 version of the icon, and then choose Delete Image Type.
Repeat steps 5 through 8 to add a second icon to the project resources, and name this icon WebPart.
In Solution Explorer, under the Resources folder for the WebPartNodeExtension project, open the shortcut menu for WebPartsNode.ico.
In the Properties window, choose the arrow next to Build Action, and then choose Embedded Resource on the menu that appears.
Repeat the last two steps for WebPart.ico.
Create a class that adds the new Web Part Gallery node to each SharePoint site node. To add the new node, the class implements the IExplorerNodeTypeExtension interface. Implement this interface whenever you want to extend the behavior of an existing node in Server Explorer, such as adding a child node to a node.
To add the Web Part Gallery node to Server Explorer
In the WebPartNodeExtension project, open the SiteNodeExtension code file, and then paste the following code into it.
Note
After you add this code, the project will have some compile errors, but they'll go away when you add code in later steps.
Create a class that defines a new type of node that represents a Web Part. Visual Studio uses this new node type to display child nodes under the Web Part Gallery node. Each child node represents a single Web Part on the SharePoint site.
To define the new node type, the class implements the IExplorerNodeTypeProvider interface. Implement this interface whenever you want to define a new type of node in Server Explorer.
To define the Web Part node type
In the WebPartNodeExtension project, open the WebPartNodeTypeProvder code file, and then paste the following code into it.
Define a class that contains data about a single Web Part on the SharePoint site. Later in this walkthrough, you will create a custom SharePoint command that retrieves data about each Web Part on the site and then assigns the data to instances of this class.
To define the Web Part data class
In the WebPartNodeExtension project, open the WebPartNodeInfo code file, and then paste the following code into it.
Define several strings that identify the custom SharePoint commands. You will implement these commands later in this walkthrough.
To define the command IDs
In the WebPartNodeExtension project, open the WebPartCommandIds code file, and then paste the following code into it.
Create custom commands that call into the server object model for SharePoint to retrieve data about the Web Parts on the SharePoint site. Each command is a method that has the SharePointCommandAttribute applied to it.
To define the SharePoint commands
In the WebPartCommands project, open the WebPartCommands code file, and then paste the following code into it.
At this point in the walkthrough, all the code for the Web Part Gallery node and the SharePoint commands are now in the projects. Build the solution to make sure that both projects compile without errors.
To build the solution
On the menu bar, choose Build, Build Solution.
Warning
At this point, the WebPartNode project may have a build error because the VSIX manifest file doesn't have a value for Author. This error will go away when you add a value in later steps.
To deploy the extension, use the VSIX project in your solution to create a VSIX package. First, configure the VSIX package by modifying the source.extension.vsixmanifest file in the VSIX project. Then, create the VSIX package by building the solution.
To configure the VSIX package
In Solution Explorer, under the WebPartNode project, open the source.extension.vsixmanifest file in the manifest editor.
The source.extension.vsixmanifest file is the basis for the extension.vsixmanifest file that all VSIX packages require. For more information about this file, see VSIX Extension Schema 1.0 Reference.
In the Product Name box, enter Web Part Gallery Node for Server Explorer.
In the Author box, enter Contoso.
In the Description box, enter Adds a custom Web Part Gallery node to the SharePoint Connections node in Server Explorer. This extension uses a custom SharePoint command to call into the server object model.
Choose the Assets tab of the editor, and then choose the New button.
The Add New Asset dialog box appears.
In the Type list, choose Microsoft.VisualStudio.MefComponent.
Note
This value corresponds to theMefComponentelement in the extension.vsixmanifest file. This element specifies the name of an extension assembly in the VSIX package. For more information, see NIB: MEFComponent Element (VSX Schema).In the Source list, choose A project in current solution.
In the Project list, choose WebPartNodeExtension and then choose the OK button.
In the manifest editor, choose the New button again.
The Add New Asset dialog box appears.
In the Type box, enter SharePoint.Commands.v4.
Note
This element specifies a custom extension that you want to include in the Visual Studio extension. For more information, see Asset Element (VSX Schema).In the Source list, choose the A project in current solution list item.
In the Project list, choose WebPartCommands, and then choose the OK button.
On the menu bar, choose Build, Build Solution, and then make sure that the solution compiles without errors.
Make sure that the build output folder for the WebPartNode project now contains the WebPartNode.vsix file.
By default, the build output folder is the ..\bin\Debug folder under the folder that contains your project file.
You're now ready to test the new Web Part Gallery node in Server Explorer. First, start debugging the extension in an experimental instance of Visual Studio. Then, use the new Web Parts node in the experimental instance of Visual Studio.
To start debugging the extension
Restart Visual Studio with administrative credentials, and then open the WebPartNode solution.
In the WebPartNodeExtension project, open the SiteNodeExtension code file, and then add a breakpoint to the first line of code in the
NodeChildrenRequestedandCreateWebPartNodesmethods.Choose the F5 key to start debugging.
Visual Studio installs the extension to %UserProfile%\AppData\Local\Microsoft\VisualStudio\11.0Exp\Extensions\Contoso\Web Part Gallery Node Extension for Server Explorer\1.0 and starts an experimental instance of Visual Studio. You will test the project item in this instance of Visual Studio.
To test the extension
In the experimental instance of Visual Studio, on the menu bar, choose View, Server Explorer.
Perform the following steps if the SharePoint site that you want to use for testing doesn't appear under the SharePoint Connections node in Server Explorer:
In Server Explorer, open the shortcut menu for SharePoint Connections, and then choose Add Connection.
In the Add SharePoint Connection dialog box, enter the URL for the SharePoint site to which you want to connect, and then choose the OK button.
To specify the SharePoint site on your development computer, enter http://localhost.
Expand the site connection node (which displays the URL of your site), and then expand a child site node (for example, Team Site).
Verify that the code in the other instance of Visual Studio stops on the breakpoint that you set earlier in the
NodeChildrenRequestedmethod, and then choose F5 to continue to debug the project.In the experimental instance of Visual Studio, verify that a new node named Web Part Gallery appears under the top-level site node, and then expand the Web Part Gallery node.
Verify that the code in the other instance of Visual Studio stops on the breakpoint that you set earlier in the
CreateWebPartNodesmethod, and then choose the F5 key to continue to debug the project.In the experimental instance of Visual Studio, verify that all Web Parts on the connected site appear under the Web Part Gallery node in Server Explorer.
In Server Explorer, open the shortcut menu for one of the Web Parts, and then choose Properties.
In the instance of Visual Studio that you're debugging, verify that details about the Web Part appear in the Properties window.
After you finish testing the extension, uninstall the extension from Visual Studio.
To uninstall the extension
In the experimental instance of Visual Studio, on the menu bar, choose Tools, Extensions and Updates.
The Extensions and Updates dialog box opens.
In the list of extensions, choose Web Part Gallery Node Extension for Server Explorer, and then choose the Uninstall button.
In the dialog box that appears, choose the Yes button to confirm that you want to uninstall the extension, and then choose the Restart Now button to complete the uninstallation.
Close both instances of Visual Studio (the experimental instance and the instance of Visual Studio in which the WebPartNode solution is open).
Extending the SharePoint Connections Node in Server Explorer
Walkthrough: Calling into the SharePoint Client Object Model in a Server Explorer Extension
Image Editor for Icons
Creating an Icon or Other Image (Image Editor for Icons)