Using a SharePoint Content Type to merge data into a Word 2007 Document


I was recently tasked with the job of replacing a K2.NET 2003 Workflow that used some Word Automation to merge some data together and produce a document.  The C# code that did the automation ran on a x32 SharePoint 2007 Server but did not function whatsoever on the x64 platform, and besides server-side automation is not recommended anyways.

I created a new Content Type in SharePoint 2007 with a Parent of Document

image

added the new site columns that I needed to hold the data I wanted to put into the document

image

I then created a new document library, went into Settings | Document Library Settings | Advanced Settings and selected “Yes” for “Allow management of content types?” and pressed “OK”.

image

Under Content Types I selected “Add from existing site content types” and chose the Content Type I created above.

image

Once my Content Type is added I return to my Document Library and select New | FormLetter

image

This will open Word 2007 with a new empty document that includes the properties from your Content Type

image

Save the .docx to your local computer and then you can add the server properties to your document by selecting Insert | Quick Parts | Document Property and select the field you would like to put into the document

image

Now after you’re done you save the file (again to your local computer), we go back to edit the Content Type settings: Site Settings | Site Content Types | FormLetter | Advanced settings

image

Upload the .docx file you saved earlier as the new template.  Now for some code to access all this, here is a snippet from my workflow

using (SPWeb currentTeamSite = workflowProperties.Web)
{

     SPListCollection lists = currentTeamSite.Lists;
     SPDocumentLibrary docLib = (SPDocumentLibrary)lists["Form Letters"];

     SPContentType CType = docLib.ContentTypes["FormLetter"];

     SPFileCollection fileCollection = currentTeamSite.GetFolder(docLib.RootFolder.Url).Files;
     SPFile template = currentTeamSite.GetFile(CType.DocumentTemplateUrl);
     SPFile file = fileCollection.Add("Form Letter" + ".docx", template.OpenBinary(), true);

     file.Item["ContentTypeId"] = CType.Id;
     file.Item["Title"] = "My Form Letter";

     // fields required by the Content Type
     file.Item["ClientFirstName"] = “Some Text”;
     file.Item["ClientLastName"] = “Some Text”;
     file.Item["ClientCompanyName"] = “Some Text”;
     file.Item.Update();

}

Now if you want to provide some support for Word 2003 in all of this I will post that to my next blog post on the subject.

About Wes MacDonald

Wes MacDonald is a DevOps Consultant for LIKE 10 INC., a DevOps consulting firm providing premium support, guidance and services for Azure, Microsoft 365 and Azure DevOps.

5 Responses to “Using a SharePoint Content Type to merge data into a Word 2007 Document”

  1. Thanks for posting this. Very interesting stuff. Definitely a good alternative to Infopath for certain situations. How would this work in word 2003 though? I suppose one could use fields instead but I think that they don\’t update automatically.

    Like

  2. To make this work for Word 2003 I save the document in Word 2003 XML format and do a string replace for the fields we care about. Now by fields I mean placeholders I have created like this {ClientFirstName} so using the column names in the content type and iterating through them I can easily replace them with real data.

    Like

  3. I appreciate the tip. Thanks.

    Like

  4. How to insert images in word documents using this approach.

    Like

    • Hi,

      If you are still on MOSS 2007 I don’t really have an answer for you, if your environment is SharePoint 2010 then I would say you could use the OpenXML API to do it.

      Hope that is of some help.

      Wes

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: