Thursday 29 May 2014

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.
  3. Note: This option will be available once you have installed the Web Deployment Setup.
  4. Add a strong name key to the solution which we will be using to sign all the DLLs.
  5. Now, we have to set the deployment project properties. Right-click on the deployment project and click 'Property Pages'.
  6. 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.
  7. Screenshot - output_assembly-Pic1.jpg
  8. 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.
  9. Screenshot - signing-pic2.jpg
  10. 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.
  11. 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.
  12. Screenshot - assemblyinfo-pic3.jpg
  13. 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:
  14. [assembly: System.Security.AllowPartiallyTrustedCallers]
  15. Build the deployment project under Release mode (any mode).
  16. 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\<PortNo>). 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\<PortNo>\bin).
  2. Open the web.config file of your SharePoint site.
  3. Add the following line in under the <PageParserPath> section:
  4. <PageParserPath VirtualPath="/*" CompilationMode="Always" 
       AllowServerSideScript="true" IncludeSubFolders="true" />
  5. 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:
  6. <SafeControl Assembly="SampleWebSiteAssembly" 
       Namespace="SampleSiteNamespace" TypeName="*" Safe="True" />
  7. 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.
  8. Change the trust level from Minimal to Medium by changing the level attribute of the <trust> section from 'WSS_Minimal' to 'WSS_Medium'.
  9. 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Conver Asp.net to SharePoint 2010

Migrating Asp.Net Application to SharePoint 2010
Migrating Asp.Net Application to SharePoint 2010
Or
Converting asp.net 3.5 Web Application to SharePoint 2010

Hi Friends,
                     This blog is about converting or migrating custom asp.net 3.5 or 4.0 web application to SharePoint 2010.
Scenario: I have an application which was developed in asp.net 3.5 with sql server database. I have a SharePoint Portal where I need to deploy my Asp.net Web application. The main aim is to integrate all the asp.net application that were developed in Asp.net so that all the application can be access using single-sign-on and all application will have single access point in SharePoint.

Solution:  To accomplish this scenario I have first created a SharePoint site collection in a web application then in this web application I have created no of sub sites for each asp.net Web Application so that each application can be separated.

So Steps will as below:

Note: This solution is targeted to SharePoint 2010.
Step 1: Create a Web Application (if you don’t have existing one) from SharePoint Central administrator.

Step 2: Create a Site collection which will be the main entry point for SharePoint Portal.

Step 3: Create sub sites for each web application which you are planning to deploy in SharePoint so that you can link each application from main site.
 -  Now first check your existing asp.net Web application setting and configuration

Step 1:  Go to your Asp.net Web application and check if all the application are using .NET framework 3.5

Step 2: Go to all the application’s (if you have n-tier application) and check that every solution is using .net 3.5 framework.

Step 3: First Create strong key name for all the application dlls which you will copy to SharePoint so every solution in an application require strong key.

  ->  To generate strong key go to Start-> Visual studio 2010(2008)-> Tools ->Visual studio command Prompt. It will open visual studio command prompt.
  -> Type :  sn –k yourdllName.dll  <path of your snk file where you will like to store>
  ->  eg: sn –k mydll.dll C:\mysnk1.snk

Step 4: Now go to your asp.net application select solution one by one ->Project Properties. You will get signing tab click on it. Check sign the assembly checkbox and upload strong key and save.

Step 5: Follow same steps 3 to 4 for all your application to sign with strong key name.

Step 6: Add following line in all the assembly.cs of all layers ( if n-tier): at the top of [assembly attribute]. It allows SharePoint to call these dlls and also allows communication among dlls.

[assembly: System.Security.AllowPartiallyTrustedCallers]

Step 6: Now compile all of your dlls layer by layer and the complete solution.

Step 7: Next you need to copy all the dlls from you asp.net solutions’ bin directory to SharePoint. So you have created a SharePoint Site go to It’s virtual directory.

    -:  If your IIS is installed in C:\ drive then your path may look like below:
    -:  C:\inetpub\wss\VirtualDirectories\8888\bin where 8888 is the port number of my sharepoint site.
    -:  Copy all the dlls to Bin directory

Step 8: Next Open the web.config of your SharePoint portal which will be at your port no 8888 (your port no may differ).
   ->  Add following Tag to web.config file:

<!–Page parser added–>
< PageParserPaths>
< PageParserPath VirtualPath=”/*” CompilationMode=”Always” AllowServerSideScript=”true” IncludeSubFolders=”true” />
< /PageParserPaths>


  ->  Mostly  <PageParserpaths> tag will be there in your web.config file search for it and place above tag in side it.
  ->  Next  you need to register your dlls within safe control section of web.config file go to <SafeControl> section and just press enter , then register your dll one by one here eg below

  <SafeControl Assembly="SPMigration, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 512e23e28e25b338 " Namespace="SPMigration" TypeName="*" Safe="True" SafeAgainstScript="True"/>

      <SafeControl Assembly="BusinessLayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken= f371028fb11feba8 " Namespace=" BusinessLayer " TypeName="*" Safe="True" SafeAgainstScript="True"/>

     - Assembly: Name of your assembly which you have copied in bin directory of SharePoint.

     -  Version: It will mostly 1.0.0.0 but you can check version of your assembly and replace it with correct version.

     - PublicKeyToken: You need to get public key token of your assembly, you can get it by using sn –T <your dll name>. It will display public key token copy it here.

     - NameSpace: It should contain name space which is written in side assembly some time assembly name and namespace is different. So check name space from your asp.net Web application and copy here.

   - Other attribute will remain same as I have written above.
Step 9: If your application contains database connection or any other dll registration which may be there in asp.net’s web.config file needs to be register here also in web.config file of SharePoint Portal. So copy connection string from your web.config to SharePoint portal config in Connection section of web.config same way register all dlls also in this config file.

Eg:
 <connectionStrings>
< add name=”MyCompanyConnectionString” connectionString=”Data Source=PRAVEEN;Initial Catalog=MyCompany;User ID=sa;Password=@System”
providerName=”System.Data.SqlClient” />
< /connectionStrings>


Step 10: Now everything is in place, we need to copy our aspx pages in SharePoint site. For this go to your asp.net Application copy only aspx pages not .cs page in a separate folder on your computer.

   - Open your SharePoint site by using SharePoint designer and click on site pages. On top of ribbon bar click on import files and point to your local folder of where you have copied  aspx page. Click on ok button it will import all the aspx pages to SharePoint site page gallery.

 -  Now you need to change Master Page: Attach v4.Master page to these pages by going to Style tab on Ribbon, Click on Attach and select the v4.master as shown here. Select aspx page one by one and apply master page to them.

  -  Now go to source code of all your aspx pages one by one and remove codebehind tag.  

  -  Inheritance tag at tope of page is very important. It may struggle you to get data from your database. So don’t change it because we have registered our dll in web.config just make sure Inheritance=”DllName.CodebehindfileName”

   - If you want to use your master page of asp.net then you need not to use master page of SharePoint just keep the things as it is and put your master page in a web part which is used to display page content and it will work fine.

  -  Now  go to your SharePoint portal (or even you can check your pages in SharePoint designer by browsing page in browser), go to Site pages and click on any page which belongs to your application. You can see it has same look and feel as SharePoint.

That’s It your application is migrated now you can use Authentication of SharePoint and you can also use default permission of SharePoint with your aspx pages.

Note: If you get any issue regarding migration of your asp.net application write your comments or you can mail me at: girish.eng@gmail.com

Related link: 
Migrating asp.net web Application to ShaerPoint 2010
Converting asp.net Web Application to ShaerPoint 2010
Converting Custom asp.net Web Application to SharePoint 2010

Thanks& Regards
Gireesh Painuly
Happy Coding…