Saturday, September 22, 2012

Use VS 2010 User Controls with VS 2008 and SharePoint

The Microsoft Visual Studio .NET logo.
The Microsoft Visual Studio .NET logo.
(Photo credit: Wikipedia)

How to register an assembly of newer VS 2010 into older VS 2008 Code

I use a project template that allows creation of Web Forms and Controls with the visual designer, is configured for WSPBuilder and code signing, and has some standard assembly references. If you aren't interested in the details of how to make your own, you can just download mine from here (http://www.elumenotion.com/Downloads/WSPTemplate.zip) and place it into yourDocuments\Visual Studio 2008\Templates\ProjectTemplates folder or you can keep reading!   A Visual Studio project template defines the base configuration of a new project. If you've ever used VS, you have used a project template. When I am starting from scratch, I usually use the C# Class Library project template because it makes the fewest assumptions about what I am doing and therefore imposes the fewest restrictions. One problem with this approach is that the Class Library template lacks the item templates for ASP.NET. Among these are the templates for Web Forms and User Controls. I like the productivity tools provided by these item templates, especially the visual designers. Fortunately, it is easy to add the Web Form and User Control item templates to a Class Library!   In Solution Explorer, right-click the project node and choose Unload Project to expose the project file for editing and then edit the file.  
The file opens in the editor. Insert the <ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids> element beneath <ProjectGuids> element and save the file.
    Right-click the project in Solution Explorer and select Reload Project. Now when you add a new item to the project you have the option to include any of the standard ASP.NET Web items.   I use the WSPBuilder tool to create web solution packages that I can install and deploy on SharePoint farms. WSPBuilder is simple to use, but it requires that the project files are in folders within the project that match the deployment target's structure (the 12 hive). The next step is to create the folders shown below.  
    When you run WSPBuilder it creates a solution manifest based on the files that are actually in the project. So, if I don't include any files in a particular folder, the presence of the folder in the solution won't affect the package. So, I usually just start out with the set of folders I am most likely to use ahead of time.   Most projects require a few feature receivers or other code that has to deploy to the Global Assembly Cache. This requires that the assembly has a strong name, which in turn requires a strong-name key. The next step is to configure code signing. You can read more about that process here: http://msdn.microsoft.com/en-us/library/ms247123.aspx.   WSPBuilder will also specify in the solution manifest that the assembly should deploy to the GAC if it finds it in a GAC folder beneath the project root. To enable this create the folder in the project and set the output path. To learn how to do this, see:http://msdn.microsoft.com/en-us/library/kb4wyys2.aspx. At this point, the project structure looks like the following:  
    Finally, before I can write any code, I need to add references to the SharePoint and various System.Web assemblies.  
    That's a lot of work to go through every time I want to create a new solution. In a team environment over the course of a year you could waste days doing this by hand. Worse, different members of the team might do things differently resulting in confusion for all. If you save the result as a project template you can avoid ever having to do these steps again. You can also share the template with your teammates. To export the project as a template, select File|Export Template and complete the wizard. The result is a ZIP file. If you give it to a teammate and they put it into their Documents\Visual Studio 2008\Templates\ProjectTemplates folder, they will have it as an option when they create a new project.  
You can download the result here: http://www.elumenotion.com/Downloads/WSPTemplate.zip    

No comments:

Post a Comment