This is a guide that will take you through steps in creating a code behind file in Visual Studio for SharePoint development. Full credit to Andrew Connell on his blog entry which demonstrates this process by manually creating files for the deployment process.
The difference with this article is that I wanted to complete the process using Microsoft’s Visual Studio Extensions WSS 1.1 (VSeWSS1.1) tool. Why, because I think this is the tool that should be used when doing any SharePoint development due to its integration with VS.Net, variety of templates, debugging integration, nice wsp viewer, auto generation of features and solutions, and it will continue to evolve over time.
1/ First we need a Content page (.aspx) that will host our ASP.Net controls. Content pages need to conform to SharePoint master pages PlaceHolderContols. So a good place to create one is in SharePoint Designer.
To create a Content page for SharePoint you can open SharePoint Designer and select File –> New –> Create from master page.This will create a blank content page as shown:
This page has no asp Content controls so you can go and add one.
<asp:Content ContentPlaceHolderId=”PlaceHolderMain” runat=”server”></asp:Content>
Inside the content control you can add html markup as shown below
3/ Copy the html (ctrl-A) from SharePoint Designer into notepad and save the physical file as CodeBehindDemo.aspx.
4/ Open up Visual Studio and create a new Project and select the Empty SharePoint project and name it accordingly (Note: you need to install VSeWSS1.1 to get these templates).
5/ Once the Project is created right click the project and select Add New Item and select the Module template.
6/ Copy the CodeBehindDemo.aspx to the Module1 folder so it looks like the image below. Then create a new class file called CodeBehindDemo.cs. This will obviously be our code behind file.
7/ Write the following code in the CodeBehindDemo.aspx page. We need to explicitly declare the asp.net controls in the class file as the .aspx and .cs files are not compiled into a single (partial) class.
8/ Back in our .aspx page we need to reference our code behind class with the following page directive.
<%@ Page Language=”C#” masterpagefile=”~masterurl/default.master” title=”|”
inherits=”SharePointCodeBehindPages.CodeBehindDemo, SharePointCodeBehindPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5″ meta:webpartpageexpansion=”full” meta:progid=”SharePoint.WebPartPage.Document” %>
Note: Check your public key token as yours will be different once you sign the assembly.
9/ This is the code inside the Module.xml file.
<?xml version=”1.0″ encoding=”utf-8″?>
<Elements Id=”581c7ebb-03bd-4d7d-ad4a-4e7ff446d2ac” xmlns=”http://schemas.microsoft.com/sharepoint/”>
<Module Name=”Module1″>
<File Path=”CodeBehindDemo.aspx” Url=”CodeBehindDemo.aspx” Type=”Ghostable” />
</Module>
<CustomAction Id=”A6DED79F-05A5-4b1a-BD27-10B1A4AAC737″
Location=”Microsoft.SharePoint.StandardMenu”
GroupId=”SiteActions”
Sequence=”99″
Title=”Code Behind Demo”
Description=”Code behind example”>
<UrlAction Url=”~site/CodeBehindDemo.aspx” />
</CustomAction>
</Elements>
10/ This is the code that is inside the Feature.xml file
<?xml version=”1.0″ encoding=”utf-8″?>
<Feature Id=”39fd665e-e168-42fe-ab11-ebea99e0a378″ Title=”Module1″ Scope=”Web” Version=”1.0.0.0″ Hidden=”FALSE” DefaultResourceFile=”core” xmlns=”http://schemas.microsoft.com/sharepoint/”>
<ElementManifests>
<ElementManifest Location=”Module1\Module.xml” />
<ElementFile Location=”Module1\CodeBehindDemo.aspx” />
</ElementManifests>
</Feature>
11/This is the code inside the manifest file
<?xml version=”1.0″ encoding=”utf-8″?>
<Solution SolutionId=”e8fa762e-9f02-4646-9fde-cf17ed7288d6″ xmlns=”http://schemas.microsoft.com/sharepoint/”>
<FeatureManifests>
<FeatureManifest Location=”Module1\feature.xml” />
</FeatureManifests>
<Assemblies>
<Assembly DeploymentTarget=”WebApplication” Location=”SharePointCodeBehindPages.dll”>
<SafeControls>
<SafeControl Namespace=”SharePointCodeBehindPages” Safe=”True” Assembly=”SharePointCodeBehindPages” TypeName=”*” />
</SafeControls>
</Assembly>
</Assemblies>
</Solution>
12/ Before we create and deploy the solution first build the project, make sure you don’t receive any errors and then check the new WSP View in Visual Studio to make everything is fine. You made need to click the Refresh button to display the wsp structure. One of the nice features about the extensions is that it is integrated with Visual Studio so if one of the .xml files has an error then it is displayed in the Error List pane and nothing is displayed in the WSP View, it is just blank. Remember to click the refresh button.
13/ Now all that is needed is to deploy the Solution (.wsp) to the farm. Very simple, right click the project and select Deploy.
14/ Make sure everything is successfully built and deployed
15/ Once built successfully you can view the .wsp file which is located in the projects bin/debug folder
16/ Ok, now lets go and test the page in SharePoint. We have a nice Custom Action we can click that will take us to the page.
Enter you name and click the button.
16/ So there you have it. A code behind page built in Visual Studio all wrapped up in a nice solution (.wsp) for deployment across the farm using the new Visual Studio Extensions for WSS 1.1 tool.