Вопрос

I create a SPWeb object with a relative url to my SPSite

using (SPWeb web = SPContext.Current.Site.OpenWeb(url))
{
    SPList centralManagementList = web.Site.RootWeb.Lists["mylist"];
    string id = web.ID.ToString();
}

With this object I can create new object like a SPList (no errors)

When i access the ID of the SPWeb object, i get the error, that the site does not exists?! But it does - I tried relative to the SPSite and relative to the SPServer.

What's wrong?

Это было полезно?

Решение

The url can be either SPSite relative (not starting with /)
Or Server Relative (starting with /)
But it has to be a subsite of the SPSite you start with

Usually I use a full url and do it like this:

using (SPSite site = new SPSite(url)) 
  using (SPWeb web = site.OpenWeb()) 
  { 
    SPList centralManagementList = web.Lists["mylist"]; 
    string id = web.ID.ToString(); 
  } 
}

then you're sure to get the right SPSite and SPWeb

Note that in your code you're refering to web.Site.RootWeb when using the List
web.Site.RootWeb will be valid even if web.Exists is false as it knows which SPSite you're refering to from the OpenWeb call even though it didn't allocate a fullblown SPWeb

Другие советы

Try to use this method

public SPWeb OpenWeb(
    string strUrl,
    bool requireExactUrl
)

Like this

using (SPWeb web = SPContext.Current.Site.OpenWeb(url, true))
Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top