SharePoint Sharpener

Obsessively Sharpening SharePoint

Posts Tagged ‘editform

How To Always Link to the Right Application Pages

with one comment

As a SharePoint developer you’ve probably run into this problem several times: How to make sure that links to application pages stay valid irrespective of where within a site collection your web part is placed.

If you hand-code links you’re likely to get the path relative to the root site wrong. Even worse, if the the filename of an application page is changed, e.g. from DispForm.aspx to DispFormNew.aspx, your links will surely break.

 

The application pages

Generally, a SharePoint list contains the following application pages:

  • AllItems.aspx
    Shows all items in the list
  • DispForm.aspx
    Displays a read-only version of a list item
  • NewForm.aspx
    Form for creating a new list item
  • EditForm.aspx
    Form for editing an existing list item

More applications pages exist but these are the ones most commonly used.

 

Ensuring unbroken links programmatically

The PAGETYPE Enumeration gives you access to the application pages and enables you to build links that always work.

Assuming you have already created your SPList and SPListItem objects the below snippet builds a link to the correct DispForm:

list.Forms[PAGETYPE.PAGE_DISPLAYFORM].ServerRelativeUrl.ToString();

Of course, the same applies to NewForm and EditForm.

 

Linking to the default view of a list

Below a list of elements you may wish to link to the underlying list’s default view. In many cases this will be the AllItems.aspx page but what happens if a user changes the default view in the settings of the list? If you’ve hardcoded a link to AllItems.aspx you may be lucky that the link still works but if AllItems.aspx has been renamed or removed, you’re out of luck.

Instead of hardcoding the link you should use PAGE_DEFAULTVIEW, like this:

list.Forms[PAGETYPE.PAGE_DEFAULTVIEW].ServerRelativeUrl.ToString();

Simple, isn’t it?

Written by Thomas Sondergaard

May 16, 2009 at 3:01 pm