On this post I will write a few words how you can create pages under the SharePoint layouts-folder. If you are wondering why would you want to do it, well it is simple. As an example you might want to create a web service (which has the same ideology behind it. In other words once you understand how to create a page you know how to do it for a web service). Other examples than web service might be a user control which you might want to use in somewhere in your project. Or you might want to create a page that streams certain data like charts. Well, the usage of this example is up to you but there are endless usages.
To make this work we will be needing the following few items:
A created ASPX file that you would copy under your desired layouts folder location.
- A code behind file in an assembly that you will deploy in GAC or your Sharepoint Bin directory or in any other place of your choice.
- Also we must take into consideration possible authentication issues.
- We need to tell the ASPX file what class it will use and where to find it. In this case in the compiled assembly in step two.
This class will save your nerves if you want to have a page that for example generates a chart image and you want that data to be available to anyone. If you do not use this class you might run into SharePoint security issues like data won’t show or you are going to be asked for authentication information. NOTICE that this class will not solver all of your authentication issues like any data access to listings or alike locations. You must make sure yourself that proper right are given to the anonymous user or use the following delegate:
SPSecurity.RunWithElevatedPrivileges(delegate
{
“Write your code here!!”
});
BUT BE CAREFUL. The code run under the delegate has many rights that you might not want to give.
Below is a code sample of how the page class looks like. Notice the overridden AllowAnonymousAccess property which when set to true tells that SharePoint to force no authentication logic on this page. Make sure this is to true if you want an anonymous page and make sure this is set to false if you want security logic to be taken into consideration.
public class ClassNoAuth : Microsoft.SharePoint.WebControls.UnsecuredLayoutsPageBase
{
protected override bool AllowAnonymousAccess
{
get
{
return true;
}
}
}
Once you are done with your code. Build & Compile and then deploy your assembly so that your SharePoint application has access to it. Putting it into GAC is probably the easiest and this is how I’m going to show in this example. Also because this assembly is deployed into GAC it needs to be signed.
Step 4:
Now what we need to do next is to tell the ASPX file where to find it’s code logic. This is ofcourse to be found in the “Sample.dll” assembly which we spoke earlier under the “Sample” namespace in the class named as “ClassNoAuth”.
So here is the piece of ASPX syntax that you must insert into the ASPX file:
<%@ Page Language=”C#” ContentType=”image/png” Inherits=”Sample.ClassNoAuth,Sample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=er3f31tf5g1fbed2″ AutoEventWireup=”true” %>
- Language: This simply states your coding language used to create the logic for this page.
- ContentType: In this case tells how this page is rendered to the client. In other words this is going to be a image. This is good if you plan to use this page to generate a chart image that is then streamed into a AJAX control.
- Inherits: This is the thing that is the most important to us. The first parameter before the “,” char is the name of the class. Notice make an explicit definition the whole path to the class. This means take into consideration namespaces also. The next param is the assembly name. Notice that you do not need to specify the file extension. Only the name is sufficient. Next are the simple assembly specific params as version, Culture and your assembly signature publickeytoken. All these information must match with your compiled assembly for the ASPX page to function properly.