ross_ritchey
ross_ritchey

Reputation: 41

c# Open XML SDK update attached template in DOCX

I am opening existing .docx files from a SharePoint Document Library over the SharePoint web services, and am attempting to attach a new Template to them. The current code for this piece seems to not be doing anything at all.

XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
OpenXmlPart documentSettingsPart = document.MainDocumentPart.DocumentSettingsPart;
XDocument documentSettingsXDoc = documentSettingsPart.GetXDocument();

documentSettingsPart.AddExternalRelationship("http://schemas.openxmlformats/org/officeDocument/2006/relationships/attachedTemplate", new Uri(_outLibraryTemplate, UriKind.Absolute));

using (XmlWriter xw = XmlWriter.Create(documentSettingsPart.GetStream(FileMode.Create, FileAccess.Write)))
    documentSettingsXDoc.Save(xw);

Does anyone have any thoughts as to why this isn't working - and what I need to do to get this going?

Upvotes: 1

Views: 1164

Answers (1)

bg6471
bg6471

Reputation: 140

This may help. It creates a new docx file from a dotx file.

I modified it a little for my own use - I added the external relationship (a dotm) to an existing file. Unfortunately I can't work out yet if I can easily programatically update the styles without having to actually open the file.

https://web.archive.org/web/20150716111136/http://blogs.msdn.com/b/vsod/archive/2012/02/18/how-to-create-a-document-from-a-template-dotx-dotm-and-attach-to-it-using-open-xml-sdk.aspx

Upvotes: 1

Related Questions