Sharepoint 2010 client object model with camlQuery - file download but no content / 0 byte

I'm trying to download a txt file from a subfolder within a folder in a document library.

I'm using camlQuery to achieve this. Unfortunately, i get no content of the txt file. It has 0 byte.

public void SaveFolderFiles(string fileName, string libraryName, ClientOM.ClientContext clientContext)
    {
        ClientOM.List sharedDocumentsList = clientContext.Web.Lists.GetByTitle(libraryName);
        ClientOM.CamlQuery camlQuery = new ClientOM.CamlQuery();
        camlQuery.FolderServerRelativeUrl = "/Site/Folder/Folder2010/";
        camlQuery.ViewXml =
            @"<View>
            <Query>
              <Where>
                <Eq>
                  <FieldRef Name='FileLeafRef'/>
                  <Value Type='Text'>" + fileName + @"</Value>
                </Eq>
              </Where>
              <RowLimit>1</RowLimit>
            </Query>
          </View>";
        ClientOM.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"]);
            ClientOM.FileInformation fileInformation = ClientOM.File.OpenBinaryDirect(clientContext, (string)item["FileRef"]);
            using (MemoryStream memoryStream = new MemoryStream())
            {
                fileInformation.Stream.CopyTo(memoryStream);
                using (FileStream fileStream = File.Create(@"D:\" + item["FileLeafRef"].ToString()))
                {
                    memoryStream.CopyTo(fileStream);
                }
                memoryStream.Flush();
            }


        }
        else
        {
            Console.WriteLine("Document not found.");
        }
    }

Maybe someone has an idea?

Regards

Answers


Try this:

using FileInformation and get the MemoryStream

string fileurl = (string)liitem["FileRef"];
FileInformation ffl = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileurl);
byte[] bytesarr = ReadFully(ffl.Stream);
MemoryStream mnm = new MemoryStream(bytesarr);

ReadFully function which converts Stream to Bytes array

 public byte[] ReadFully(Stream input)
    {
        byte[] buffer = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }

Need Your Help

Search and replace in current file with Sublime Text plugin

python sublimetext2 sublimetext

I am trying to port a Sublime Text build system to a plugin.

Solve Picture Streching

php jquery css3

I am making a project in which i am using a cover picture like Google+.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.