Exporting a SharePoint Site Collection to XML


This is a repost of something I did on the MSDN SharePoint 2010 forums covering a method for exporting data in a SharePoint site collection to XML. Note that this example does not cover exporting documents or storing navigation/web part settings. It just exports structure and all list items. So if anyone else has a use for this they can use this as a starting place. :)

  1. SPSite site = new SPSite(string url);
  2. Console.WriteLine("<Site>");
  3. foreach(SPWeb web in site.Webs)
  4. {
  5. Console.WriteLine("<web title=\"" + web.Name + "\">);
  6. foreach(SPList list in web.Lists)
  7. {
  8. Console.WriteLine("<list title=\"" + list.Title + "\">);
  9. SPFieldCollection fields = list.Fields;
  10. foreach(SPListItem item in list.Items)
  11. {
  12. Console.WriteLine("<item>");
  13. foreach(SPField field in fields)
  14. {
  15. //note put a check for null field values here
  16. Console.WriteLine("<property title="\" + field.Title + "\" value=\"" + item[field].ToString() + "\"/>);
  17. }
  18. Console.WriteLine("</item>");
  19. }
  20. Console.WriteLine("</list>");
  21. }
  22. Console.WriteLine("</web>");
  23. }
  24. Console.WriteLine("</site>"
  1. #1 by Sandeep on April 30, 2011 - 1:46 pm

    Content Structure report in sharepoint is also a good view to look at the complete site collection one shot

    • #2 by Maarten on May 2, 2011 - 1:25 pm

      The content structure report is super handy, but doesn’t go down to the list item level which the code snippet above does.

(will not be published)


%d bloggers like this: