0

I have the following code using CSOM to download a file from a sharepoint site:

    public IEnumerable<FileUpload> DownloadFiles(string client, Guid userGuid, IEnumerable<Guid> fileUploadGuids)
    {
        using (var context = new ClientContext(documentStore))
        {
            client = client.ToLower();

            var result = IntialSetUp(client, userGuid, fileUploadGuids, context);

            context.Load(result, items => items.Include(
                item => item.File.Name,
                item => item[FileTitle],
                item => item[FileRef]
            ));

            context.ExecuteQuery();

            List<FileUpload> fileUploads = new List<FileUpload>();

            foreach (var item in result)
            {
                using (var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, item[FileRef].ToString()))
                {
                    using (var memory = new MemoryStream())
                    {
                        fileInfo.Stream.CopyTo(memory);

                        fileUploads.Add(new FileUpload()
                        {
                            Name = item[FileTitle].ToString(),
                            FileUploadGuid = new Guid(item.File.Name),
                            FileBytes = memory.ToArray()
                        });
                    }
                }
            }

            return fileUploads;
        }
    }

    public  void AddNetworkCredentials(ClientContext context)
    {
        string login = ConfigurationManager.AppSettings["UserName"];
        string password = ConfigurationManager.AppSettings["Password"];

        context.Credentials = new NetworkCredential(login, password, ConfigurationManager.AppSettings["Domain"]);

        context.ExecuteQuery();
    }

    public ListItemCollection IntialSetUp(string client, Guid userGuid, IEnumerable<Guid> fileUploadGuids, ClientContext context)
    {
        AddNetworkCredentials(context);

        var list = context.Web.Lists.GetByTitle(client);

        context.ExecuteQuery();

        return GetItems(FileLeafRef, Array.ConvertAll(fileUploadGuids.ToArray(), x => x.ToString("N")), list, FieldType.File, documentStore + "/" + client + "/" + userGuid.ToString("N"));
    }

This code will run fine the first time round, but every consecutive request will throw the following error:

The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.

This only happens for this service request and doesn't effect other requests that do not download the actual file, this leads me to believe it has something to do with the OpenBinaryDirect method but I haven't found anything describing this issue.

Any ideas as to what might be causing this and how it could be remedied?

notes:

  • Left in a bunch of ExecuteQuerys for testing purposes.
  • The title of the file is actually a guid so we could have multiple files with the same name (the file name is a separate column)

1 Answer 1

1

This post has the same problem, which was solved by adding an if statement to check of the context has any pending requests:

       if (ctx.HasPendingRequest)
           ctx.ExecuteQuery();

right before your using statement for the file OpenBinaryDirect command

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.