Monday, October 18, 2010

SharePoint 2010: Get Current page’s Url. Request.Url vs 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 (like http://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 asp.net built-ins.

No comments:

Post a Comment