Troubleshooting issues with DispForm, EditForm and NewForm


Each SharePoint List can be altered with a custom DispForm.aspx, EditForm.aspx and NewForm.aspx to display, edit or create list items and metadata. This post is about restoring them to a working point.

Botched Forms

So one of these forms are edited to a loss state or deleted altogether. If this is the case it will have some tangible effects on the List display:

DispForm botched

The above screen shows a botched Display Form where the Item Url shows http://moss/?ID=1

Behind the scene

All three forms are based on the same template {SharePointRoot}\TEMPLATE\Pages\form.aspx and have a List Form Web Part in the “Main” WebPartZone with specific settings to connect to the list and which function they have. Also, the ID property of the List Form Web Part must match the value of the __WebPartId attribute.

SharePoint 2007

SharePoint 2010
SP 2007 NewForm SP 2010 NewForm

Recovery

In a perfect world you can reset the edited form to the Site Definition or restore the it from the Recycle Bin, however this is often not the case.

Here’s a quick reference to the Microsoft.SharePoint.PAGETYPE enum:

Member name

Description

PAGE_INVALID

Not used. Value= -1.

PAGE_DEFAULTVIEW

Default view. Value=0.

PAGE_NORMALVIEW

Normal view. Value=1.

PAGE_DIALOGVIEW

File dialog box view. Value=2.

PAGE_VIEW

View, including both default view and normal view. Value=3.

PAGE_DISPLAYFORM

Display form for list items. Value=4.

PAGE_DISPLAYFORMDIALOG

Display form for a file dialog box. Value=5.

PAGE_EDITFORM

Edit form for list items. Value=6.

PAGE_EDITFORMDIALOG

Edit form for a file dialog box. Value=7.

PAGE_NEWFORM

New form for list items. Value=8.

PAGE_NEWFORMDIALOG

New form for a file dialog box. Value=9.

PAGE_SOLUTIONFORM

Solution form. Value=10.

PAGE_MAXITEMS

Not used. Value=11.

(http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.pagetype.aspx)

I’ve highlighted the three related to this matter (applies to both SharePoint 2007 and SharePoint 2010).

Manually

1. It’s easiest if you take a corresponding form from another SharePoint List in the site and export it (SharePoint Designer) or just copy it to a text editor. This way, most of the settings of the List Form Web Part are already correct

2. Generate a new GUID and fill that in for the __WebPartId and <ID> properties of the List Form Web Part markup. It has to be the same GUID but the formatting is different. See the above screens for samples

3. Update the <ListName> property with the List ID you’re targeting

4. Verify all other properties of the List Form Web Part and save the file as either DispForm.aspx, EditForm.aspx or NewForm.aspx

5. Import (SharePoint Designer) the page (or paste contents from the text editor)

Scripted

I had a botched list in SharePoint 2007 and I wrote a Console Application for it. It takes the markup for a Form and I’ve replaced some values with placeholders already. It connects to the List and replaces the placeholders with real values, then it saves the file to SharePoint.

1 string listUrl = "http://moss/Lists/Sample List"; 2 using (SPSite site = new SPSite(listUrl)) 3 { 4 using (SPWeb web = site.OpenWeb()) 5 { 6 SPList list = web.GetList(listUrl); 7 RestoreListForm(list, PAGETYPE.PAGE_DISPLAYFORM); 8 //RestoreListForm(list, PAGETYPE.PAGE_EDITFORM); 9 //RestoreListForm(list, PAGETYPE.PAGE_NEWFORM); 10 } 11 }
1 private static void RestoreListForm(SPList list, PAGETYPE ptype) 2 { 3 SPWeb web = list.ParentWeb; 4 5 // Create Form File 6 string formFilename = null; 7 SPControlMode formMode = SPControlMode.Invalid; 8 SPFileCollection files = list.RootFolder.Files; 9 Guid wpId = Guid.NewGuid(); 10 CalcFormInfo(ptype, out formFilename, out formMode); 11 12 byte[] formContents = Encoding.ASCII.GetBytes(String.Format(pagecontent, list.Title, list.ID.ToString("B"), formMode, (int)ptype, wpId.ToString("B"), String.Concat("g_", wpId.ToString("D").Replace("-", "_")).ToLower())); 13 14 try 15 { 16 if (files[formFilename].Exists) 17 files[formFilename].Delete(); 18 } 19 catch { } 20 21 SPFile formFile = files.Add(formFilename, formContents, true); 22 } 23 24 private static void CalcFormInfo(PAGETYPE type, out string fileName, out SPControlMode mode) 25 { 26 switch (type) 27 { 28 case PAGETYPE.PAGE_DISPLAYFORM: 29 fileName = "DispForm.aspx"; 30 mode = SPControlMode.Display; 31 break; 32 case PAGETYPE.PAGE_EDITFORM: 33 fileName = "EditForm.aspx"; 34 mode = SPControlMode.Edit; 35 break; 36 case PAGETYPE.PAGE_NEWFORM: 37 fileName = "NewForm.aspx"; 38 mode = SPControlMode.New; 39 break; 40 default: 41 fileName = null; 42 mode = SPControlMode.Invalid; 43 break; 44 } 45 }

Find the complete code here: Program.cs. Note that there’s a different page markup and additional properties for SharePoint 2010 and the code needs to be adapted to that.

There’s no magic in the code above, just automating some of the manual steps in a quick fix script.

Conclusion

As a general practice, don’t directly modify the default forms, rather create copies or blank forms and update the List or Content Type Settings (through SharePoint Designer or programmatically) to point to your custom forms.

List Settings

Content Type Settings

If you do have a botched list, hopefully the above information can be of help.

 


Links to this post

Comments

Monday, 9 Jan 2012 02:48 by dotNetFollower
Hello! There is only one problem with FormMode - in the most cases, this property returns Invalid value inside OnInit-method. But sometimes to get the correct SPControlMode value in OnInit is strictly necessary. I've developed a special method for that. The method is described in this article - Get SPControlMode, which is associated with the current request. I hope it will help somebody out.

Thursday, 21 Jun 2012 03:55 by Cédric
Wonderful you saved my life ! :)

Wednesday, 5 Feb 2014 12:38 by thé noir
nice share thx ;)

Monday, 13 Feb 2017 12:12 by WilliamEl
Hello. I need to contact admin. Thank you.

Thursday, 16 Feb 2017 10:38 by Steven Van de Craen
You can contact me through the form at http://www.sharepointblogs.be/blogs/vandest/contact.aspx

CAPTCHA Image Validation