How get SPWeb object from web exact url
Hi,

I have site collection object and its sub-web exact url. I want to get SPWeb object by using exact web url. I try SPSite.OpenWeb(string,bool) function by passing this excat url, but it throw an exception ie invalid character near ':'.

SPSite.url =http://servername/site1

subsite url is http://servername/site1/site2/site3. I need web object against subsite url.
November 18th, 2009 6:06pm

If site3 is a sub site then the following code should bring back the SPWeb for it

 using(SPSite site = new SPSite("http://servername/site1/site2/site3"))
{
      using(SPWeb web = site.OpenWeb())
      {
                                   
       }
    
}
Free Windows Admin Tool Kit Click here and download it now
November 18th, 2009 8:03pm

Hi Ani,

just calll SpSite.OpenWeb(" http://servername/site1/site2/site3 ") or


SPSite.OpenWeb("/site2/site3")

See here:
http://msdn.microsoft.com/en-us/library/ms474633.aspx

regards,
Markus
  • Edited by Markus I_ Wednesday, November 18, 2009 9:00 PM
  • Marked as answer by Chengyi Wu Friday, November 20, 2009 12:46 AM
November 18th, 2009 8:05pm

Hi Markus,


Per definition, the SPSite.OpenWeb method:
"Returns the site that is located at the specified server-relative or site-relative URL."
And the parameter is "a string that contains either the server-relative or site-relative URL of the site. A server-relative URL begins with a forward slash ("/"), while a site-relative URL does not begin with a forward slash."

So you cannot call that method with an absolute URL like your suggestionSPSite.OpenWeb("http://servername/site1/site2/site3"). Or to be exact, you can, but it will probably give you an AgumentException.

AFAIK there is no such method as SPWeb.OpenWeb.

Peter
  • Marked as answer by Chengyi Wu Friday, November 20, 2009 12:46 AM
Free Windows Admin Tool Kit Click here and download it now
November 18th, 2009 8:15pm

Peter, I've wanted to mention SPSite, not SPWeb, of course. thanks!
  • Edited by Markus I_ Wednesday, November 18, 2009 9:17 PM
  • Marked as answer by Chengyi Wu Friday, November 20, 2009 12:46 AM
November 18th, 2009 9:02pm

Hi Markus,

That is better! :-)

Peter
Free Windows Admin Tool Kit Click here and download it now
November 18th, 2009 9:06pm

Hi,

Thanks for the reply. Steven's answer help me.

I also learned one more thing here. By providing your current web's exact url in SPSite constructor will create an instance of site of current web. But site.OpenWeb() will return the web object corresponding to provided url, not the web object of site url.


using(SPSite site = new SPSite("http://servername/site1/site2/site3"))
{
using(SPWeb web = site.OpenWeb())
{

}

}

Here site Url is http://servername/and site.OpenWeb() return SPweb object against "http://servername/site1/site2/site3".

November 20th, 2009 12:02pm

If you want to use SPSite.OpenWeb() then you need the server relative URL. Here is one way to get it:

string url = http://something.com/subsite/subsite/subsite/subsite;

using(SPSite oSite = new SPSite(url))

{

//Funny enough this gets you the root site-collection (SPSite) regardless of how many levels deep the url actually goes.

string t = url.Replace(oSite.Url."");

t = oSite.ServerRelativeUrl + t;

// t now equals '/subsite/subsite/subsite/subsite', which is the sever relative url to the SPWeb you started with.

SPWeb oWeb = oSite.OpenWeb(t);

}

Free Windows Admin Tool Kit Click here and download it now
April 17th, 2015 12:55pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics