4

I've to move some items from one document library to an other one in a different site. I use object client model to do this My issue is having an exception while i'm trying to open the file and here is the exception: Cannot invoke HTTP DAV request. There is a pending query

Here is my code for copy document

public static void CopyDocuments(SP.ListItem doc, SP.Web srcWeb, SP.ClientContext srcContext,string srcLibrary,string destUrl, string destLibrary)
    {
        // set up the destination context
        SP.ClientContext destContext = new SP.ClientContext(destUrl);
        //destContext.Credentials = new NetworkCredential("SPUSER01", "SPUSER01");
        // get the new list
        SP.Web destWeb = destContext.Web;
        destContext.Load(destWeb);
        destContext.ExecuteQuery();

        //Récupère l'éventuel folder
        string folderDest = doc.FieldValues["FileRef"].ToString().Replace(srcWeb.ServerRelativeUrl + "/" + srcLibrary, "");
        folderDest = folderDest.Substring(0, folderDest.LastIndexOf('/'));

        try
        {
            if (doc.FileSystemObjectType == SP.FileSystemObjectType.File)
            {
                // get the file
                SP.File f = doc.File;
                srcContext.Load(f);
                srcContext.ExecuteQuery();

                // build new location url
                string nLocation = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + destLibrary.Replace(" ", "") + folderDest + f.Name;

                // read the file, copy the content to new file at new location
                SP.FileInformation fileInfo = SP.File.OpenBinaryDirect(srcContext, f.ServerRelativeUrl);//he is throw the exception
                SP.File.SaveBinaryDirect(destContext, nLocation, fileInfo.Stream, true);

                SP.File spfile = destWeb.GetFileByServerRelativeUrl(nLocation);
                ListItem newListItem = spfile.ListItemAllFields;
                destContext.Load(newListItem);
                destContext.ExecuteQuery();
                newListItem["Created"] = doc["Created"];
                newListItem["Created_x0020_By"] = doc["Created_x0020_By"];
                newListItem["Modified_x0020_By"] = doc["Modified_x0020_By"];
                newListItem["Modified"] = doc["Modified"];
                newListItem["Author"] = doc["Author"];
                newListItem["Editor"] = doc["Editor"];
                newListItem["Organisation_x0020_FR"] = doc["Organisation_F"];
                newListItem["Organisation_x0020_NL"] = doc["Organisation_Nl"];
                newListItem["Service_x0020_FR"] = doc["Service_x0020_Fr"];
                newListItem["Service_x0020_NL"] = doc["Service_x0020_Nl"];
                newListItem["Doc_x0020_Version"] = doc["Doc_x0020_Version"];
                newListItem["Doc_x0020_Language"] = doc["Doc_x0020_Language"];
                newListItem["Doc_x0020_Coordinates"] = doc["Doc_x0020_Coordinates"];
                newListItem["Doc_x0020_Title"] = doc["new_x0020_title"];
                newListItem["Doc_x0020_ID"] = doc["_dlc_DocId"];
                newListItem["Doc_x0020_Description"] = doc["Description0"];
                newListItem["Title"] = doc["Title"];
                newListItem["urlLS"] = Properties.Settings.Default.serverUrlLS + doc["FileRef"];
                newListItem.Update();
                // execute it
                List list = destContext.Web.Lists.GetByTitle(destLibrary);
                destContext.Load(destContext.Site);
                destContext.ExecuteQuery();

                destContext.Load(list);
                destContext.ExecuteQuery();
            }

            if (doc.FileSystemObjectType == SP.FileSystemObjectType.Folder)
            {
                // load the folder
                srcContext.Load(doc);
                srcContext.ExecuteQuery();

                // get the folder data, get the file collection in the folder
                SP.Folder folder = srcWeb.GetFolderByServerRelativeUrl(doc.FieldValues["FileRef"].ToString());
                SP.FileCollection fileCol = folder.Files;

                // load everyting so we can access it
                srcContext.Load(folder);
                srcContext.Load(fileCol);
                srcContext.ExecuteQuery();
                // create the folder at the new document library if it doesn't already exist
                AddNewFolder(destUrl, destLibrary, folderDest, destLibrary, "Folder", folder.Name, doc);
            }
        }
        catch (Exception ex)
        {
            logger.WriteError("File Error = " + ex.ToString());
        }
    }
2
  • Check if WebClient service running on the your client machine.
    – Aanchal
    Commented Oct 1, 2014 at 11:22
  • yes it's running and i've restarted for being sure. Commented Oct 1, 2014 at 13:29

1 Answer 1

12

The error:

Additional information: Cannot invoke HTTP DAV request. There is a pending query.

is encountered when ClientRuntimeContext.HasPendingRequest property returns true.

In case of File.OpenBinaryDirect method you could use the following verification:

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

Example

private static void DownloadFile(Web web, string fileUrl,string targetPath)
{
    var ctx = (ClientContext)web.Context;
    if (ctx.HasPendingRequest)
       ctx.ExecuteQuery();
    using(var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, fileUrl))
    {
        var fileName = Path.Combine(targetPath, System.IO.Path.GetFileName(fileUrl));
        using (var fileStream = System.IO.File.Create(fileName))
        {
            fileInfo.Stream.CopyTo(fileStream);
        }
    }
}
2
  • 1
    Your answer works, but I do wonder what query is being executed? It is a strange - but logical - solution to a strange problem.
    – arame3333
    Commented Mar 17, 2016 at 8:57
  • CSOM API stores all the queries in internal queue, HasPendingRequest basically tells that there are queries not submitted to server yet. As far as i could guess, OpenBinaryDirect operation requires query queue to be empty Commented Mar 17, 2016 at 9:38

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.