0

I have a scenario in which I have to replace few contents in a word document based on the library / list input form of sharepoint 2010.

Example :

I have 2 Placeholders : [Title] and [SubTitle]

Now, if the end user, using sharepoint's default new form enters the value of Title and SubTitle as "New" and "NewTest" respectively then this should get replaced in the word document on the submission of the new form.

how to accomplish this using server side code?

Thank you

2

You can achieve this without writing single line of code if you want to do in document library(Not in list).

You can create one word template for document library and add field place holder to that file. SharePoint will update its value automatically once you update it in new/edit form.

Visit this to know how to configure it.

Hope it will help :)

  • what I want is that I already have a template created say for HR Policy System of a company , In that when the user fills the forms the content place holder should get replaces with the inputs. In this I dont want to display the document template to the user. And after successful completion of the replacement the template should be converted in pdf and there should be a separate file created. i.e. if the template is Policy1 then the edited document should be a new document with name as Policy_Leaves. – Vishwas Goswami Oct 13 '15 at 11:31
  • I want to achieve this using server side coding, with event receivers and using open xml – Vishwas Goswami Oct 13 '15 at 11:32
  • And can you tell me how to apply information rights management for a word document. – Vishwas Goswami Oct 13 '15 at 11:33
  • user will add information in list or library? – Kalpesh Vaghela Oct 13 '15 at 11:34
  • The documents will get saved in a library. – Vishwas Goswami Oct 13 '15 at 11:37
0

This is not a SharePoint specific question and it might get moved to another board. Follow the following steps and make changes where needed.

  1. Install Open XML SDK 2.5 on server. More info: here and here
  2. Add required references. More on this: here
  3. Open the document from the library:

    byte[] byteArray = File.ReadAllBytes("PATH_TO_DOCUMENT_IN_LIBRARY"); MemoryStream mem = new MemoryStream(); mem.Write(byteArray, 0, (int)byteArray.Length); WordprocessingDocument _wordDoc = WordprocessingDocument.Open(mem, true); string _w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; MainDocumentPart part = _wordDoc.MainDocumentPart; NameTable nt = new NameTable(); XmlNamespaceManager _nsManager = new XmlNamespaceManager(nt); XmlNamespaceManager _nsManager.AddNamespace("w", _w); XmlDocument _xmlDoc = new XmlDocument(nt); XmlDocument _xmlDoc.Load(part.GetStream());

  4. Insert text into a bookmark:

    public void InsertTextIntoBookmark(string bookmarkName, string text) { if ((text != String.Empty) && (text != null)) { //find it XmlElement bookmarkStartNode = (XmlElement)_xmlDoc. DocumentElement.SelectSingleNode("//w:bookmarkStart[@w:name='" + bookmarkName + "']", this._nsManager); XmlNodeList followingNodesList = bookmarkStartNode. SelectNodes(".//following::w:t | .//following::w:bookmarkEnd[@w:name='" + bookmarkName + "']", this._nsManager); foreach (XmlElement el in followingNodesList) { var res = from bm in this._wordDoc.MainDocumentPart.Document.Body.Descendants<DocumentFormat.OpenXml.Wordprocessing.BookmarkStart>() where bm.Name == bookmarkName select bm; var bk = res.SingleOrDefault(); if (bk != null) { DocumentFormat.OpenXml.Wordprocessing.Run bookmarkText = bk.NextSibling<DocumentFormat.OpenXml.Wordprocessing.Run>(); if (bookmarkText != null) { bookmarkText.GetFirstChild<DocumentFormat.OpenXml.Wordprocessing.Text>().Text = text; el.InnerText = text; } else { var parent = bk.Parent; // bookmark's parent element DocumentFormat.OpenXml.Wordprocessing.Run run = new DocumentFormat.OpenXml.Wordprocessing.Run( new DocumentFormat.OpenXml.Wordprocessing.RunProperties()); DocumentFormat.OpenXml.Wordprocessing.Text txt = new DocumentFormat.OpenXml.Wordprocessing.Text(text); run.Append(txt); // insert after bookmark parent parent.Append(run); } bk.Remove(); } } //flush to doc _xmlDoc.Save(_wordDoc.MainDocumentPart.GetStream(FileMode.Create)); } }

Hope this helps.

P.S. Someone please help me to format the code :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.