How to auto populate Word template using Open XML SDK 2.0
By peter.stilgoe
Here is some simple code that updates a word document template that contains 2 content controls in the template document called:
MatterNumber
DocType
The template document is – C:\Users\stilgoep\WordTest\test-contentcontrol.docx
The output document is – C:\Users\stilgoep\WordTest\test-contentcontrol-updated.docx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using System.Xml;
using System.IO;
namespace BasicOpenXMLUpdater
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting up Word template updater ...");
//get path to template and instance output
string docTemplatePath = @"C:\Users\stilgoep\WordTest\test-contentcontrol.docx";
string docOutputPath = @"C:\Users\stilgoep\WordTest\test-contentcontrol-updated.docx";
//create copy of template so that we don't overwrite it
File.Copy(docTemplatePath, docOutputPath);
Console.WriteLine("Created copy of template ...");
//stand up object that reads the Word doc package
using (WordprocessingDocument doc = WordprocessingDocument.Open(docOutputPath, true))
{
//create XML string matching custom XML part
string newXml = "<root>" +
"<MatterNumber>00011111</MatterNumber>" +
"<DocType>Contract</DocType>" +
"</root>";
MainDocumentPart main = doc.MainDocumentPart;
main.DeleteParts<CustomXmlPart>(main.CustomXmlParts);
//add and write new XML part
CustomXmlPart customXml = main.AddCustomXmlPart(CustomXmlPartType.CustomXml);
using (StreamWriter ts = new StreamWriter(customXml.GetStream()))
{
ts.Write(newXml);
}
//closing WordprocessingDocument automatically saves the document
}
Console.WriteLine("Done");
Console.ReadLine();
}
}
}
You need to add the following 2 references:
DocumentFormat.OpenXml
WindowsBase.dll
You will also need to download the Word Content Control Toolkit from CodePlex to map your content controls to your XML data.
More From pstilgoe
Populating an Infopath dropdown using a specific Sharepoint list view
By peter.stilgoe
If you have a Sharepoint list containing multiple views, one view shows all approved items and another view shows all items that are pending approval. You have an Infopath form where you have a dropdown control that you want to be populated with only ‘Approved’ list items from your Sharepoint list.
If you create your Infopath data connection & copy the URL to this list view as your source you will notice it still returns ALL items as opposed to your filtered view listing only the approved items.
The way round this is to create you data connection using an XML source, use this link to find out how to view your list data as XML and get the source URL – http://www.petestilgoe.com/2009/11/converting-a-sharepoint-list-into-xml/
Now you need to expand on this & add the particular view to the url to make the data connection get the filtered data for you.
Currently your URL looks something like this:
/_vti_bin/owssvr.dll?Cmd=Display&List={GUID}&XMLDATA=TRUE&noredirect=true
1) Now in your Sharepoint list click ‘Settings’ –> ‘List Settings’
2) Scroll down to the bottom & click on the view you want to use as your datasource
3) Now your URL contains the {List GUID} as well as the {View GUID}
eg. /_vti_bin/owssvr.dll?Cmd=Display&List=%7BD66DCB51%2D80B0%2D43AB%2DB80B%2DCD7D23E63B09%7D&View=%7B6D0CEF69%2DF899%2D40D0%2D86B2%2D6E01CC7D9CA1%7D&XMLDATA=TRUE
4) The URL is encoded so you will need to replace “%7B” with “{” and “%7D” with “}” and “%2D” with “-” so now your url will look something like
/_vti_bin/owssvr.dll?Cmd=Display&List={D66DCB51-80B0-43AB-B80B-CD7D23E63B09}&View={6D0CEF69-F899-40D0-86B2-6E01CC7D9CA1}&XMLDATA=TRUE
5) Use this url as your XML data source in Infopath and your drop down data will be filtered as per your chosen view.
More From pstilgoe
How to display most viewed documents from a Document Library
By peter.stilgoe
Came accross an interesting solution on Linkedin to display most viewed Documents from a Document Library in a webpart. I havnt tried it myself but sounds like it would work:
1) Install LogParser on Web App Server (Sharepoint Index Server)
2) Setup a batch file with the LogParse query of your choice.
For example:
“C:\Program Files\Log Parser 2.2\logparser” -i:IISW3C -o:XML “SELECT TOP 10 REPLACE_CHR(cs-uri-stem,’+',’ ‘) AS sUrl, EXTRACT_FILENAME(cs-uri-stem) AS sName, EXTRACT_EXTENSION(cs-uri-stem) AS sExt, COUNT(*) AS [Total Hits] INTO MYFILE.XML FROM \\WFE1\C$\Windows\system32\LogFiles\W3SVC1\*.log, \\WFE2\C$\Windows\system32\LogFiles\W3SVC1\*.log WHERE TO_UPPERCASE(sUrl) Like ‘/SITES/SPECIALSITE%%’ AND TO_UPPERCASE( sExt ) IN (‘DOCX’;'PPTX’;'PNG’;'GIF’;'JPG’;'BMP’) AND TO_UPPERCASE(sUrl) Not Like ‘%%/_T/%%’ AND TO_UPPERCASE(sUrl) Not Like ‘%%/_W/%%’ GROUP BY sUrl,sName,sExt ORDER BY [Total Hits] DESC
3) This query code search only for some documents (docx, pptx, etc.) on the site ‘/sites/specialsite’ on the default web application and site collection W3SVC1. The logs are located on WFE1 and WFE2. The log parser result file is an XML file named MYFILE.XML.
4) Daily Schedule the execute of the batch and the upload of the xml file to a sharepoint document library
5) Create an XSLT file for presentation and upload to the sharepoint document library.
6) Finally show the XML with the XML WebPart with the xml and xslt files as the parameters.
More From pstilgoe
Converting a Sharepoint List into XML
By peter.stilgoe
This easiest way is to use the URL below & insert your list GUID
http://servername/site/_vti_bin/owssvr.dll?Cmd=Display&List=*YOURUNIQUELISTID*&XMLDATA=TRUE
To get the GUID of the list you need to go into the list & click ‘Settings’ -> ‘List Settings’. Now if you look at your URL you will see something like:
/_layouts/listedit.aspx?List=%7B7B476F98%2D1075%2D4A6D%2D8F63%2D4C754B3F41FC%7D
The URL is encoded so you will need to replace “%7B” with “{” and “%7D” with “}” and “%2D” with “-” so the end result would look something like: {7B476F98-1075-4A6D-8F63-4C754B3F41FC}



March 8th, 2011
