A reader (people read this crap?!?!?!) asked me how to return SharePoint list items in XML. It's very easy!
Say you want to run a simple return with some very simple parameter types:
-
returnListItemsXML(list.Items);
this is pretty simple! But we haven't built the returnListItemsXML method yet
Firstly, build up your method file with its name decoration and parameter types:
-
public static XmlDocument returnListItemsXML(SPListItemCollection listItems)
You can see that we are passing in an SPListItemCollection in order to to get all the items from the relevant list (which is why in the primary method we are using list.Items).
Next, you must create a new XmlDocument object in order to represent an XML document :
Next, build a small if condition in order to check whether the the list items return a null value or not. If they do not, there is something that can be loaded into the XML document.
-
if (listItems != null)
-
{
-
}
Then, load the XML document with the list items:
-
document.LoadXml(listItems.Xml);
so, afterwords you would have this:
-
if (listItems != null)
-
{
-
document.LoadXml(listItems.Xml);
-
}
otherwise, just return the document:
-
return document;
That's it! Very simple indeed











June 24th, 2007 at 5:56 pm
[...] Return Your SharePoint List Items In XML [...]
August 4th, 2008 at 11:23 pm
Hi, can you provide the full sample code for this? Thank you.