Mittwoch, 10. Februar 2010

How to get absolute path to a sp-object on the simple way

Often I have to work with absolute urls instead of the server-relative that I can get from SharePoint-Objects like for e.g. SPContext.Current.ListItem.Url that returns me something like "Pages/subfolder1/publishingpage1.aspx".

Until now I cumbersome concatenated the different url-parts to get the absolute path but I found a much more simple method that relieves that task: just use the MakeFullUrl-Method of your SPSite-Object. Simple reference the server-relative URL as parameter and get the full path.

// for e.g.: returns http://server/sitecollection/Pages/folder/publishingpage.aspx if executed in the code-behind of a PublishingPage
string absoluteUrl = SPContext.Current.Site.MakeFullUrl(SPContext.Current.ListItem.Url);