3

We want to share documents in a SharePoint Document Library (SP 2013) which are automatically generated each week by another software system.

These documents are in PDF and RTF format (maybe also html).

I would like to upload the documents once first "by hand" and then let the other software system overwrite the documents and check-in a new version.

How can this be done programmatically (the easiest possible way)?

7
  • 1
    IMO you could stick with the OS built-in Task Scheduler to do the file saving (copying?) to the library's path. Whether this is done using batches, VBS, VBA or something else is up to the source platform & your personal favor. Does this sound sufficient for your requirement?
    – moe
    Commented Jan 13, 2016 at 11:07
  • sounds good - where can I find examples how to do that? IMHO it is not sufficient to copy the files to the correct library, but also to publish them as a new x.0 version which is visible to the users.
    – mBBe
    Commented Jan 13, 2016 at 12:18
  • 1
    1. Create a batch to copy files (stackoverflow.com/questions/986447/…) Note: requires sufficient user permissions in both source and destination & 2. Create a basic batch file task on Task Scheduler (stackoverflow.com/questions/4437701/…) Note: you need to set a weekly trigger & force replace existing file if it's using the same name. Do you really need to use a publishing process/minor versions? What's the end user purpose? That might require some further setup.
    – moe
    Commented Jan 13, 2016 at 13:05
  • Thanks for your explications! I want to keep old version to document the changes of the documents. We won't need minor versions in this case, but they are enabled in general in the document library.
    – mBBe
    Commented Jan 13, 2016 at 13:14
  • 1
    To have a version history you won't need a publishing process/minor versions - only Create major versions setting is required. The document's would automatically have versions as 1.0, 2.0 and so on. This means the files would be in a published state once they are copied to the destination.
    – moe
    Commented Jan 13, 2016 at 13:40

1 Answer 1

2

I would either built a small console app using CSOM or use PowerShell and CSOM together.

I haven't checked if this code works, but it probably look something like this:

using (var context = TokenHelper.GetClientContextWithAccessToken(uri.ToString(), token))
            {
                var web = context.Web;
                using (var fs = new FileStream(sourceFilePath, FileMode.Open))
                {
                    FileInfo fi = new FileInfo(sourceFilePath);

                    FileCreationInformation flciNewFile = new FileCreationInformation();

                    flciNewFile.ContentStream = fs;
                    flciNewFile.Url = System.IO.Path.GetFileName(sourceFilePath);
                    flciNewFile.Overwrite = overWriteIfExists;

                    List docs = web.Lists.GetByTitle(listName);
                    Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(flciNewFile);

                    context.Load(uploadFile);
                    context.ExecuteQuery();

                    fs.Close();

                    //publish the file
                    uploadFile.Publish("comment");
                    context.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.