Monday, August 11, 2008

How to ADD Custom ASp.Net Page to MOSS Site

How to Add a Custom ASP.NET Application into a Site Template

How to Add a Custom ASP.NET Application into a Site Template

Posted on 5/6/2005 9:36 PM by pBoy

Every now and then there are circumstances where you would like to add a custom ASP.NET page to become part of a WSS or SPS site. How do you do it? Can you do it? Happily, the answer is yes.

It takes some careful effort to put all the pieces together but you can incorporate your ASPX pages and custom assembly so that it looks like it’s part of an out of the box SharePoint site. Below are the steps for doing this for a WSS site; they are very similar for an SPS Area (close enough that I’ll leave it as an exercise for the reader):

1. Create your custom ASP.NET application – add your pages and compile into an assembly (.DLL). Very Important: Your web pages MUST inherit from Microsoft.SharePoint.WebPartPages.WebPartPage if you want them included as part of a site. However you can still use most of the standard ASP.NET techniques to build your pages, add custom ASP.NET controls, do data-binding, etc.

2. Create a custom site definition – the steps to do this are beyond the scope of this blog, but there have been many articles published that describe how to do this. To customize WSS sites you’ll want to copy the contents of the STS folder in the Templates/LCID directory (where LCID is the language code of your installation, such as 1033 for US English).

3. Modify ONET.XML - add this section to the Modules section at the bottom:



In this case the Url attribute of the File element is going to be the name of the ASPX page. If you have multiple pages then you will need to create multiple File elements.

4. Modify the Module section for the template in which you want the page included. For example to include your page when someone creates a new team site based on the Team Site template:


...




Each template type has its own Configuration section; you need to customize the Modules sub-element for each template that should contain your custom page.

5. Put the assembly for your custom page in the appropriate BIN directory, i.e. C:\Inetpub\wwwroot\bin. The location of the bin directory will vary depending on how your virtual server is configured. Also a bin directory does not exist by default; if that’s the case then just create one and copy the assembly into it.

6. Open a command prompt and run IISRESET. This is necessary because ONET.XML is cached by IIS so it needs to be flushed in order to pick up the changes you made.

Once you’ve completed all of these steps you can create a new site based on your updated template. Once your site is created you can navigate to the page directly in the site. For example if you add a page called steve.aspx to a template and create a site at http://portal/sites/monkey, you can navigate to the page at http://portal/sites/monkey/steve.aspx.

It’s an interesting technique and certainly not needed often, but it’s good to understand the required steps in case you ever need to go down that path.

How to integrate ASP.NET codes inside SharePoint

You definitely won't be able to do code-behind in SharePoint. However, here is a workaround to get inline code to work:

- "An error occurred during the processing of xxx.aspx. Code blocks are not allowed in this file."
- "This page has encountered a critical error. contact your system administrator if this problem persist."

Example inline code
==================================
Open up SharePoint Designer and the masterpage you want to edit. Switch to the code view and anywhere from the first tag down you can add some inline c# code:



This shows how you can use the SharePoint object model in your inline code. If you saved the master page now and opened the site in your browser you would get an error about not being able to run code blocks (listed above). There’s an extra line you need to put into your web.config file first to allow this inline c# to execute.

Modify the web.config file
==================================
1. Open the web.config file and make the following change:





The PageParserPaths xml tags will be there already you just need to add the line in between.

2. Save web.config .
3. Start -> Run -> iisreset /noforce , click OK.

Note: I would like to point out that making this change is possibly not good practice. The fact that CompilationMode being set to "Always" points out that there may be a performance hit and caching won’t exactly work well.

Integrating ASP.NET 2.0 Web Pages into SharePoint 2007

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...

  1. 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.
  2. 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.

Write 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}:\Inetpub\wwwroot\wss\VirtualDirectories/{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 Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS

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 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.

Converting an ASP.NET site into a SharePoint site

Introduction

There are a lot of ASP.NET web developers who are moving to SharePoint site creation. This article will explain in detail how an ASP.NET webpage developed in Visual Studio can be converted into a SharePoint site. If there is a requirement for a website created in Visual Studio, just the old fashioned way with the code-behind logic and other layers like Data Access and Business Logic, to be converted into a SharePoint site, and still make it work the same way with the same code-behind, you are in the right place. This article deals with right that.

Scenario

There is an ASP.NET website solution that contains three layers viz. Code-Behind Layer, Business-Logic Layer, and the Data-Access Layer. The website has functionality implemented in all these layers. The Business-Logic and the Data-Access layers are in a different Class Library project. We have to convert this website into a SharePoint site. Also, we want to use the same look and feel of the SharePoint site. So, we have to use the SharePoint master page instead of the one that we are having (we can also use our own master page; just that you have to add some default Place Holders that are required for the SharePoint functionalities). In this article, we are dealing with a website with the same look and feel as a SharePoint site.

Steps Overview

There are three steps that are explained in the article which will help you to transform your ASP.NET Web Application into a SharePoint site.

Step1: Add a Web Deployment Project to your website solution that will create a single DLL for the website. This step will give us the code-behind DLL and other dependency DLLs for the website.

Step2: Copy the bin folder items (DLLs) into the SharePoint site, and tweak the web configuration file of the SharePoint site to use the copied DLLs.

Step3: Open your created SharePoint site in SharePoint Designer and import the web pages from our web application and link it to the appropriate DLLs.

Step 1: Adding a web deployment project to your website solution that will create a single DLL for the website

  1. The first step is to make sure you have added the proper namespaces and avoided class name duplications within a namespace before going to step 2.
  2. Add a web deployment project into your website solution. This can be done by right clicking on the website project and choosing 'Add Web Deployment Project' option.

Note: This option will be available once you have installed the Web Deployment Setup.

  1. Add a strong name key to the solution which we will be using to sign all the DLLs.
  2. Now, we have to set the deployment project properties. Right-click on the deployment project and click 'Property Pages'.
  3. Go to the 'Output Assemblies' tab, and choose the 'Merge all assemblies to single assembly' option (this is the default option), and give the assembly name.

Screenshot - output_assembly-Pic1.jpg

  1. Next, go to the Signing tab and choose the 'Enable strong naming' option, and choose the strong name key that you have created in step 3.

Screenshot - signing-pic2.jpg

  1. Also check the option 'Mark the assemblies with AllowPartiallyTrustedCallersAttribute (APTCA)'. This will make the DLL partially trusted, and thus the SharePoint site can use them. Click on OK.
  2. The Data Access, Business Logic, or any other assemblies that are a dependency to the web application must be strong named with a strong name key.

Screenshot - assemblyinfo-pic3.jpg

  1. Also, we have to allow partially trusted callers for the dependency DLLs. This can be done by opening the Assembly.info file of the Class Library project and putting the following line of code as shown above:
[assembly: System.Security.AllowPartiallyTrustedCallers]
  1. Build the deployment project under Release mode (any mode).
  2. Go to the path where the deployment project has put the output. Here, in the bin directory, you will find the web deployment DLL file. If there are any dependency projects such as the Business Layer and the Data-Access Layer, those DLLs also will be copied to this bin folder.

Now, we have the DLLs that can be used in our SharePoint site for using the same functionality as in our ASP.NET site.

Step 2: Copy the bin folder items (DLLs) into the SharePoint site and tweak the web configuration file of the SharePoint site to use the copied DLLs

The next step in our SharePoint site creation is linking the DLLs that we have created in the procedure above into our already existing blank SharePoint site. There are also some changes that are required in our SharePoint site's web.config file (By default found in C:\Inetpub\wwwroot\wss\VirtualDirectories\). Following are the steps that has to be done:

  1. Copy the bin folder contents from your ASP.NET deployment folder into the bin folder of your SharePoint site (usually in C:\Inetpub\wwwroot\wss\VirtualDirectories\\bin).
  2. Open the web.config file of your SharePoint site.
  3. Add the following line in under the <PageParserPath> section:
<PageParserPath VirtualPath="/*" CompilationMode="Always" 
   AllowServerSideScript="true" IncludeSubFolders="true" />
  1. Register the assemblies that will be used in the SharePoint site (web deployment DLL which has the code-behind code, the Business Layer and Data Access DLLs) as a SafeControl. For this, add the following under the <SafeControls> section:
<SafeControl Assembly="SampleWebSiteAssembly" 
   Namespace="SampleSiteNamespace" TypeName="*" Safe="True" />
  1. Also add all the other dependency DLLs that your site will be using. Note that all these DLLs must be strong named and marked AllowPartiallyTrusted.
  2. Change the trust level from Minimal to Medium by changing the level attribute of the <trust> section from 'WSS_Minimal' to 'WSS_Medium'.

Note: You can also do the following to enable the original errors being shown in the SharePoint site screen for easy error identification. You have to change the mode attribute of the <customErrors> section to Off. Also, change the CallStack attribute of the <SafeMode> section to True.

Step 3: Open your created SharePoint site in SharePoint Designer and import the web pages from our web application

Let us say we have a link in our SharePoint site in the left navigation panel, on the click of which you want to display one of your ASP.NET pages in the content place holder of the SharePoint site. This is what we do:

  1. Open your SharePoint site in the SharePoint Designer (in this article, we are using SharePoint Designer 2007).
  2. Click on File-->Import-->File. This will open the Import dialog box.
  3. Click on 'Add File' and choose the ASPX page that you have created from your local folder. Please note that you have to take the ASPX file from the deployment folder and not the ASPX page that is there in the project. Click OK. This will import the page into your SharePoint site.
  4. Now, double-click on the newly imported page. Click on the Split option in the Design/Split/Code option in the centre pane (bottom left of the pane).
  5. As soon as you do this, you will see in the designer window an error that says: The Master Page file cannot be loaded. This is because the master file that we have used in the project is different from the master page that the SharePoint site uses. You can either import the master page or use SharePoint's default master page. In this article, we are going to use SharePoint's default master page.
  6. Change the 'MasterPageFile' attribute in the Page directive of the web page to a value same as the default.aspx in the SharePoint site, which is ~masterurl/default.master.
  7. Delete the 'CodeFile' attribute from the Page directive as this is only for Visual Studio purposes.
  8. Now, change the ContentPlaceHolderID of the place holders in the ASP.NET page to a relevant SharePoint site place holder. For example, the ContenPlaceHolderID of the main content place holder of the ASP.NET page must be changed to 'PlaceHolderMain'.
  9. After mapping the place holders of the ASP.NET page to that of the SharePoint master page, the page will render in the design view with the default master page.
  10. Now, we have to change the Inherits attribute to add the assembly name. For example, if the namespace is 'SampleSiteNamespace' and the assembly name that the page uses for code-behind is SampleWebSiteAssembly, then we set Inherits="SampleSiteNamespace.SampleWebSiteAssembly", and this assembly must be in the bin of the SharePoint site as added in Step 2 above.

Now, we have our ASP.NET site as a SharePoint site, ready to run with the same look and feel of the SharePoint site