0

We have an automated job written in Python that uploads one file to a SharePoint location every day. The problem is that this job leaves the uploaded file in a "Checked Out" status. How can I ensure that the auto-uploaded document is Checked-In and available to end users?

I have discovered that if I disable the "Require Documents to be Checked Out before eding" option in the Library Settings, then the upload works as we need, but we need to leave this option turned on for future changes by other users.

Here is the current Python code that uploads the file:

  import requests
        from requests_ntlm import HttpNtlmAuth
        session = requests.Session()
        session.auth = HttpNtlmAuth('DOMAIN\\username','password', session)

      my_headers = {
                    'accept' : 'application/vnd.ms-excel;odata=verbose',
                    'content-type' : 'application/vnd.ms-excel;odata=verbose',
                    'odata' : 'verbose',
                    'X-RequestForceAuthentication' : 'true',
                    'Connection': 'close'
                }


        put_url = 'https://site.com/test/2020%20Reports'
        with open('/home/user/template.xlsx','rb') as fin:
            myFile = requests.put('{}/template.xls'.format(put_url), 
                                  data=fin.read(), 
                                  auth=session.auth, 
                                  headers=my_headers)

How can I leave "require checkout" turned on, but still ensure that the uploaded file is checked in and available to contributors?

4
  • How is the file being uploaded? Is a user manually logging into SharePoint and uploading the file each day, or are you using an automated script or job?
    – willman
    Feb 24 '20 at 18:36
  • We have some job which will upload the file to this folder automatically, but this is in checked out status. We want to automate this to check in automatically(only when the file is created).
    – mssr
    Feb 24 '20 at 19:01
  • What code/technology is the job using. The same remote API's that are used to upload a file can be used to Check-in the file after upload is complete, but to give you an example, would need to know how this job is coded: REST,CSOM,JSOM,PowerShell, etc.? If you post the code snippet that is currently uploading the file, we can show you how to modify that code to finish checking in the file.
    – willman
    Feb 24 '20 at 19:05
  • Thanks William. We are using python to upload the file. Tried different options, but not working.
    – mssr
    Feb 24 '20 at 19:09
0

Your code is using SharePoint's REST API to upload the file, so you would need to add a secondary call to the REST method that invokes a Check-In. The documentation for the File Check-in HTTP POST is here. Luckily, it is one of the more simple calls to make, as it requires no body data. To Check in the file, you need to POST to https://{site_url}/_api/web/GetFileByServerRelativeUrl('/Folder Name/{file_name}')/CheckIn(comment='Comment',checkintype=0)

You should be able to use the same session and headers you have already established for the file upload. I am not a Python developer, but I think you need add the following to the end of your code:

checkin_url = "https://site.com/test/_api/web/GetFileByServerRelativeUrl('/test/2020%20Reports/template.xls')/CheckIn(comment='Comment',checkintype=0)"
checkinResponse = requests.post(checkin_url, data='',auth=session.auth,headers=my_headers)

26
  • Thanks Willman. Do we need to use _api/web/GetFileByServerRelativeUrl ? Can i use the final sharepoint url.
    – mssr
    Feb 24 '20 at 22:46
  • getting 404 error.
    – mssr
    Feb 24 '20 at 22:58
  • There should be some redundant overlap in your URLs. The SiteUrl before the /_api/web is the full URL of the root of your site, but the Server-relative URL of your file still includes the folder portions of your path. So, in the example in my posted answer, notice that it assumes you have a site named Test on the server site.com, but the file URL still has to start with /test/.
    – willman
    Feb 24 '20 at 23:38
  • If you are allowed to post your actual URL, I can help troubleshoot.
    – willman
    Feb 24 '20 at 23:39
  • Thanks Willman. This url is almost similar to the below. "site.com/am/DIT/DDQ/Shared%20Documents/2018%20Docs%20Reports/…"
    – mssr
    Feb 25 '20 at 0:26

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.