分类: 系统运维
2009-07-22 15:18:05
Ages ago, in the time that SharePoint 2007 was still beta, I dived into how to create "in site context" pages that should be hosted in the /_layouts directory of SharePoint. My adventures from back then can be found in this blog post. I don't want to take the default Microsoft approach where all server-side code is included in the aspx pages themselves. Developing this way is way more difficult that using code-behind files. I found a solution by creating a Visual Studio 2005 web site in the /_layouts virtual directory of my SharePoint web site, which points to the physical folder C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS. In this approach all code behind files are part of the solution, and are compiled and cached on page request. Although this approach works, I don't really like it. I prefer the Visual Studio 2003 approach where all code-behind is compiled into a single assembly that can be deployed. Another problem is the location of referenced assemblies. I had my referenced assemblies in the GAC, but I prefer to deploy to a bin folder so no IISRESET recycling of the SharePoint application pool is needed on recompilation.
What I really want to achieve is the following:
Create a that can be deployed to the SharePoint /_layouts virtual directory, so my code is executed in the context of a site.
The solution happens to be really easy:
Create a web application project, either directly in the /_layouts folder or somewhere else and copy over all files needed to run your application.
The *.dll and *.pdb files produced as build output must be places in the bin folder of your SharePoint web site. In my test situation this is the folder C:\Inetpub\wwwroot\wss\VirtualDirectories\3a938f6a-15f2-49ae-be78-328ad78974f5\bin. You can find this folder in your Internet Information Server Manager as follows:
The value in Local Path specifies the path to the virtual directory, and in this virtual directory you find a folder bin.
If you create your web application project within the /_layouts virtual directory, you can set the build output path directly to this bin folder.
Note that you can't use the Publish Web feature of the web application project, because you can't specify a separate path to deploy your assemblies to:
For my test I created the following project:
I added some really simple code to the Default.aspx and Default.aspx.cs files to prove that it works:
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SergeLayoutsTest._Default" %> DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns="" > <head runat="server"> <title>Site title testtitle> head> <body> <form id="form1" runat="server"> <div> Title of this site: <asp:Label ID="LabelTitle" runat="server" Text="Label">asp:Label> div> form> body> html>
Default.aspx.cs:
using System; using Microsoft.SharePoint; namespace SergeLayoutsTest { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SPWeb web = SPContext.Current.Web; LabelTitle.Text = web.Title; } } }
There is one more thing to do, exclude the selection of the authentication mode from your web.config file:
web.config:
xml version="1.0"?> <configuration> <appSettings/> <connectionStrings/> <system.web> <compilation debug="true" /> system.web> configuration>
We can now run the page in the context of two different sites to see that it works:
# Serge van den Oever [Macaw] : SharePoint 2007 - _layouts, pages in site context, help!
PingBack from http://weblogs.asp.net/soever/archive/2006/06/14/SharePoint-2007---_5F00_layouts_2C00_-pages-in-site-context_2C00_-help_2100_.aspx
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
Did you try using that same page from another virtual directory running with a different application pool?
My tests show that if you do, it fails.
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
Hi Ishai,
Did you try to copy your code-behind dll to the bin directory of the SharePoint site in the other web application. I assume this works.
Serge
PS: You have a great weblog!
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
Hi Serge,
Thanks for your great post! I'm currently investigating in the same area. I tried your proposition with a "web application project". It works fine. Until now I have two issues:
- myWeb.AllowUnsafeUpdates = true; throws an security exception (I think it's because the assembly has to be in "bin" instead of "app_bin" or GAC)
- Global.asax cannot be used
Before I tried the following way with two different VS projects: In the first project there are only .aspx files (in a subfolder of layouts). The second project is a class library with all the code behind and additional classes I need. In the .aspx files the reference to the code looks like this:
<%@ Page Language="C#" AutoEventWireup="true" Inherits="Documena.DmAssistant.AppPages._Start, Documena.Library, Version=1.0.0.0, Culture=neutral, PublicKeyToken=08e712e82e6eb11b" MasterPageFile="/_layouts/application.master" %>
The assembly may reside in "app_bin" or GAC.
Everything works fine (expect Global.asax). The big disadvantage of this solution: you have to define the controls of the .aspx pages in the class library manually.
Any comments are welcome!
Roni
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
I will have to try your suggestion, but I think the problem is the application pool.
Roni, you should not deploy to the app_bin folder. you should only work on the bin folder.
by the way, is this the same roni who worked for cellcom a few years back?
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
How do you reference the microsoft.Sharepoint.dll? I get an error when i do it
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
i am getting the following error
Request for the permission of type 'Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' failed.
Any Ideas??
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
Respected Sir
Manish here.
Sir i want ot nkow thet can we set the context at run time in share point assuming that we have got the url of a page by using parentweb property then we have to set this as a context at run time.
Thanks in Advance.
Bye.
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
The problem i had was with the WSS_minimal instead of WSS_medium in the web.config file it is working now but i am getting
Content controls have to be top-level controls in a content page or a nested master page that references a master page.
when i try to add a master page in the OnPreInit(EventArgs e) method
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
I've used this method to get current user information. But SPContext.Current.Web.CurrentUser.LoginName returns "SHAREPOINT\system". How to get logged in user?
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
i am getting error ~/_layouts/application.master is not found
please reply
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
Thanks for the great post.
Did u ever try to deploy a web application that uses its own master page.
When I tried, I get an error message that says,
The referenced file '/MasterPages/TestMaster.master' is not allowed on this page. at System.Web.UI.TemplateParser.ProcessError(String message)
at System.Web.UI.BaseTemplateParser.GetReferencedType(VirtualPath virtualPath, Boolean allowNoCompile)
at System.Web.UI.PageParser.ProcessMainDirectiveAttribute(String deviceName, String name, String value, IDictionary parseData)
at System.Web.UI.TemplateParser.ProcessMainDirective(IDictionary mainDirective)
Please reply.
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
I have built a number of Web Apps in my layouts directory, and they generally can be used from any site like this but when I add a local, simple master page, then the web app only works from the site root, and not the sub sites. I get the classic "Unknown Error". Any thoughts?
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
How to create personal site using code
i need to create a personal site for all the users while iam creating userprofile's
wht is the object model i need to specify,
does personal site has tempalate..
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
Thanks alot! this post has saved my life. 2 days of trial and ERROR. You have no idea how cool this is! Thanks again!
YYYYEEEEEEEEESSSSSSSSSS
# Entradas imprescindibles para desarrollar aplicaciones dentro de sharepoint «
Pingback from Entradas imprescindibles para desarrollar aplicaciones dentro de sharepoint «
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
i am getting the following error
Request for the permission of type 'Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' failed.
Any Ideas??
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
Hi,
We have developed web application that is copied to the _Layouts folder in SP and is IIS configured with Windows authentication. But I'm faced with big problem, Authentication. The application has different modules. I'm creating an ASP menu control on the home page of a site and where each menu item points to a module folder path on the SP server. The module should be restricted only to few users. Planning to create a group in SP. But I dont know how to authenticate the user in Page_Load event.
Any help would be greatly appreciated
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
Thanks for such a nice post.
I have a problem similar to this.
I have 2 connected web parts (2 .cs files) as project folder (one is provider and second is consumer) in MOSS 2007 portal. The provider web part passes selected data row to consumer web part.
The consumer web part need to fetch some detail value from received value and based on that it required to display dundas chart.
So I have created one web application with one .aspx page. This page is receiving querystring data and based on that it creates chart.
From my consumer web part , I am calling this page as HTML IMG tag (
The problem is this, as long I keep the web application open in studio, the chart get displayed properly. But when I closes the web application , the graph doesn't appear.
So can u tell me how I can put that .aspx page such that its scope will remain with my sharepoint application.
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
Hi,
I'm trying your solutions, but when i try connect to masterpages not display, end i config it's run with mymasterpage. I have a problem , a can't add webpart to page. I think's you have one solution for my problem, please help me!
Thanks you
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
HI,
All are working except the pages which usese session variables.
I have tried all these, add session module in httpModule.
enable session state in page directive and also in web.config in my application.
Are you know how can access session state from these pages.
# re: SharePoint 2007 - /_layouts and how to create pages that run in site context
I am getting this error
This error (HTTP 403 Forbidden) means that Internet Explorer was able to connect to the website
Can any one help me.
Thanks
# 将ASP.NET网站嵌入到MOSS站点下面的做法
很多朋友都有这个想法:我原先用ASP.NET写好一个应用程序,能不能直接地用到MOSS中去,而且还要能访问到MOSS的对象模型(也就是访问当前MOSS站点的上下文)呢? 答案是可以的,但是很别扭。下面...