Salvete! I have a web-part (sp 2010) with a web-page viewer that loads a custom aspx page. I need to get the url of the current subsite from the aspx page. In other words, if the path in my browser is http://myportal/mysite/mysubsite/default.aspx
, I want to get mysubsite
.
Problem is that references to SPContext.Current.Web.Url
always get the address relative to the page I am loading instead of the actual parent of the web-part.
Please help!
I have consulted these threads to no avail:
- how to get current URL in SharePoint?
- Get the current site url
- https://stackoverflow.com/questions/1544268/sharepoint-get-the-full-url-of-the-current-page-in-code-behind
[update]
Okay, folks, a little progress here, but not yet a viable solution.
I found that System.Web.Request.UrlReferrer.ToString()
will get the parent's url the first time the aspx page is loaded along with the parent page. It will also get the url of the parent time each time the page is refreshed.
However, after that, it only loads the url of the aspx page and not the parent. So I tried parsing the url with regex to get the subsite, which works. Then I put that into a cookie that is saved on the page_load sub, just after the url is parsed and the subsite name is gotten. --->This lets me work with that variable a little bit more. I think it is due to the same issue that I can only get that cookie's value one time. Look:
Public Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
dim fullurl as string = Request.UrlReferrer.ToString() 'gets full url of the page
Dim r As Regex = new Regex(myRegexPattern, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
Dim m As Match = r.Match(fullurl)
myVar = m.value
myVarcookie = New HttpCookie(myVarcookieName, myVar)
Response.Cookies.Add(myVarcookie)
myVarLabel.Text = myVar
End Sub
And here is my code for retrieving the cookie:
Public Function GetmyVarCookie() as string
dim retval as string = Request.Cookies("myVar").Value
response.write("cookieval = " & retval)
return retval
end function
And then I call this code on the button on my aspx page:
public Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
myVar = GetmyVarCookie()
response.write("myVar=" & myVar)
end sub
Maybe I am doing something wrong with the cookies?
[update]
Okay, the first time I save a cookie, it saves it under the parent domain such as my.site.com
. But if I interact (submit) with any button in the aspx page, (asp.net buttons) then the code begins to read things from the stance of the aspx page instead. This means that any cookies now being read from .my.site.com
- notice the dot in front. So I seem to have gotten it to work by saving the cookie with a domain of .my.site.com
. Now I can get that information either on page load OR on submit.
If there were a better way of getting the full url, that would be the optimal solution.
[Conclusion]
- We have an aspx page from a virtual directory loaded into a page-viewer web-part on a different subsite (same domain).
- On first page_load of the aspx page, the parent url can be gotten with
Request.UrlReferrer.ToString()
- On second page_load, you can only get the url of the aspx page because the referrer has changed (I don't know why).
- Save a cookie the first time around with your url, but save it to
.my.site.com
with the dot in front. - Now you can get the url from the cookie from inside your aspx page.
[update] What I have done above is still a workaround and not a real solution. I still welcome any solutions. There should be a way to use asp.net to get the url of the current site from the browser after the page is loaded. Before postback, I can use a normal means of getting the subsite, as several answering folk below have pointed out, but after postback, the aspx page in the web-page-viewer webpart only gets its own url. Has noone else experienced this?
[update] Ah! Here is someone with the same issue: https://stackoverflow.com/questions/2543162/get-page-url-by-using-a-program-within-a-pageviewerwebpart-in-sharepoint/10456248#10456248