Get SharePoint to Automatically Read and Write XML With XMLParser

Have you ever wondered how the property promotion used by SharePoint to extract and write InfoPath form values to library columns works? XMLParser is the answer: http://msdn.microsoft.com/en-us/library/aa544164.aspx

You can use the exact same technique in your own solutions to read and write xml content by updating list item values. This works due to the power of content types. All hail the content type!

The content type assigned to your library will define the columns (FieldRefs) that your xml will inherit. By extending the definition of the FieldRef elements to include a Node attribute we can map the field to a xml node via XPath.

Sample content type definition that maps MyLabel and MyValue fields to XML nodes:

image

Notice that the MyValue FieldRef also includes an Aggregation attribute. These can be used to perform mathematical functions on values (see link above for list of available functions). The XPath above I’m using reaches into the XML schema of the uploaded files and identifies where in the XML files these fields should be mapped to. Here’s the very basic XML I’m using in this demo:

image

Lastly, there is one more important FieldRef that needs to be included in your content type definition:

image

This FieldRef is used by the XMLParser to ensure that the appropriate content type ID is written to the XML file. In turn, the XML files uploaded to SharePoint must also contain the <?MicrosoftWindowsSharePointServices ContentTypeID="0x0101007438f6c6e5834860a94a8284a8c7106c"?> element which is then used to identify which content type the XML file should be mapped to. The content type ID specified in the FieldRef element and the XML must match for the XMLParser to work. Finally, this FieldRef must use the ID of {4B1BF6C6-4F39-45ac-ACD5-16FE7A214E5E} – this is the internal field ID for the content type site column.

Note: Replace the content type Id in my demo with your own content type id. Do not replace the FieldRef ID.

Once your content type is fully defined, deploy it to SharePoint, create a library to host your content type and then upload an XML file that includes the <?MicrosoftWindowsSharePointServices processing instruction described above.

The results should be instantly visible, before I’ve even confirmed the document upload, the XMLParser has executed, found my processing instruction in the xml file, found the same content type assigned to my library and performed the mappings defined by my FieldRefs:

image

Now my file is uploaded, the vales extracted from the XML are available to me in the list:

image

If I update the list item, and then download the xml document, the XMLParser will ensure that any new values entered into my list item columns are then written back to the underlying XML before they are downloaded, thus ensuring the list item values and XML values remain in sync.

The full content type definition used in this demo:

image

The full sample xml used in this demo:

image

Both these files can be downloaded from my SkyDrive: https://skydrive.live.com/?cid=941d17eca8c6632d&sc=documents&uc=2&id=941D17ECA8C6632D%21351#

Enjoy!