Using the SharePoint 2010 Client OM with Open XML - Creating a Document
This is a clipboard friendly version of example #4, Creating a Document, from Using the SharePoint 2010 Managed Client Object Model with Open XML.
This blog is inactive.
New blog: EricWhite.com/blog
using System;
using System.IO;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using Microsoft.SharePoint.Client;
using ClientOM = Microsoft.SharePoint.Client;
class Program
{
static void Main(string[] args)
{
ClientContext clientContext =
new ClientContext("https://intranet.contoso.com");
byte[] byteArray = System.IO.File.ReadAllBytes("Template.docx");
using (MemoryStream memoryStream = new MemoryStream())
{
memoryStream.Write(byteArray, 0, (int)byteArray.Length);
using (WordprocessingDocument doc =
WordprocessingDocument.Open(memoryStream, true))
{
// Insert a new paragraph at the beginning of the
//document.
doc.MainDocumentPart.Document.Body.InsertAt(
new Paragraph(
new Run(
new Text("Newly inserted paragraph."))), 0);
}
string fileUrl = "/Shared Documents/NewDocumentFromTemplate.docx";
memoryStream.Seek(0, SeekOrigin.Begin);
ClientOM.File.SaveBinaryDirect(clientContext, fileUrl, memoryStream, true);
}
}
}