Skip to main content

Error: "Value cannot be null. Parameter name: formType" retrieving default view page of document library

·2 mins

Few days ago I faced with the problem of linking to default view page of custom document library (for purposes of redirection with Source parameter after uploading new document and filling it’s meta data form).

At first I tried using SPList.Forms collection indexed with PAGETYPE enumeration as I found in article “How To Always Link to the Right Application Pages”. But every time I was trying to get the default view object this way:

var defaltViewUrl = documentLibrary.Forms[PAGETYPE.PAGE_DEFAULTVIEW].Url;

I was getting the following exception:

ArgumentNullException: "Value cannot be null. Parameter name: formType"

Fortunately this was working fine:

var defaultViewUrl = string.Format("{0}/{1}", documentLibrary.ParentWeb.Url, documentLibrary.DefaultView.Url);

I’ve checked that SPList.Forms collection returns valid forms/pages objects only for these three values:

  • PAGETYPE.PAGE_DISPLAYFORM
  • PAGETYPE.PAGE_EDITFORM
  • PAGETYPE.PAGE_NEWFORM

For every other it throws arg null exception as described above. This looks definitely like a bad design in Sharepoint API (one of many…) - the collection should be indexed with other enum that contain valid set of values.

You can find some more detailed info about the reason of this strange behavior on the short StackOverflow thread started by my question. How Stefan figured out that error is caused by enum to string conversion? I guess this mystery was revealed with ILSpy or some similar tool ;-)

Cheers!

A notice for archeologists...🏺 This post was originaly published on my previous blog and moved here. Some links and resources might not be up to date.