Freigeben über


Using the SharePoint 2010 Client OM with Open XML – Processing all Documents in a Document Library

This is a clipboard friendly version of example #5, Processing all Documents in a Document Library, from Using the SharePoint 2010 Managed Client Object Model with Open XML.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOC

using System;
using System.IO;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using Microsoft.SharePoint.Client;
using ClientOM = Microsoft.SharePoint.Client;

class Program
{
static private void CopyStream(Stream source, Stream destination)
{
byte[] buffer = new byte[32768];
int bytesRead;
do
{
bytesRead = source.Read(buffer, 0, buffer.Length);
destination.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
}

static void Main(string[] args)
{
ClientContext clientContext =
new ClientContext("https://intranet.contoso.com");
List sharedDocumentsList = clientContext.Web.Lists
.GetByTitle("Shared Documents");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<View Scope='Recursive'>
<Query>
<Where>
<Eq>
<FieldRef Name='File_x0020_Type'/>
<Value Type='Text'>docx</Value>
</Eq>
</Where>
</Query>
</View>";
ListItemCollection listItems =
sharedDocumentsList.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
foreach (var item in listItems)
{
Console.WriteLine("FileLeafRef: {0}", item["FileLeafRef"]);
Console.WriteLine("FileDirRef: {0}", item["FileDirRef"]);
Console.WriteLine("FileRef: {0}", item["FileRef"]);
FileInformation fileInformation =
ClientOM.File.OpenBinaryDirect(clientContext,
(string)item["FileRef"]);
using (MemoryStream memoryStream = new MemoryStream())
{
CopyStream(fileInformation.Stream, memoryStream);
using (WordprocessingDocument doc =
WordprocessingDocument.Open(memoryStream, true))
{
var firstParagraph = doc.MainDocumentPart.Document
.Body.Elements<Paragraph>().FirstOrDefault();
if (firstParagraph != null)
{
string text = firstParagraph.InnerText;
Console.WriteLine(" First paragraph text: {0}",
text);
}
else
Console.WriteLine(
"Document doesn't contain any paragraphs.");
}
}
Console.WriteLine();
}
}
}