Friday, May 25, 2012

Get Current page’s Url. Request.Url vs SPUtility.GetPageUrlPath

SharePoint 2010: Get Current page's Url. Request.Url vs SPUtility.GetPageUrlPath

Use SharePoint's SPUtility.GetPageUrlPath

Few days back while I was working on SharePoint application page, I had needed to get the current page's url. I'm used to using Request.Url to get the current page url and used Request.Url in several places. But interestingly, when I tried to get the current page url from application page, it was giving me the root web's url (likehttp://mysharepoint/_layouts/pages/mypage.aspx) whereas my actual page's url was something like http://mysharepoint/subsite/_layouts/pages/mypage.aspx. The Request.Url was just skipping the SPWeb name.

Then I tried the SharePoint's SPUtility.GetPageUrlPath and found the actual url. Though the Request.RawUrl returns the relative url which includes the subsite name but it seems safe to use SPUtility.GetPageUrlPath method to get current page's url to make sure the page is valid and we can trust SharePoint API more in SharePoint context rather than fully relaying on built-ins.

Shahzad Afzal

No comments:

Popular Posts