Return Your SharePoint List Items In XML

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:

[csharp]

returnListItemsXML(list.Items);

[/csharp]

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:

[csharp]

public static XmlDocument returnListItemsXML(SPListItemCollection listItems)

[/csharp]

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 :

[csharp]

XmlDocument document = new XmlDocument();

[/csharp]

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.

[csharp]

if (listItems != null)
{
}

[/csharp]

Then, load the XML document with the list items:

[csharp]
document.LoadXml(listItems.Xml);

[/csharp]

so, afterwords you would have this:

[csharp]

if (listItems != null)
{
document.LoadXml(listItems.Xml);
}

[/csharp]

otherwise, just return the document:

[csharp]

return document;

[/csharp]

That’s it! Very simple indeed :-)

Share

7 thoughts on “Return Your SharePoint List Items In XML”

  1. Thank you for your coding,
    would you please provide the whole of the coding for me, it is interesting for this, however, how to write this coding.

  2. Hi, i’m looking to do this, but am very new to c# and SahrePoint. Do you have any more information?

    Cheers keep doing what your doing!

  3. Excellent work. Somehow, I am getting an error though! Is there something I am missing?

Leave a Reply

Your email address will not be published. Required fields are marked *