Wednesday, December 2, 2009

Changing the Forms of your SPList in WSS/MOSS 2007

Had been browsing the web and found no good solution to this, I think I’ll write about this instead.

Basically the whole idea is to change the Display/Edit/New form of a specific list to point to another page, rather then the default NewForm.aspx or EditForm.aspx or DispForm.aspx (for custom lists for example). And would like to do so without going through all the pain of creating a new list definition.

Now, a quick solution would be to use SharePoint designer, where you can change those form’s URL directly through the List Property. However, a caveat, you are only allowed to point the form URL to a page that exists in the current web. In other words, you cannot point them to custom pages you deployed _layouts folder. A possible solution here might be writing ‘stub’ pages, that will further redirect users using javascript to the correct _layouts pages.

Another more elegant solution would be to change the forms’ URL programmatically through SharePoint DOM. However, take note that SPForm object cannot be modified. For example, the following will give you error:

SPForm newForm = list.Forms[PAGETYPE.PAGE_NEWFORM];
newForm.Url = "xxxxx";

Now, the fun part is, although SPList’s forms do not change,  SPContentType allows you do to so! The solutions I have is:

  1. Configure the list so that it supports ContentType (in advance list settings). For Custom List, this will create a content type named “Item” and content type named “Folder” for this particular list
  2. Using powershell/other coding means, do the following:

SPContentType ct = list.ContentTypes[0]; //assume it is the first
ct.NewFormUrl = "_layouts/MyNewForm.aspx";
ct.EditFormUrl = "_layouts/MyEditForm.aspx";
ct.DisplayFormUrl = "_layouts/MyDisplayForm.aspx";
ct.Update();

Now, this particular list will use the correct pages you deployed in the layouts folder!

No comments:

Post a Comment