Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8236776
  • 博文数量: 444
  • 博客积分: 10593
  • 博客等级: 上将
  • 技术积分: 3852
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-09 15:26
文章分类

全部博文(444)

文章存档

2014年(1)

2013年(10)

2012年(18)

2011年(35)

2010年(125)

2009年(108)

2008年(52)

2007年(72)

2006年(23)

分类: 系统运维

2009-07-22 15:18:05

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

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

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

Comments

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

# 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

# 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

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

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 «

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

Friday, April 11, 2008 4:21 PM by

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

Wednesday, October 29, 2008 5:07 AM by

# 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

Friday, March 06, 2009 9:43 AM by

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

Wednesday, April 01, 2009 12:17 PM by Lavu

# 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

Thursday, April 09, 2009 8:21 PM by 陈希章

# 将ASP.NET网站嵌入到MOSS站点下面的做法

很多朋友都有这个想法:我原先用ASP.NET写好一个应用程序,能不能直接地用到MOSS中去,而且还要能访问到MOSS的对象模型(也就是访问当前MOSS站点的上下文)呢? 答案是可以的,但是很别扭。下面...

阅读(1212) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~