Skip to content

SPSite.OpenWeb throws System.IO.FileNotFoundException: There is no Web named “/sites/sitecoll”.

May 25, 2011

Even if the situation is quite straightforward it is easy to run into the following error which is thrown by SPSite.OpenWeb():

System.IO.FileNotFoundException: There is no Web named “/sites/sitecoll”.

At first it may look strange that the good old SPSite.OpenWeb() cannot open a web that evidently exists but if we look at the code closer we see that it’s not the OpenWeb’s fault

The below code fails with this error even if the site exists at http://server/sites/something

using (SPSite site = new SPSite("http://server/")) 
{ 
	using (SPWeb web = site.OpenWeb("/sites/something"))
	{
		//ERROR
	}
}

The problem here is that the ‘something’ is not a site (SPWeb) of the root site collection but a separate site collection (SPSite) so the OpenWeb method of the root site collection won’t be able to open it. We need to restructure the code a bit to work:

using (SPSite site = new SPSite("http://server/sites/something"))
{
	using (SPWeb web = site.OpenWeb())
	{
		//OK
	}
}

Notice that the two site variables in the above two examples reference different site collections. The first represents http://server/ while the second points to http://server/sites/something

Advertisements
2 Comments
  1. shamala permalink

    Thanks a ton for the solution

  2. Thanks for this post.

    You saved my day !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: