Second Life of a Hungarian SharePoint Geek

June 11, 2009

Columns missing when using the Lists.GetListItems SharePoint web service

Filed under: SharePoint, Web service — Tags: , — Peter Holpar @ 03:21

Note: This is a repost, the original one was published on SharePoint Blogs on June 11, 2009

More people complained about that the result of the Lists.GetListItems method call does not contain the empty fields, so the resulting XML cannot be load into a DataSet:
http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/95855246-b8f8-4dda-897a-c4480cc74044/#0a2d42ed-7365-413c-a12f-ccbf95025c12
http://www.tech-archive.net/Archive/SharePoint/microsoft.public.sharepoint.portalserver.development/2006-09/msg00056.html

A dirty workaround for that issue may be to adding the missing attributes to the XML from code before trying to load into the DataSet.

In the following code I illustrate this approach. In this code I first get the columns of the default view of the list using the List.GetListAndView method, then retrieve the data using the Lists.GetListItems method, iterate through all the rows and columns, and if an attribute is missing for a column, add an empty attribute.

  1. String listName = "YourListName";
  2. listService.Credentials = new NetworkCredential("user", "password", "domain");
  3. XmlNamespaceManager nsmgr;
  4.  
  5. // get info about the default view
  6. // the 2nd parameter is null -> it is the default view
  7. XmlNode listView = listService.GetListAndView(listName, null);
  8. nsmgr = new XmlNamespaceManager(listView.OwnerDocument.NameTable);
  9. nsmgr.AddNamespace("a", "http://schemas.microsoft.com/sharepoint/soap/");
  10.  
  11. List<String> fieldNames = new List<string>();
  12. foreach (XmlNode field in listView.SelectNodes("a:List/a:Fields/a:Field", nsmgr))
  13. {
  14.     XmlAttribute attr = field.Attributes["Name"];
  15.     // it should not be null, but we check it
  16.     if (attr != null)
  17.     {
  18.         // we store all fields in a collection for later use
  19.         fieldNames.Add(attr.Value);
  20.     }
  21. }
  22.  
  23. // get data from list
  24. XmlNode items = listService.GetListItems(listName, null, null, null, null, null, null);
  25. nsmgr = new XmlNamespaceManager(items.OwnerDocument.NameTable);
  26. nsmgr.AddNamespace("z", "#RowsetSchema");
  27. nsmgr.AddNamespace("rs","urn:schemas-microsoft-com:rowset");
  28. XmlNodeList itemNodeList = items.SelectNodes("rs:data/z:row", nsmgr);
  29. foreach(XmlNode itemNode in itemNodeList)
  30. {
  31.   foreach (String fieldName in fieldNames)
  32.   {
  33.     String wsFieldName = "ows_" + fieldName;
  34.     XmlAttribute attr = itemNode.Attributes[wsFieldName];
  35.     // if the attribute is missing, we should add it
  36.     if (attr == null)
  37.     {
  38.       attr = itemNode.OwnerDocument.CreateAttribute(wsFieldName);
  39.       itemNode.Attributes.Append(attr);
  40.     }
  41.   }
  42. }
  43. DataSet listDataSet = new DataSet();
  44. // read the result into a data set
  45. XmlTextReader readerListDataSet = new XmlTextReader(items.OuterXml, XmlNodeType.Document, null);
  46. listDataSet.ReadXml(readerListDataSet);

After this kind of preparation of XML it can be loaded into the DataSet.

You should know that this approach may not scale and perform well in case of a large amount of data, and I consider it a dirty workaround, but I don’t know currently other solution for this request.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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

Blog at WordPress.com.

%d bloggers like this: