次の方法で共有


Using the SharePoint 2010 Client OM with Open XML - Retrieving a Document

This is a clipboard friendly version of example #1, Retrieving a Document, 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;
// The following using directive is used to resolve the ambiguity
// between the System.IO.File and Microsoft.SharePoint.Client.File
// classes.
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>
<Query>
<Where>
<Eq>
<FieldRef Name='FileLeafRef'/>
<Value Type='Text'>Test.docx</Value>
</Eq>
</Where>
<RowLimit>1</RowLimit>
</Query>
</View>";
ListItemCollection listItems = sharedDocumentsList.GetItems(camlQuery);
clientContext.Load(sharedDocumentsList);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
if (listItems.Count == 1)
{
ClientOM.ListItem item = listItems[0];
Console.WriteLine("FileLeafRef: {0}", item["FileLeafRef"]);
Console.WriteLine("FileDirRef: {0}", item["FileDirRef"]);
Console.WriteLine("FileRef: {0}", item["FileRef"]);
Console.WriteLine("File Type: {0}", item["File_x0020_Type"]);
FileInformation fileInformation =
ClientOM.File.OpenBinaryDirect(clientContext, (string)item["FileRef"]);
using (MemoryStream memoryStream = new MemoryStream())
{
CopyStream(fileInformation.Stream, memoryStream);
using (WordprocessingDocument doc =
WordprocessingDocument.Open(memoryStream, false))
{
var firstParagraph = doc
.MainDocumentPart
.Document
.Body
.Elements<Paragraph>()
.FirstOrDefault();
if (firstParagraph != null)
{
string text = firstParagraph.InnerText;
Console.WriteLine(text);
}
else
Console.WriteLine("Document doesn't contain any paragraphs.");
}
}
}
else
Console.WriteLine("Document not found.");
}
}