Thursday, January 8, 2009

Modifying Open XML Documents that are in SharePoint Document Libraries using Web Services

 

Modifying Open XML Documents that are in SharePoint Document Libraries using Web Services

[Blog Map]

When using the Open XML SDK with SharePoint web services, one of the most basic operations is to get a document from a document library using web services, modify it using the Open XML SDK (and LINQ to XML), and save it back to the document library. This post describes how to do this, and provides a sample in C#.

It is simple to extend this sample to iterate through all documents in a library, apply some changes to each one, and save them back. In an upcoming post, I’ll present a sample to ‘sanitize’ (remove comments, accept revisions, and remove personal information) all documents in a document library. This is a pretty useful. I keep a library of documents that I send externally as needed, and it’s always best to not have personal information embedded in the documents. By running this sample, I can regularly check to make sure that the document library is clean.

For a brief tutorial on SharePoint web services, see “Getting Started with SharePoint (WSS) Web Services using LINQ to XML”. For this example, you need to add two references to web services (both Lists and Copy). The procedure for adding a reference to the Copy web service is the same as adding a reference to the Lists web service.

This code uses the Open XML SDK. Remember to add a reference to the Open XML SDK assembly. This code uses V1 of the SDK.

The code references the System.IO.FileFormatException class, which is in the WindowsBase assembly, so add a reference to it.

This code uses the technique of converting XmlNode to XElement (and back again), as detailed in “Convert XElement to XmlNode (and Convert XmlNode to XElement)”, so that we can use LINQ to XML instead of XmlDocument.

One important aspect of the code is that you retrieve the document as a byte array:

ModifyDoc.CopyWebService.FieldInformation[] fields;

byte[] byteArray;

copy.GetItem(url, out fields, out byteArray);

After retrieving the byte array, you can write the byte array to a MemoryStream, and use the MemoryStream to open an in-memory Open XML document.  After modifying the in-memory document, you can convert it back to a byte array and serialize back to the SharePoint document library.  The technique is described in the post, “Working with In-Memory Open XML Documents”.

Here is the code to serialize it back to the SharePoint document library:

string[] urls = { url };

ModifyDoc.CopyWebService.CopyResult[] copyResults;

copy.CopyIntoItems(url, urls, fields, mem.ToArray(), out copyResults);

Now that we’ve covered these basics, we can start doing some more fancy stuff using SharePoint web services and the Open XML SDK.

Here is the complete listing (the code is added as an attachment to this post):

Eric White's Blog : Modifying Open XML Documents that are in SharePoint Document Libraries using Web Services

No comments:

Blog Archive