Monday, September 19, 2011

How get SharePoint 2007 SpListItem DispForm Url

We need to implement a bit of tweaking to the List Item to get the Display Form url. I wonder why this was not provided very straight forward. Okay, the trick is need to get to PAGETYPE and then get the url. See the below code stub for details. In this similar process we can get the view/edit page urls as well.

 

#region Get Display Form URL

        private string GetDisplayFormURl()

        {

 

            using (SPSite site = new SPSite("http://yoursite"))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    SPList list = web.Lists[0];

                    SPListItem item = list.Items[0];

                    ////Now we have two options to concatenate the string.

                    string ItemDispFormUrl = String.Concat(item.Web.Url, "/",item.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url, "?id=", item.ID.ToString());

                    string DisplayFromUrl = item.Web.Url + "/" + item.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url + "?id=" + item.ID.ToString();

                    return DisplayFromUrl;

                }

            }

        }

 #endregion

 

Thanks for reading. If you have some other explanation – please post a comment… I’ll be happy to hear.

...HaPpY CoDiNg

Partha (Aurum)

 

1 comment: