Thursday, January 4, 2007

Setting a Custom Edit Form for a Content Type

This is my first post of many regarding Sharepoint 2007 development and some of the interesting things I came across during the last four months. This is being posted from Word 2007 and its new ability to directly publish to blogs. So if it has some problems, then please bear with me until I can correct them.
Many quirks that I observed in the beta releases of Office 2007 have been fixed in RTM. However, many of them especially in WSS/MOSS cross site searching remain. Behavior you would expect from standard CAML or SQL searching is not necessarily there. This subject will be for future posts. However, this post is about a quirk regarding setting a custom edit form for a content type in a document library. There was a great post by Ton Stegeman http://www.sharepointblogs.com/tonstegeman/archive/2006/10/29/15374.aspx regarding creating custom edit forms for Sharepoint content types. Once you had created your edit form and deployed to the layouts directory you had to set the “EditFormUrl” property of the content type. This works great. Unfortunately, I had a requirement where the client wanted to turn this custom edit form on and off. For example, from a configuration page the customer would choose a content type associated with a document library and then check a check box to enable the custom edit form, or uncheck it to disable the custom edit form and go back to the default. I thought you could just set the “EditFormUrl” property to an empty string or null and then update the content type and the document library. However, after doing this the “EditFormUrl” property was still set to my custom edit form. When developing on a beta product you tend to feel like Thomas Edison. You know 10,000 ways something does not work. So after much experimentation I was able to get the “EditFormUrl” property to clear and have the default edit form display.
The ContentType object contains an “XmlDocuments” property which is a SPXmlDocumentCollection containing a collection of XmlDocuments. This collection contains two XmlDocuments. One for the xml containing the names for the content type’s formtemplates and the second containing the xml containing the urls for the editform, newForm and displayForm. In order for the content type to revert back to the default you must delete the second xmlDocument in the collection using the following code:
ct.XmlDocuments.Delete("http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url");
You must then call the content type’s update method which will regenerate this xmlDocument and the corresponding editForm property.
The following code demonstrates this technique.

using (SPSite site = new SPSite("http://basedev/sitedirectory/tester1/"))
{
using (SPWeb web = site.OpenWeb())
{

SPList list = web.Lists[documentlibrary];
SPContentType ct = list.ContentTypes[contentType];

if (!on)
{
ct.EditFormUrl = string.Empty;
ct.NewFormUrl = string.Empty;
ct.DisplayFormUrl = string.Empty;
}
else
{
ct.EditFormUrl = "_layouts/whateverEdit.aspx";
ct.NewFormUrl = "_layouts/whateverNew.aspx";
ct.DisplayFormUrl = "_layouts/whateverDisplay.aspx";
}
ct.XmlDocuments.Delete("http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url");

ct.Update();
list.Update();
}
}