0

I am using following code to copy a file from a Document Library to another. The file is copied and is there, but the fields will not be updated.

fileInfo = File.OpenBinaryDirect(srcClntCntxt, fileServerRelativeUrl);

using (ClientContext clntCntxt = ContextHelper.DestinationClientContex())
{
   Web web = clntCntxt.Web;
   clntCntxt.Load(web);
   clntCntxt.ExecuteQuery();

   string nLocation = web.ServerRelativeUrl.TrimEnd('/') + "/destLib/fileName";

    File.SaveBinaryDirect(clntCntxt, nLocation, fileInfo.Stream, true);
    File newFile = web.GetFileByServerRelativeUrl(nLocation);
    ListItem newFileItem = newFile.ListItemAllFields;
    clntCntxt.Load(newFile);
    clntCntxt.Load(newFileItem);
    clntCntxt.ExecuteQuery();

    newFile.CheckOut();
    newFileItem["MyComments"] = "test test"; //this is a multi line text 
    newFileItem.Update();
    newFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);
    clntCntxt.ExecuteQuery();
  • Nadeem, where exactly do I need to add it. When I add it before checkOut it casts this: You can only publish, unpublish documents in a minor version enabled list – Medes Mar 17 '15 at 13:11
  • No, It didn't work – Medes Mar 17 '15 at 13:18
3

Try this

clntCntxt.Load(newFile);
clntCntxt.ExecuteQuery();

//check out to make sure not to create multiple versions
newFile.CheckOut();

ListItem newFileItem = newFile.ListItemAllFields;
newFileItem["MyComments"] = "test";
newFileItem.Update();

// use OverwriteCheckIn type to make sure not to create multiple versions 
newFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);
| improve this answer | |
  • Amal, I can not see any difference between your code and mine – Medes Mar 17 '15 at 13:12
  • I reversed Update and CheckIn – Amal Hashim Mar 17 '15 at 13:12
  • I am sorry about my mistake, I have already tested it, Now I have updated the question. – Medes Mar 17 '15 at 13:15
  • Can you check "MyComments" is the correct internal name of the field? – Amal Hashim Mar 17 '15 at 13:18
  • Yes it is the Internal Name of the field – Medes Mar 17 '15 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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