0

I am getting "System.InvalidOperationException: 'This property cannot be set after writing has started.'" when calling OpenBinaryDirect().

Please check below code and suggest.

public static void CopyDocuments()
{
    var securePassword = new SecureString();
    //Convert string to secure string  
    foreach (char c in "")
        securePassword.AppendChar(c);
    securePassword.MakeReadOnly();            

    // set up the src client
    ClientContext srcContext = new ClientContext("https://");
    srcContext.Credentials = new SharePointOnlineCredentials("", securePassword);

    // set up the destination context
    ClientContext destContext = new ClientContext("https://");
    destContext.Credentials = new SharePointOnlineCredentials("", securePassword);

    // get the source list and items
    Web srcWeb = srcContext.Web;

    List srcList = srcWeb.Lists.GetByTitle("Account");
    CamlQuery camlQuery = new CamlQuery();
    camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>CheckCopyFromSharepoint.txt</Value></Contains></Eq></Query></View>";
    ListItemCollection itemColl = srcList.GetItems(camlQuery);
    srcContext.Load(itemColl);
    srcContext.ExecuteQuery();
    // get the destination list
    Web destWeb = destContext.Web;
    destContext.Load(destWeb);
    destContext.ExecuteQuery();
    foreach (var doc in itemColl)
    {
        // get the file
        Microsoft.SharePoint.Client.File file = doc.File;
        srcContext.Load(file);
        srcContext.ExecuteQuery();
        // build destination url
        string nLocation = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + "Deal".Replace(" ", "") + "/" + file.Name;
        // read the file, copy the content to new file at new location
        FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(srcContext, file.ServerRelativeUrl);  //Error at this line
        // Error
        //System.InvalidOperationException: 'This property cannot be set after writing has started.'
        Microsoft.SharePoint.Client.File.SaveBinaryDirect(destContext, nLocation, fileInfo.Stream, true);
    }
    Console.WriteLine("success...");
}
5
  • Does this happen at the very first time OpenBinaryDirect is called? If not perhaps it's an issue of your dev environment like in stackoverflow.com/questions/46782742/…. If the first call is proper and it executes at the second one it might be a question of reusing destContext. And than the SaveBinaryDirect to the destContext is the problem. So one test could be to load the Context for each file to write.
    – Celophysis
    Jun 7, 2018 at 8:22
  • this is happening at very first call of OpenBinaryDirect.
    – Ajit004
    Jun 7, 2018 at 8:24
  • just to exclude that reusing srcContext is the problem: what about if you use a using bracket and create a separate client context for the download like in dailydotnettips.com/… or codeproject.com/Articles/103503/… ?
    – Celophysis
    Jun 7, 2018 at 9:02
  • @Celophysis tried that . . same issue again
    – Ajit004
    Jun 7, 2018 at 9:30
  • the last thing I can suggest is to use OpenBinaryStream instead of OpenBinaryDirect like in tech-karma.blogspot.com/2013/05/…
    – Celophysis
    Jun 7, 2018 at 9:55

0

Your Answer

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