The integration between SharePoint and Microsoft office contiunes to improve with every successive release, but as of Office 2010 and SharePoint 2010, there is still a bit of feature gap when it comes to automation.
When a document originates from within SharePoint it is not too bad – you have to do a little CustomXMLPart trickery to set the properties. But what if your file is a SharePoint virgin?
The adding a document to sharepoint adds at least 3 additional Custom Xml Parts to the Word Document – one of them big, complex and guid-filled. To generate these parts would be a bit of a nightmare.
Turns out, you don’t need all of the xml to pre-populate fields. Nor do you need any guids. Here is all the XML you need:
<?xml version="1.0"?> <p:properties xmlns:p="http://schemas.microsoft.com/office/2006/metadata/properties" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls"> <documentManagement> <SharePoint_x0020_ColumnName>Text Value</SharePoint_x0020_ColumnName> </documentManagement> </p:properties>
I will leave it to you to figure out the column names (hint – spaces become “_x0020_”).
Store that xml bit in a string (say, xmlString), and call Document.CustomXMLParts.Add(xmlString);
Now, when the document is saved to sharepoint, the fields will be pre-populated, all the appropriate xsd and guids updated, and the user gets no errors about missing required fields!