Using the PAGETYPE enumeration to get url to formpages

Sometimes you might need to get the url to the editform.aspx or dispform.aspx (or something else). I´m going to show you a way to do that in an easy way:

using (var sitecollection = new SPSite(siteId))
    using (var site = sitecollection.RootWeb)
        SPListItem li = site.Lists["Orders"].GetItemById(itemId);

        string url = li.ParentList.Forms[PAGETYPE.PAGE_EDITFORM].ServerRelativeUrl;
        UriBuilder uri = new UriBuilder(sitecollection.MakeFullUrl(url))
            Query = string.Format("ID={0}", li.ID.ToString())

Ok, so when I have access to the SPListItem I´m using the SPList.Forms property to get access to the EditForm. One of the possibilities of that collections indexes is to send in a PAGETYPE enumeration, in this case the PAGE_EDITFORM.

Then use an UriBuilder to build the url. I´m also using the MakeFullUrl method on the SPSite to make sure I get the correct url if we´re using alternate access mappings etc.
Finally, I´m adding the id of the SPListItem as a querystring.

This should return something like this (if the SPListItem exists on a subsitecollection and subsite…etc):

Note: This could as easily be made in an extension method on the SPListItem class.


  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: