Customizing SharePoint OOB Forms

With my short time working with SharePoint, I have noticed that when you try to do any item creation of file uploading you always get a generic looking form like this:

What we want to do is move away from the generic form and change it completely. This doesn’t just mean changing the css and javascript that executes, but also controlling the back end (C# code). We want to be able to control everything that happens behind the scenes; control where/when controls get added to the page and what happens to them when the user hits the submit button. This hasn’t been an easy task; we have run into multiple issues along the way.

One issue that we have run into was that we wanted to be able to have any list show up no matter where you were on the site. So if you were at http://rootsite/ you could still access a list that was located in http://rootsite/sites/non-rootsite/. The code below is the way that we found worked for adding controls to the page in any type of form situation (new, edit or display). We found that if the “context.FormContext.FormMode = SPControlMode.New” wasn’t there then controls from a different subsite wouldn’t render.

[code language=”csharp”]if (!isNewItem)
field = item.Fields.GetFieldByInternalName(field.InternalName);

var editControl = field.FieldRenderingControl;
editControl.ID = “fld_” + uniqueid;
editControl.FieldName = field.InternalName;

//edit mode if id is provided
if (!isNewItem)
{
editControl.ControlMode = this.IsReadOnly ? SPControlMode.Display : SPControlMode.Edit;
editControl.ItemId = item.ID;

var context = SPContext.GetContext(HttpContext.Current, item.ID, list.ID, web);
editControl.RenderContext = context;
editControl.ItemContext = context;
}
else
{
editControl.ControlMode = SPControlMode.New;
editControl.ItemId = item.ID;

var context = SPContext.GetContext(HttpContext.Current, item.ID, list.ID, web);
context.FormContext.FormMode = SPControlMode.New;
editControl.RenderContext = context;
editControl.ItemContext = context;
}

editControl.ListId = list.ID;

this.pnlControls.Controls.Add(editControl);[/code]

Another issue that we have run into is the way validation is handled. Validation hasn’t been as simple as one would expect.

[code language=”csharp”] if(!this.Page.IsValid)
{
var validators = this.Validators;
}[/code]

Here you will get Required field validators that are showing up as BaseValidators but you will also get BaseFieldControls that have the IsValid property set to false. So you have to account for that. But if there have been any custom validation added to the list, then that validation happens (from what we have seen) only when the SPListItem.Update() is called. If validation fails the update will throw a SPListDataValidationException that you have to catch and deal with at that time. So dealing with the validation has been pretty tricky so far.

These are just some of the problems that have become apparent in our process to completely customize the OOB forms. It has been quite the journey and we have come a long ways. But we still have a long ways to go.