One of the most useful features for developer’s in SharePoint 2007 is the ability integrate ASP.NET 2.0 pages into SharePoint 2007.
Consider the following scenerios…
-
A company has one or more stand alone ASP.NET 2.0 applications that uses a data store other than SharePoint. They would like to integrate it into SharePoint but don’t want to redo the application to use SharePoint’s data store. You could modify the application so it loads up inside SharePoint using the correct SharePoint master page. The application can still pull it’s information from any data store it wants to it will only be using SharePoint to host the application.
-
An application is written where all the data is stored in SharePoint but you want a specialized display of it. You could create an ASP.NET 2.0 page that loads the data from SharePoint and then display it however you like.
Prerequisites
Microsoft.SharePoint.dll
SharePoint’s .NET dll that contains the SharePoint objects. At a minimum, this is needed to use the correct SharePoint master page. This will be located on the machine that has SharePoint installed. If your development machine does not have SharePoint installed, you will need to find this dll and copy it to s folder local on your machine.
Visual Studio Web Deployment Projects Add-In Installed
You will need to download and install a free Visual Studio add-in that enables an ASP.NET 2.0 website to be compiled to a single dll. You will need to compile your website to a single dll to deploy the assembely to SharePoint.
The Visual Studio 2005 add-in can be found at http://msdn2.microsoft.com/en-us/asp.net/aa336619.aspx. The Visual Studio 2008 add-in can be found at http://www.microsoft.com/downloads/details.aspx?FamilyID=0AA30AE8-C73B-4BDD-BB1B-FE697256C459&displaylang=enWrite Permissions to the Bin Folder of the SharePoint Site
You will need to be able to deploy your website’s assembely to site’s bin folder. This is typcially located at the {Local_Drive}:InetpubwwwrootwssVirtualDirectories/{Port or site}/bin
Write Permissions to the SharePoint’s Layouts Folder
You will need to be able to deploy your aspx pages to SharePoint’s Layout folder. This is typically located at {Local_Drive}:Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATELAYOUTS
Create the ASP.NET Page
Create the ASP.NET Website Project
In Visual Studio 2005, create a new website project. Right click on the project in solution explorer and select add a new item. Then choose web form and name it something that makes sense to the project.
Changes to the ASPX Page
Replace the system generated contents of the file with the following.
<%@ Page Language=”C#” MasterPageFile=”" AutoEventWireup=”true” CodeFile=”example.aspx.cs” Inherits=”example” Title=”Example “ %> <asp:Content ID=”contentMain” ContentPlaceHolderID=”PlaceHolderMain” Runat=”Server”>
//Page markup goes here
</asp:Content>
The 2 most important parts of this code are the MasterPageFile=”" in the Page directive and the ContentPlaceHolderID=”PlaceHolderMain” attribute in the <asp:Content> tag.
Changes to the Code Behind File
Add the following using statements.
.
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
Override the pages OnPreInit method. This is where you will tell it to use the SharePoint master page. It should look like the code sample below.
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
SPWeb web = SPControl.GetContextWeb(Context);
MasterPageFile = web.CustomMasterUrl;
}
Deploying the Project
Right click on the project in solution explorer and select `Add Web Deployment Project.’. Make note of the location you specify in the dialog that appears. This is where you will get the files you need to copy to the machine running SharePoint.
Right click on the new deployment project and hit rebuild. This will compile your website to a single dll (not including any other dll’s you might of referenced) and place the whole website to the location you specified when you created the web deployment project.
Copy all the dll’s from the location you specified’s debug/bin or release/bin (depending on how you built your site) folder (excluding the Microsoft.SharePoint.dll) and paste them to the bin folder for the SharePoint web application on the machine running SharePoint. Note: The dll’s could also be deployed to the GAC so all SharePoint web applications on the server can use the pages. This would require signing the dll.
Copy the example.aspx from the location you specified’s debug or release (depending on how you built your site) folder and paste them to the layouts folder under where you installed SharePoint.
Viewing the Page in SharePoint
From the site you installed the dll’s in and all of the child sites created in that site, you can access your pages by going to http://{SiteName}/ _layouts/pageName.aspx.
Tags: ASP.NET, Coding, Development, Web Page
I Love You Man .
You Saved me Day.
Great Article.
Thanks for posting this!