Friday, May 28, 2010

Developing/Deploying ASP.NET Web Application in SharePoint 2007 under _layouts folder

Hi Folks,

This post i will walkthrough the steps required to create and deploy asp.net web application under _layouts folder in sharepoint 2007. Also note that this application is different than the MSDN article to build asp.net 2.0 website in sharepoint 2007, since this article only works if you copy the whole project ( with .cs files ) under website folder in layouts directory which is not recommended and not a best practice.

In this article i will go through the steps to create a web application with sharepoint master page and to deploy the web application dll in GAC as well.

1) Open your VS 2008, and select ASP.NET web application template.
2) Add Microsoft.Sharepoint.dll in your project references.
3) Create a folder in your project called _layouts, the purpose of this project is to have a dummy masterpage that let you develop your custom pages with sharepoint site master page and the pages will pick the site masterpage at runtime.
4) To get a dummy application.master, get it from this link:
http://cid-4bc94054914a6469.skydrive.live.com/self.aspx/Blog%20Code/application.master

5) Right click on your project to sign your assembly of the web application, since this dll will be in GAC.

6) Open the default.aspx page that you have and update the following:



<%@ Assembly Name="<span style="font-weight:bold;">ASSEMBLYNAME</span>, Version=1.0.0.0, Culture=neutral, PublicKeyToken=<span style="font-weight:bold;">b1be248cde129fd5</span>" %>

<%@ Page Language="C#" AutoEventWireup="true"  Inherits="<span style="font-weight:bold;">NAMESPACE</span>._Default" MasterPageFile="~/_layouts/application.master" %>



<asp:Content ID="Content1" ContentPlaceHolderId="PlaceHolderMain" runat="server">

    <div>

    Title of this site: <asp:Label ID="LabelTitle" runat="server" Text="Label">

    </asp:Label>

    </div>

</asp:Content>

<asp:Content ID="Content2"

ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">

Test My ASP.NET Web Application

</asp:Content>




To download the sample page, here is the link:
http://cid-4bc94054914a6469.skydrive.live.com/self.aspx/Blog%20Code/home.aspx


Note: all your pages have to have 3 content controls that mimics the contentplaceholder that you have on your custom masterpage or sharepoint out of the box master page.

Tip: to get the public key token, just drag and drop your dll in GAC and right click on your assembly in your gac folder ( c:\windows\assembly) and you will get the public key token that you need to paste on all your pages.

7) To change the master page to your pages to the sharepoint master page, write the following method on your .cs file of your page:

protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);

SPWeb Web = SPContext.Current.Web;
this.MasterPageFile = Web.MasterUrl;
}



8) One you are done, publish your website using publishing feature on your web application, right click on the web application and click on publish.

set the url of the web application to be under LAYOUTS folder in sharepoint.


9) Last, Deploy the web application dll file in GAC, you can create a feature for that, for development purposes, just drag and drop the webapplication.dll in your GAC under c:\windows\assembly folder.


10) Navigate to your test page and will see it works perfect, url for example :
http://SHAREPOINTSITE/_layouts/WEBSITEFOLDER/home.aspx


Hope this helps.

Regards,
Mostafa Arafa
Post a Comment