1

I have some code that runs fine on a SP2013 server that opens a SharePoint file and processess it as a memory stream. I need to have the same working for a 2010 server.

Using the same code and the most recent client and runtime DLLs I am able to connect to the server and the library but I get an error "Method "OpenBinaryStream" does not exist" When the following line executes:

ClientResult<Stream> fileContents = file.OpenBinaryStream();

I have checked its supported in 2010. It shows up in intellisense and doesn't have a problem compiling.

Here is the full code:

class Program
{
    static void Main(string[] args)
    {
        string serverURL = "http://SP2010";
        string libraryName = "Documents";
        ClientContext context = new ClientContext(serverURL);

        List myList = context.Web.Lists.GetByTitle(libraryName);
        CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
        ListItemCollection items = myList.GetItems(query);

        // Retrieve all items in the ListItemCollection from List.GetItems(Query). 
        context.Load(items);
        context.ExecuteQuery();
        foreach (ListItem listItem in items)
        {
            string fileText = string.Empty;

            Microsoft.SharePoint.Client.File file = listItem.File;

            context.Load(file);
            context.ExecuteQuery();

            string fileUrl = file.ServerRelativeUrl;
            Console.WriteLine(fileUrl);


                ClientResult<Stream> fileContents = file.OpenBinaryStream();
                context.ExecuteQuery();
                MemoryStream fileContentStream = new System.IO.MemoryStream();
                fileContents.Value.CopyTo(fileContentStream);
                fileText += extractor.TextFromTextFile(fileContentStream);

Any ideas very much appreciated.

Thanks Bren

2

It does not appear to be supported in Microsoft.SharePoint.Client.dll in SharePoint 2010. The link you provided to MSDN was for Microsoft.SharePoint.dll.

  • 1
    Sergey reread the original post, they're using the client namespace, which lacks that method. You posted the SSOM method. – Trevor Seward May 18 '17 at 14:14
  • I apologize for inconvenience, have posted the wrong link. Removed my old to not to mislead. OpenBinaryDirect method is correct. – Sergey May 20 '17 at 8:33
2

Thanks for the pointers, here is the working code which uses the OpenBinaryDirect and pushes it into a memory stream.

FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, fileUrl);

                using (var ms = new MemoryStream())
                {
                    var buf = new byte[1024 * 16];
                    int byteSize;
                    while ((byteSize = fileInfo.Stream.Read(buf, 0, buf.Length)) > 0)
                    {
                        ms.Write(buf, 0, byteSize);
                    }

                    fileText += extractor.TextFromDOCX(ms);
                    Console.WriteLine(fileText);
  • Please make sure to mark this as an answer when you're able! – Trevor Seward Jun 2 '17 at 14:59
0

for anyone else scratching there head ... this can be done using the client library but OpenBinaryDirect is static I was trying to call it on the instance so I couldn't find it

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.