0

I'm using C# CSOM to add documents to a Sharepoint library. I'm using the code below which works. How do I modify this code so that if the Document already exists in Sharepoint it will CheckOut the Document, Update it and CheckIn the new version of the Document? Or am I misunderstanding how this works? Thanks.

        using (ClientContext ctx = new ClientContext(siteUrl))
        {
            using (System.IO.FileStream fs = new System.IO.FileStream(sourceFilePath, System.IO.FileMode.Open, FileAccess.Read))
            {
                FileCreationInformation fcInfo = new FileCreationInformation();
                fcInfo.ContentStream = fs;
                fcInfo.Url = file_name_only;
                fcInfo.Overwrite = true;

                Folder targetFolder = ctx.Web.GetFolderByServerRelativeUrl(libraryName + "/" + folderPath);
                Microsoft.SharePoint.Client.File myFile = targetFolder.Files.Add(fcInfo);

                myFile.CheckIn("Initial CheckIn", CheckinType.MajorCheckIn);

                ctx.Load(myFile);
                ctx.ExecuteQuery();
            } 
        }

1 Answer 1

0

It turns out that it was much easier than I thought. The only complexity was correctly constructing the relativeURL. Oddly, for GetFolderByServerRelativeUrl it was sufficient to have the relative URL as the Library name onwards, but for GetFileByServerRelativeUrl I seemed to need the full path including the domain name (i.e. a full path rather than a relative path).

        using (ClientContext ctx = new ClientContext(siteUrl))
        {
            string relativeURL = SharepointSite + libraryName + "/" + folderPath + "/" + file_name_only;
            bool alreadyInSharePoint = false; ;

            try
            {
                Microsoft.SharePoint.Client.File file = ctx.Web.GetFileByServerRelativeUrl(relativeURL);
                file.CheckOut();
                ctx.Load(file);
                ctx.ExecuteQuery();
                alreadyInSharePoint = true;
            }
            catch (Microsoft.SharePoint.Client.ServerException ex)
            {
                switch (ex.ServerErrorTypeName)
                {
                    case "System.IO.DirectoryNotFoundException":
                    case "System.IO.FileNotFoundException":
                        break;
                    default:
                        throw ex;
                }
            }

            using (System.IO.FileStream fs = new System.IO.FileStream(sourceFilePath, System.IO.FileMode.Open, FileAccess.Read))
            {
                FileCreationInformation fcInfo = new FileCreationInformation();

                fcInfo.ContentStream = fs;
                fcInfo.Url = file_name_only;
                fcInfo.Overwrite = true;

                Folder targetFolder = ctx.Web.GetFolderByServerRelativeUrl(libraryName + "/" + folderPath);
                Microsoft.SharePoint.Client.File myFile = targetFolder.Files.Add(fcInfo);


                myFile.CheckIn("Initial CheckIn", CheckinType.MajorCheckIn);

                ctx.Load(myFile);
                ctx.ExecuteQuery();
            }
        }
    }

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.