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());
}
}