Sunday, August 26, 2007

Use your own Code Behind for your Page Layouts or Master Page

You create your Code Behind as normal. You'll need to include the Microsoft.Sharepoint classes that you'll use. Base your Master Page on "System.Web.UI.MasterPage" and your Page Layouts will be "Microsoft.SharePoint.Publishing.PublishingLayoutPage". If you plan to add any controls, do that in the "CreateChildControls()" method. That method is also were you will wire up any events you want for those controls.

Now in your ASPX page, reference the assembly at the top, like:

<%@ Assembly Name"your.assembly.name, version=1.0.0.0, Culture=neutral, Publickeytoken=####" %>

Next add the "Inherits" like:

<%@ Page language="C#" Inherits="your.assembly.name.ClassName"%>

After you build your project, put the DLL in the GAC or the Bin folder of the web application. Then you'll need to reference the DLL in the web.config as a SafeControl.

Now, the page that is using the Page Layout should be using your code behind.

Need a custom ASPX in Sharepoint and not use a Master Page etc?

It's simpler than you may think. The proper way to put the file in the 12-hive would be to use a solution to deploy it (I deployed it as a feature). I'm not going into detail on how to do that part. This how to do it manually.

OK, so create your page as you normally create one. If you want code behind, you'll have to compile the DLL and place it in the Bin/GAC, then you'll need to add a reference to it in the web.config of the application as a safe control. That will cause your web application to load the DLL so you can use it.

After you get it looking how you want, simply copy the aspx page to a location in the 12-hive. Most say use \layouts\Custom\yourpage.aspx. No problems with putting it there.

In your browser the address will look something like this: http://yoursite/_layouts/Custom/yourpage.aspx

Here is an example of one (I don't know how to insert code blocks yet, if you can help, send me an email so I can make the code look better.):

ASPX Page:

<%@ Assembly Name="your.assembly.name (this is the dll for your code behind), version, etc" %>
<%@ Page language="C#" Inherits="your.assembly.name.CLASSNAME(Code Behind)" %>
<HTML>
<HEAD>
</HEAD>
<BODY>
<form>
Hi
</form>
</BODY>
</HTML>

If you don't use a code behind, remove the assembly reference and the "Inherits" property.

If you need more details, let me know.

Saturday, August 25, 2007

Where to store global properties?

Before sharepoint, we used the web.config to store properties. Then in the global.cs, we would store them as application variables (Application["Name"] = ConfigurationSettings.AppSettings["NameValue"];). If we wanted to update the properties, we would have to make the change to the web.config. We all know what happens when you do that.

What I did in Sharepoint (Only because I don't know of a better way. Advice is welcomed) is create a new class called GlobalProperties and based it off of ConfigurationSection. In the constructor, I'm calling a method that reads from a list and stores the name/value of the items in a ConfigurationPropertyCollection. I've also setup a method that will reset the collection since the constructor is only called one time.

I'm not sure this is the best way to do it, but it made sense to me.

No parameterless constructor defined for this object

This error popped up all of a sudden when we tried to create a new page. It was only happening with 1 specific page layout. All the existing pages still worked, so it was only when the page was being created.

I tried a few things to fix this problem, including renaming the namespace, and the class name. I already had an empty constructor in the code.

I decided to delete the page layout from the gallery then redeploy our solution which places the page layout in the gallery. That did the trick. I guess the page layout was corrupted.