VSM Cover Story

Beat SharePoint Into Shape

Visual Studio 2005 extensions for Windows SharePoint Services 3.0 simplify the process of incorporating SharePoint into your Visual Studio applications.

C#, SharePoint 2007(WSS and MOSS)

SharePoint is Microsoft's collaboration tool that enables users to publish and share information.The tool provides the infrastructure required to authenticate users and regulate the authorization and access to each page, library, and element. Microsoft launched Microsoft Office SharePoint Services (MOSS) 2007 in January, implementing a series of changes and improvements in SharePoint's usability and functionality.

SharePoint is a complete development platform. It lets you customize its interface based on your users' requirements, and you now have the option of using SharePoint as the host for new application types. SharePoint 2007 features an expansive object model (OM) API that extends to all components of the server and offers access to libraries, documents, and the properties of each element. The OM is fully compatible with ASP.NET 2.0 (and 3.0), and you can program against it using Microsoft Visual Studio .NET 2005 and C# specifically.

To help you develop SharePoint applications, Microsoft has created Visual Studio 2005 extensions for Windows SharePoint Services 3.0, Version 1.0 (see Additional Resources for details on downloading these extensions). This free set of extensions provides project templates, item templates, and a solution generator, but it works only with Visual Studio 2005 at this time; SP1 is recommended, but not obligatory.

Installation of the extensions is straightforward, requiring only that you accept the license conditions and configure the local installation directory. After the installation is complete, Visual Studio displays a SharePoint project type item with several new templates (see Figure 1).

The extensions include project templates to construct a Team Site Definition, a Blank Site Definition, a List Definition, and Web Part projects. The templates are downloaded each time you start the server, and they're used in each request. SharePoint saves the content data in the database and, when a user requests a page, plugs Site Definition into the specific content data and renders it to the client. This explanation somewhat oversimplifies a complicated process, but this is the basic approach SharePoint uses to make its magic happen. You can take advantage of the Team Site Definition and List Definition templates to create as many SharePoint templates as required.

The Visual Studio templates make it possible to develop the code immediately. They configure the essential references, craft the directives (using using in C#, or Imports in Visual Basic), create the correct inheritances for the class, generate GUIDs (if desired), and create a base initialization method. For example, SharePoint generates a .cs page if you select a Web Part template (see Listing 1).

The extensions also install item templates you can use from within a project. You access these templates from Visual Studio's Solution Explorer and from the context menu of the project by navigating to Add | New Item. You can aggregate List Definition, Content Type, Web Part, Custom Field, and Module templates to the project.

SharePoint 2007 introduces the novel concept of Content Type templates, which comprise a collection of various information and control elements that you can use as a unit to constitute a Content Type. For example, imagine a contract with metadata for names, dates, and values and a WorkFlow (created with Windows Workflow Foundation) that commands the acceptation procedure. Together, you can convert them into a SharePoint Content Type.

SharePoint uses special XML configuration files defined in Collaborative Application Markup Language (CAML). The Module item template lets you create the Module element of the configuration file. The Custom Field class inherits from the SharePoint SPFieldText class, which represents a field that contains a particular line of text in a SharePoint list or library; you can then use this Custom Field to develop new field types within SharePoint.

Modify the Templates
As with Visual Studio templates, the SharePoint extensions templates are saved as plain-text files in your file system, and you can edit them freely. For example, the Web Part template contains the Render method, but you usually need to employ an override of the CreateChildControls method to create the controls for usage in a more "elegant" way. Additionally, it's convenient if the definition of a property, complete with all its parameters, is also available.

To modify the original Web Part template, go to the C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplatesCache\CSharp\SharePoint directory for the C# templates (the Visual Basic templates are in the corresponding directory). This directory includes a separate directory for each template, and the WebPart.zip directory contains the WebPart1.cs file with the definition of the template for the Web Parts. You can modify the file with any ASCII text editor, or you can transcribe the changes with Visual Studio (see Listing 2).

This code defines the example for the property (commented) and the new method. Before you can save the file, you need to change its read-only attribute because the files in these directories are read-only by default.

The SharePoint Web Part template requires a configuration file (with the .webpart extension). This file isn't included in the extensions' template, but you can add it so that you don't need to create it each time. Modify the WebPart.vstemplate file to add the configuration in the <Project> section:

<ProjectItem ReplaceParameters=
"true" TargetFileName=
"WebPart1.webpart">WebPart1.webpart</ProjectItem>

Add this line to WebPart.csproj:

<None Include="WebPart1.webpart" />

Next, add this code under the row where you add the Temporary.snk file:

("<None Include=
"Properties\Temporary.snk" />")

It requires only a few lines of code to manipulate the WebPart1.webpart file (see Listing 3).

After you create and compile the project, you must replace the "YourPublicKeyToken" string with the generated token, because it was unknown at the time of creation. You can find the token using the sn tool with the T parameter. The next time you use the Web Part template, the project will include the .webpart file and the modifications made in the WebPart1.cs file.

Use the SharePoint Solution Generator
The SharePoint extensions include a Windows application called SharePoint Solution Generator that lets you create SharePoint projects from existing Team Site Definition and List Definition projects.

In previous versions of SharePoint, deploying newly developed functionality to the production servers was a problem. After you made the necessary changes and customizations, you either had to copy the files, definitions, and assemblies to test and production servers, or you had to make a custom installation program. The SharePoint Solution Generator, which Microsoft introduced with SharePoint 2007, lets you make all types of changes and customizations in SharePoint and deploy them in a unified way. It also gives system administrators a way to deploy any changes with minimal effort, maintaining the benefits of versioning. Users can return to a previous version without the risks inherent in copying files, and users can replicate all the changes in every server of the farm in one movement.

Assume you want to create a new Team Site Definition project for SharePoint. You can generate the project with one of the default templates and customize it by applying the lists, libraries, and Web parts of choice.

For additional customizations not available from the default configurations of SharePoint, look to the new SharePoint Designer, a graphical tool that lets you make changes to the interface. You can use SharePoint Designer to create basic Workflow documents and make use of the specialized Web Parts. SharePoint Designer--formerly called FrontPage and a member of the Office family--is envisioned as a tool that can let users without specialized technical skills create adaptations to the SharePoint system.

If you want to change the functionality and make other modifications not available from SharePoint or SharePoint Designer, or if you need to program specific Team Site Definition or List Definition modifications, you can use the SharePoint Solution Generator to export the entire site--including the modifications made with SharePoint Designer--to a Visual Studio project. At this stage, you can implement the custom enterprise modifications of choice.

After installing the extensions, go to Start | All Programs | SharePoint Solution Generator to link to the generator. You can choose between the creation of a Team Site Definition or a List Definition. If you choose Team Site Definition--whether local or remote--you can generate the solution. If you choose List Definition, you can select one or more lists from the selected site. The final screen lets you select the project name and location.

You can follow the entire SharePoint Solution Generator process onscreen. After a few seconds (or minutes, if the site is large), the application analyzes the definitions and properties, produces copies of each found file, generates the provisioning code, creates a Visual Studio project, and organizes a cleanup of the temporary files (see Figure 2).

Customize Your Project
You're now ready to make all your desired changes and customizations. All the necessary files are present, including the XML definition files (onet.xml and schema.xml) and auxiliary documents. The project contains a partial class, SiteProvisioning.cs, under the Site Provisioning Handler. (This class is used exclusively for Site Definition generation; you can provision List Definition projects only with a feature.) Use the class to facilitate the installation in SharePoint (complemented by SiteProvisioning.Internal.cs, the second part of the partial class), where you can define the necessary activation actions. (Note: This isn't obligatory.)

Finally, construct the project from the Build menu (it compiles the necessary assembly) and deploy it in SharePoint Server. Visual Studio generates the SharePoint Solution and deploys it directly in the local instance of SharePoint (assuming your computer has SharePoint 2007 installed; if not, only the Solution will be built). You can find the Solution files in a subdirectory in the bin-Debug or bin-Release directory of the project. The actual Solution is a file with a WSP extension located in the bin directory and is ready to be applied to any SharePoint installation (SharePoint administrators can use the stsadm tool to install the solutions, and the Central Administration windows of SharePoint to delete installed Solutions). Visual Studio projects created with the templates for Web Part, List Definition, and Team Site Definition also construct solution files in a similar way.

You'll find a SharePoint Solution tab in the properties of the Visual Studio project. Here you can configure the components of each Solution's element (such as the names of the features, the title, the descriptions, and the icons). SharePoint Solutions includes a collection of features, a method SharePoint 2007 utilizes to install and activate functionality. You can find all the required features in the Solution directory.

When combined with the Visual Studio SharePoint extensions, Solutions, and features help to simplify your work. The project templates, item templates, and solution generator help you implement new functionality and avoid repetitive implementation tasks. The ability to adapt the default templates to your specific needs helps to increase reliability and ease of programming.

About the Author

Gustavo Velez is an MCSD senior application developer for Winvision, a Microsoft Gold Partner in the Netherlands. He has many years experience developing Windows and Office applications and more than four years of daily programming experience with SharePoint. You can find Gustavo's articles in many of the leading trade magazines. He's pleased to be the Webmaster of SkunkWorks, the only Spanish-language SharePoint site.

comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.