Home > SharePoint 2010 > OpenWeb(String) method

OpenWeb(String) method

I guess this is a SharePoint beginner problem ,however it took me a while to figure out what the problem is and I will blog it to help me to remember. Basically I wrote the following code to grab some list item from my SharePoint subsite http://win-oirj50igics/RestAPI,however I got the error stating that :

“<nativehr>0x80070002</nativehr><nativestack></nativestack>There is no Web named / http://win-oirj50igics/RestAPI&#8221;.

The problem is that OpenWeb(String) method returns the web site that is located at the specified server-relative or site-relative URL. It is the relative URL , so after I changed http://win-oirj50igics/RestAPI to RestAPI, everything works fine.

using (SPSite site = new SPSite(http://win-oirj50igics/))
{
SPWeb web = site.OpenWeb("http://win-oirj50igics/RestAPI");
SPQuery query = new SPQuery();
query.Query = camlDocument.InnerXml;
SPListItemCollection items = web.Lists["Songs"].GetItems(query);
IEnumerable sortedItems =
from item in items.OfType()
orderby item.Title
select new Song {SongName = item.Title, SongID = item.ID};
songs.AddRange(sortedItems);
}

Update 29 Feb 2012

One of my co-worker had very strange issue to today with openweb method.He is getting the same error, however he tried all the fixes specified about , nothing worked.What is more interesting is if he press F5 to debug the code(put a break point before try to get list , it will work)If you simply run the code , it does not.Finally we figure out the issue , it is because he is open a web that is the root web of another site collection.After we change the site url , everything worked.

using (SPSite site = new SPSite("http://YBBEST"))
 {
//http://ybbest/sites/hr is a site collection , not a subsite of the ybbest.
//change ybbest to ybbest/sites/hr and use openrootweb make it work.
 using (SPWeb web = site.OpenWeb("sites/HR"))
 {
 SPList spList = web.Lists.TryGetList("Tasks");
 if (spList != null)
 {
 Console.WriteLine("Success!");
 Console.WriteLine(string.Format("Title : {0}", spList.Title));
 Console.WriteLine(string.Format("Task Status : {0}", spList.Items[0]["Status"]));
 }
 else
 {
 Console.WriteLine("List does not exist !");
 }
 }
 }
Advertisements
Categories: SharePoint 2010
  1. No comments yet.
  1. No trackbacks yet.

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: