您的位置:首页 > 其它

SharePoint 2007 - /_layouts and how to create pages that run in site context

2008-03-14 18:19 731 查看

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 web application project 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:

Right-click of the SharePoint web site

Select properties

Go to the Home Directory tab

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Site title test</title>
</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" />
<!-- <authentication mode="Windows" /> -->
</system.web>
</configuration>


We can now run the page in the context of two different sites to see that it works:









Published Thursday, November 09, 2006 4:13 PM by svdoever
Filed under: SharePoint

Comments

Thursday, November 09, 2006 10:16 AM by Serge van den Oever [Macaw] : SharePoint 2007 - _layouts, pages in site context, help!

# 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

Thursday, November 09, 2006 4:31 PM by Ishai Sagi

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

Friday, November 10, 2006 5:12 AM by svdoever

# 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!

Monday, November 13, 2006 4:44 AM by Roni Hofer

# 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

Monday, November 13, 2006 9:34 PM by Ishai Sagi

# 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?

Thursday, November 30, 2006 10:20 AM by Ryan

# 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

Thursday, November 30, 2006 12:15 PM by ryan

# 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??

Monday, December 04, 2006 2:06 AM by Manish

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

Monday, December 04, 2006 2:58 PM by Ryan

# 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

Monday, February 19, 2007 1:51 AM by anser

# 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?

Tuesday, April 17, 2007 12:33 PM by wasi

# 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

Tuesday, May 08, 2007 7:22 AM by Karthik

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

Wednesday, June 06, 2007 8:50 PM by James McDowell

# 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 siteroot/.../page.aspx 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?

Wednesday, August 08, 2007 5:05 AM by PrashanthSpark

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

Thursday, October 11, 2007 2:20 AM by Craig

# 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

Thursday, November 29, 2007 11:39 AM by Entradas imprescindibles para desarrollar aplicaciones dentro de sharepoint «

# Entradas imprescindibles para desarrollar aplicaciones dentro de sharepoint «

Pingback from Entradas imprescindibles para desarrollar aplicaciones dentro de sharepoint «

Sunday, December 30, 2007 5:55 AM by negm.phlestin@gmail.com

# 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??

Thursday, February 14, 2008 2:36 PM by t_Kalpana@rediffmail.com

# 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐