Question

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:

[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

Was it helpful?

Solution 5

The reason the url gets lost is on account of the postback. The postback is caused by the submit action on a button. The codes offered by the nice people in the other answers would suffice if there were no postback issue, which would be the case in a normal sharepoint page. However, the page is in a webpart, and is doing a postback to itself, so, when it posts back to itself, it loses reference to the parent page that loaded it.

Here is a viable solution:

Make the page in question able to receive query parameters in its url. Then, in the codebehind you can add query parameters to the postbackurl property of the button.

In this fashion, you can make the page pass variables back to itself. Now the page will know its subsite address.

like this:

dim myparameter as string = GetSubsite() 'whatever method of getting the url name at page_load time - remember, it works when the entire sharepoint site is first loaded.
submitbutton.PostBackUrl = "index.aspx?myparam=" & myparameter

OTHER TIPS

Have you tried:

System.Web.HttpContext.Current.Request.Path

You can use web.URL property... for ex: SPSite oSite=new SPSite("http://yourserver/site1"); SPWeb oWeb=oSite.OpenWeb("site1");

oWeb.URL will give you the URL of the web...

You could just pass the url as a query string parameter to the page in the subsite. Wouldn't something like this work? http://myportal/mysite/mysubsite/default.aspx?refurl=http://myportal/mysite

In your code on the subsite page, you can read the querystring and create your site object using that. The query strings should be retained on postback.

UPDATE: Maybe I misunderstood the problem. As I understand it now, you are trying to get the url of the subsite in which the url resides... Try the following:

using (SPSite siteCollection = new SPSite("http://MyServer/MySite/MySubSite/MyDocLib/Page.aspx"))
{
SPWeb myWeb = siteCollection.OpenWeb();
string weburl = myWeb.Url;
}
SPWeb parentWeb = site.ParentWeb;
LbParentWebData.Text = parentWeb.Name;

is this what you need?

You need to use SPContext.

SPContext.Current.Web.ServerRelativeUrl

will return "/mysite/mysubsite". SPContext.Current.Web is your child site, which is the parent of the web part. One of the properties of SPContext.Current.Web should give you exactly what you want.

SPContext for SP2010 on the Microsoft site: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontext(v=office.14).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top