Get data from a SharePoint 2010 list and generate a hierarchical XML file that can be used as a data source for a Flash movie on a site without SharePoint Designer by building a console application that can be launched from a button on a page or from a workflow.
In effect your app needs to perform the following automation tasks:
- Get a flat XML file of the data
- Convert the flat XML file into a hierarchical file
- Save the file as an XML document and upload it to a SharePoint document library
Step 1 – Get data to a flat XML file
The first step is to get data from a SharePoint list and store it as XML in memory. This could be done in one of several ways: SPList class and a CAML SPList class and LINQ query, Web Services, GetListItems, or the URL protocol of the RPC method. This solution will use URL Protocol, because it’s a simple and transparent way to access the data. This step also needs to:
- Get the XML output from a special View of the list rather than the default view of the list, so all of the columns can be shown all of the time without affecting data entry or management
- Trim off any extraneous characters from the values that come from SharePoint lists, like dates, URLs, or strings delimited with “;#”
- Resolve a potential SP2010 security exception when accessing from the localhost
- Build a valid URL to the document, regardless of whether it is a PDF or a Web link
Step 2 – Construct hierarchical XML document
Read (aka query) the in-memory XML and reconstruct the a collection of elements and attributes into a hierarchical XML output using LINQ to XML. Note that the results of one LINQ query expression can be the input of another LINQ expression. Code it in a way that matched up with the XML output to make it easier for those that follow. And a few other things:
If item is set to “Show” in the list then show it, otherwise hide it.
Sort the output by SectionId rather than by SectionTitle to conform to the SWF design and match the existing XML exactly
Yep, there’s another way to do it that groups the top level, but we won’t do that here. It looks something like this …
Step 3. Save XML document to document library
This is where the Client Object Model comes in. You’ll also use System.IO for memorystream.
Results
This code feeds a Flash movie that allows users to dial in on documents by a predefined taxonomy on a SharePoint site. What once required hours of manual XML file edits and uploads can now be done in one click, and the door is now open to automating the entire document upload, approval and management process using workflows and InfoPath web forms. Presumably, this kind of approach can also be used with other kinds of animated web parts that use XML as a data source.
