SharePoint 2007 - _layouts, pages in site context, help!

NOTE: See this blogpost for updated information on this topic.

SharePoint 2003 has a very powerful feature to run pages in the context of a site, this is especially used for the administrative pages. All these administrative pages live in the “/_layouts” folder. If you have two sites, http://server/A and http://server/B, and an administrative page admin.aspx, you can execute this page in site context as follows:

http://server/A/_layouts/admin.aspx executes in the context of site A

http://server/B/_layouts/admin.aspx executes in the context of site B

Within those pages you can directly access the current site with the following line of code:

       SPWeb site = SPControl.GetContextWeb(Context);
 

With this site object as starting point you can access all information that is in your site.

This approach is used in all standard pages in /_layouts, with all pages containing inline code.

All the years I have been working with SharePoint 2003 I had another approach to create web pages to run in the context of a site. I just create a web project, deploy the aspx pages, images, client scripts etc to a directory in /_layouts, and put the "code behind" dll's into /_layouts/bin.

This approach no longer works, and a new adventure begins...

THE ADVENTURE

  1. In SharePoint 2007 the _layouts virtual directory no longer contains a "bin" directory, all pages use inline code. First thing I tried was to add a "bin" directory, deploy my "code behind" dll's there and go to my page http://server/_layouts/myapp/default.aspx. Nothing happened, not even an error, just a blank page.
  2. I change the name of of the page to a non-existant name: http://server/_layouts/myapps/blabladefault.aspx. Again nothing happened. All url's requested in _layouts that don't exist just give a blank page!!
  3. After looking at the web.config in SharePoint 2003  I see that the line:

    <trust level="Full" originUrl="" />

    is missing, so I add that line. Still the same result, a blank page. But at the same time strange things start to happen to other parts of SharePoint. The other administrative pages return a blank page as well, and the pages in the SharePoint administration site start to loose their design. After looking at the source of the page it is not so strange: a lot of assets from the /_layouts path are referenced. But it is strange that with trust level "Full", the highest as far as I know, things stop working!

  4. I remove the "bin" directory, the web.config is as prestine as it was, and I try again. Something happens: I get an expected error, the "code behind" dll can't be found! A nice error is displayed in "SharePoint syle", with the message: Could not load type 'myapp._Default'. Ah, almost there I'm thinking, we just need a probing path.

  5. I add the probing path to give .Net direction on where to look for the dll's:

    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <probing privatePath="myapp\bin"/>
        </assemblyBinding>
    </runtime>

    and I hit the refresh button in great expectation... same error. the "code behind" dll can't be found.

  6. One last thing to try that must work, otherwise I'm getting mad: throw the "code behind" assembly into the GAC. I strong-sign my assembly, drag it to the GAC, do an IISRESET and... shoot! I'm mad! Still the same error!

  7. Back to the model that Microsoft uses in the _layouts pages: all code inline in the aspx pages, and this works... I hate this approach, but it seams the only way.

QUESTIONS

And now my questions!!! There must be someone out there who knows how to solve my problem.

  1. Is it possible to have pages with code behind in _layouts, and have the code behind assemblies in a bin directory?
  2. Is it possible to have our own custom"_layouts" like virtual directory that executes "in context" of a page?

UPDATE: I managed to get a 2005 web project working, where the code is not precompiled. You don’t need to deploy your code-behind code deployed as a DLL. One thing to watch out for however: if you have a web.config file in your project, comment out the following line: <authentication mode="Windows"/>.

12 Comments

  • Did you ever find a solution to your original problem in this post? If so, I'd be very interested in hearing how you resolved it.

  • I am in the same boat. I used to use the _layouts directory to accomplish interacting with SharePoint OM to do some complex Administration automation via some custom web app. It looks like they are now forcing us to use the wpresources directory. So it is only going to be available to that Site Collection.

    So to use wpresources.
    1) Find the directory (C:\inetpub\wwwroot\wss\VirtualDirectories\{GUID}\wpresources).
    2) Change the web.config file to allow .aspx and .ascx files.
    3) Copy your files into the directory.
    4) Enable the directory to run as an application.
    5) And you should be good to go!!!

  • So - I've been trying to figure this one out for a while myself because I want to use the web application project, not the web site project, for my moss development. My inital motivator was the ability to use page inheritance - another story...

    Just got it to work - after a little chat with my man Brad Younge (who posted above) - so had to let you know.

    1) Create a bin directory (if it's not there already) in wwwroot\wss\{GUID} for the wss web app you want your custom pages to be in.

    2) After you create your custom web app project in VS, change the output directory of it's assembly to the bin you just created in the wss web app.

    3) Create a virtual directory in IIS that points to the root of your custom web, and make sure it's running in the same app pool as the wss web app

    4) Run it

  • Sorry - didn't state that the virtual directory in iis should be in the _layouts directory of the wss web app where you created the bin.

  • Hi,
    I am in the same boat - we need comples active directory authntication to be handled by out aspx pages in sharepoint 2007
    I am not able to find any clue how to add aspx pagges with code behind so that I can do what I want apart from accessing the sharepoint admin functionality.
    Please help - spent 2 weeks to find the solution but nowhere to go
    Thanks in advance

  • I can't create a site in SharePoint 2007 with trust level in web.config is wss_custom. Please give me a way so that I can do this. Thanks very much.

  • Hi There,
    I have encountered an permission issue in the sharepoint and it would be great if you can help. Please find the details below.

    I have created a aspx page which shows a grid with a 'DownLoad' button on each row, and clicking on the button it reads the database and downloads the data as a text file. I have placed this aspx page in '_Layout' folder and showing through a 'WebPartpage viewer' in the share point. The download functionality is working fine for 'Administrator' but for other members it is showing an error "This error (HTTP 403 Forbidden) means that Internet Explorer was able to connect to the website, but it does not have permission to view the webpage.".

    Please find the part of the code that I have for Download function
    [
    Dim fs As System.IO.FileStream = Nothing

    fs = System.IO.File.Open("typeh.bch", System.IO.FileMode.Open)
    Dim btFile(fs.Length) As Byte
    fs.Read(btFile, 0, fs.Length)
    fs.Close()
    With Response
    .AddHeader("Content-disposition", "attachment;filename=" & "typeh.bch")
    .ContentType = "application/octet-stream"
    .BinaryWrite(btFile)
    .End()
    End With
    ]

    Many thanks in advance

    Suresh

  • SharePoint 2007 _5F00_layouts_2C00_ pages in site context_2C00_ help_2100_.. Tiptop :)

  • Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my
    newest twitter updates. I've been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  • My developer is trying to convince me to move to .net from PHP. I have always disliked the idea because of the costs. But he's tryiong none the less. I've been using Movable-type on a variety of websites for about a year and am worried about switching to another platform. I have heard fantastic things about blogengine.net. Is there a way I can transfer all my wordpress content into it? Any help would be really appreciated!

  • Hello just wanted to give you a quick heads up. The text in your post seem to be running off the screen in Ie. I'm not sure if this is a format issue or something to do with internet browser compatibility but I figured I'd post to let you know. The design look great though! Hope you get the issue fixed soon. Kudos

  • Heya just wanted to give you a brief heads up and let you know a few of the images aren't loading properly. I'm not sure why but I think its a linking issue. I've tried it in two different web browsers and both show the same results.

Comments have been disabled for this content.