13 January, 2010

Dumping SPList Items to a Feature

I’ve been working for a while on a project and filled up the Site Collection Images list with a lot of images get the user experience going in my virtual dev environment. When we got access to the customer environment I wasn’t in the mode to manually upload all the images again. So I wrote the following little console application that dumps out the content from the Site Collections Images list. The output from the app is:

  • Feature.xml – file containing the feature def and all elements
  • images.xml – file containing the Module with the images
  • Images folder – folder containing all the image files from the list

Feature_Structure

Then I copied the generated artifacts to my solution and built the wsp package. This code might not be very useful as is but will perhaps make a point on how this technique can be used.

Before going in to the code one note is worth making is that everything is hard coded, including the guid ID. I know it beforehand so I could do that, if you’re adopting this to something more generic you probably want to generate the ID during runtime.

The code otherwise is pretty simple. Open the list, loop over it and generate a Feature.xml and an elements.xml file and dump all the files into a folder.

Here’s the code:

using System;
using System.IO;
using System.Text;
using Microsoft.SharePoint;
namespace ReverseSiteCollectionImages {
    internal class Program {
        private static void Main(string[] args) {
            using (var site = new SPSite("http://stockholm:666")) {
                SPList siteCollectionImagesList = site.RootWeb.Lists["Site Collection Images"];
                var featureBuilder = new StringBuilder();
                var imagesBuilder = new StringBuilder();

                //Init feature XML
                featureBuilder.AppendLine(" ");
                featureBuilder.AppendLine(" ");
                featureBuilder.AppendLine("<elementmanifests> ");
                featureBuilder.AppendLine("<elementmanifest location="\"images.xml\"/"> ");

                //Init Elements XML
                imagesBuilder.AppendLine("<elements xmlns="\"http://schemas.microsoft.com/sharepoint/\"">");
                imagesBuilder.AppendLine( "<module name="\"Images\"" path="\"Images\"" rootwebonly="\"TRUE\"" url="\"SiteCollectionImages\""> ");

                //Itterate over images in list
                foreach (SPListItem item in siteCollectionImagesList.Items) {
                    //Write xml to feature.xml and elements.xml
                    featureBuilder.AppendLine("<elementfile item="" location="\"Images\\""></elementfile>");
                    imagesBuilder.AppendLine("<file item="" name="\""" type="\"GhostableInLibrary\"" url="\"""></file>");

                    //Get the file...
                    SPFile file = item.File;
                    Stream stream = file.OpenBinaryStream();

                    if (Directory.Exists(".\\Images") == false) {
                        Directory.CreateDirectory(".\\Images");
                    }

                    //... and persist it to disk
                    using (var fileStream = new FileStream(".\\Images\\" + item.Name, FileMode.Create, FileAccess.Write)) {
                        int Length = 256;
                        var buffer = new Byte[Length];
                        int bytesRead = stream.Read(buffer, 0, Length);

                        while (bytesRead > 0) {
                            fileStream.Write(buffer, 0, bytesRead);
                            bytesRead = stream.Read(buffer, 0, Length);
                        }
                    }
                }

                //Finish up xml
                imagesBuilder.AppendLine(" </module> ");
                imagesBuilder.AppendLine("</elements>");
                featureBuilder.Append("</elementmanifest></elementmanifests> ");
                featureBuilder.Append(" ");

                //Flush xml files to disk
                using (var featureWriter = new StreamWriter(".\\Feature.xml", false)) {
                    featureWriter.Write(featureBuilder.ToString());
                    featureWriter.Flush();
                }

                using (var imagesWriter = new StreamWriter(".\\images.xml", false)) {
                    imagesWriter.Write(imagesBuilder.ToString());
                    imagesWriter.Flush();
                }
            }
        }
    }
}

Tags: ,