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

 

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:

Comments

# Serge van den Oever [Macaw] : SharePoint 2007 - _layouts, pages in site context, help!
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

# Entradas imprescindibles para desarrollar aplicaciones dentro de sharepoint &laquo;

Pingback from  Entradas imprescindibles para desarrollar aplicaciones dentro de sharepoint &laquo;

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

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Started GET "/notebooks/" for 127.0.0.1 at 2023-07-14 09:59:56 +0800 Processing by NotebooksController#index as HTML Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms) NoMethodError (undefined method `all' for Notebook:Module): app/controllers/notebooks_controller.rb:4:in `index' Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/actionpack-4.2.11.3/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.2ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/actionpack-4.2.11.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.1ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/actionpack-4.2.11.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.5ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/actionpack-4.2.11.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (9.8ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (0.2ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.2ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.1ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (8.4ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.1ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.2ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (15.1ms)
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值