OpenWeb(String) method
- by ybbest
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>0×80070002</nativehr><nativestack></nativestack>There is no Web named / http://win-oirj50igics/RestAPI”.
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<Song> sortedItems =
from item in items.OfType<SPListItem>()
orderby item.Title
select new Song {SongName = item.Title, SongID = item.ID};
songs.AddRange(sortedItems);
}