2

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

2
  • Just refer this link: stackoverflow.com/questions/1544268/…, it gives the answer to your question. Commented Apr 10, 2012 at 5:04
  • No, I have visited that page, already, and all of those solutions give me the url of the aspx page, not the parent of the web-part.
    – bgmCoder
    Commented Apr 10, 2012 at 14:50

6 Answers 6

2

Have you tried:

System.Web.HttpContext.Current.Request.Path
1
  • Yes, it won't get the address of the parent. It only gets the address of the page in the page-viewer part.
    – bgmCoder
    Commented Apr 10, 2012 at 4:25
1

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...

3
  • It will give the url - but only the url of the aspx page loaded in the web-part, not the url of the parent.
    – bgmCoder
    Commented Apr 10, 2012 at 15:59
  • oWeb.URL will give you.. http:// yourserver/site1.. Here the parent is '/'
    – Strider
    Commented Apr 12, 2012 at 3:29
  • I don't get that - my problem is that I don't already know "site1" in the code. I need code that will find the web that follows "myportal/mysite" - that is, "mysubsite" as in my original post. It is not the same thing as extracting a section of the url from a given url - I need to get the url in the first place.
    – bgmCoder
    Commented Apr 26, 2012 at 16:53
1

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;
}
4
  • Hmmm... No, because I can't pass the url to the page in the web-page-viewer. I would have the same issue. There isn't much difference in getting the full url or just the query.
    – bgmCoder
    Commented Apr 30, 2012 at 21:22
  • 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("MyServer/MySiteCollection/MySubSite/MyDocumentLibrary/…)) { SPWeb myWeb = siteCollection.OpenWeb(); } Commented May 1, 2012 at 20:37
  • Thanks for helping out! The page that needs the real url is an aspx page inside a web-page-viewer. Your answer is the same as the one Paddy has suggested, and has the same issue, which I have described in my post - it only works when the parent page is first loaded. Once I interact with the aspx page in the wpv, it won't get the url of the sharepoint subsite anymore. Instead it gets the url of the very aspx page. Everybody has suggested the normal means of getting the urls, but it only works one time. I can't believe I am the only one having this problem!
    – bgmCoder
    Commented May 2, 2012 at 15:09
  • Okay, I think I got it (take a look at my answer). But I gave you my vote for the original idea of using a query parameter.
    – bgmCoder
    Commented May 5, 2012 at 22:39
0
SPWeb parentWeb = site.ParentWeb;
LbParentWebData.Text = parentWeb.Name;

is this what you need?

1
  • Where do I get "site" ?
    – bgmCoder
    Commented Apr 10, 2012 at 14:42
0

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
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.