Friday, April 15, 2011

SharePoint: The List That is Referenced Here no Longer Exists

While updating list items in a SharePoint list using the SharePoint web service API and specifically through calling
UpdateListItems method you may get everything setup correctly (authentication, authorization, retrieving the list using GetListItems etc.) but when you attempt to update the list using the API your updates are not reflected in the list and when poking around the returned object from UpdateListItems you'll notice it contains the detailed error message "The list that is referenced here no longer exists."

If that's the case make sure you are referencing the correct subsite and not the main site when you create the web reference to Lists.asmx web service. Alternatively you could set the Url property programmetically.

Here is an example:

If your site URL is http://my_share_point_site, the list.asmx can be found at http://my_share_point_site/sites/main/_vti_bin/lists.asmx?WSDL and you could use that URL to reference the API and some things might work. When you attempt to update a list found under some sub_site, your updates will silently fail. You have to reference http://my_share_point_site/sites/main/sub_site/_vti_bin/lists.asmx?WSDL for updates to your.

Some code:

string fieldName = "SomeListFieldToUpdate";
 SPLists.Lists listService = new Lists();
 listService.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
 listService.Url = "http://my_share_point_site/sites/main/sub_site/_vti_bin/lists.asmx?WSDL";
 /* setting Url not required except you referenced a different sub_site or the main site when 
 you created your web reference to Lists.asmx */
 XmlDocument batchDoc = new XmlDocument();
 XmlElement batchElement = batchDoc.CreateElement("Batch", "http://schemas.microsoft.com/sharepoint/soap/");
 batchElement.SetAttribute("OnError", "Continue");
 batchElement.SetAttribute("ListVersion", "21");
 batchElement.SetAttribute("ViewName", editListViewGUID);
 string xml = string.Format(@"        
   {0}        
   {2}
   ", ID, fieldName, fieldValue);
 batchElement.InnerXml = xml;
 var result = listService.UpdateListItems(listGUID, batchElement);
 int.TryParse(result.FirstChild.FirstChild.InnerText, out errorCode);
 return errorCode;

5 comments:

  1. Dude, you saved my behind :)

    I had the same problem one day after it had worked. I was looking at everything, except at the changed url I fed the listservice. Thanks man! :)

    ReplyDelete
  2. You are a life saver. I had the exactly same problem now it is working after URL change.

    ReplyDelete
  3. You helped me a lot! Saved my day :)

    ReplyDelete
  4. Whohoo! I was stuggling with this too... this was such a simple fix! Thanks!

    ReplyDelete