3

I use following code to add file to SharePoint library using client object model.

   Web web = context.Web;
                FileCreationInformation newFile = new FileCreationInformation();
                newFile.Content = System.IO.File.ReadAllBytes(filePath);
                newFile.Url = destFileName;
                newFile.Overwrite = true;
                List docs = web.Lists.GetByTitle(listName);
                Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
                context.Load(uploadFile);
                context.ExecuteQuery();

Question is, can I include my comment during this routine itself?

As it stands, I have to checkout file again and check in with comments using following code, which makes 2 versions of file which is unnecessary.

if (uploadFile.CheckOutType != CheckOutType.None)
                    { 
                        uploadFile.CheckOut();
                        context.ExecuteQuery();
                    }

                    uploadFile.CheckIn(checkinComments, checkinType);
                    context.ExecuteQuery();

1 Answer 1

2

It is possible to check-in file with comment in one round trip only. Here is the updated code --

Web web = context.Web;
                FileCreationInformation newFile = new FileCreationInformation();
                newFile.Content = System.IO.File.ReadAllBytes(filePath);
                newFile.Url = destFileName;
                newFile.Overwrite = true;
                List docs = web.Lists.GetByTitle(listName);
                Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
                uploadFile.CheckIn("checkinComments", CheckinType.MajorCheckIn);
                context.ExecuteQuery();

Also, You do not require 'uploadFile' to load again. All pending changes will be commited by 'context.ExecuteQuery()'.

2
  • this one does not work , as on final executeQuery, error is thrown file "filename" is not checked out. Commented Aug 21, 2014 at 7:11
  • 1
    Looks like Document Library does not have "Require Check Out" enabled. So, in this case, check if the document library has "Require Check Out" enabled before calling check-in method. If it is enabled then the file being uploaded will be checked out and it has to be check-in. Commented Aug 25, 2014 at 11:15

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.