Files are not checked in after upload a file to sharepoint using requests/requests_ntlm

Hi ,
I tried to upload file(excel file) to sharepoint using requests/requests_ntlm package.
Files are uploaded to sharepoint, but not checked in state.

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

put_url = 'https://secure.sp.kp.org/teams/scpmgcc/complete%20care/Proactive%20Care/POE_CCTE_TAT/2017%20Reports'
with open('p:/ccte/_ccte_tat_template.xls','rb') as fin:
    myFile = requests.put('{}/_ccte_tat_template.xls'.format(put_url), 
                          data=fin.read(), 
                          auth=session.auth, 
                          headers={'content-type':'application/vnd.ms-excel'})

Even i tried with the header information with the follwoing

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

In both the options files are in checked out stage.
Please let me know how to make these files checked in.
1 Like

I have had success with the following approach. Substitute your info for the <> sections… Hope this helps in some way.

headers = {‘Content-Type’: ‘application/json; odata=verbose’, ‘accept’: ‘application/json;odata=verbose’}
r = requests.post( “https://.com/sites//_api/contextinfo”, auth=auth_data,
headers = headers )
formDigestValue = r.json()[‘d’][‘GetContextWebInformation’][‘FormDigestValue’]
headers = {‘Content-Type’: ‘application/json; odata=verbose’, ‘accept’: ‘application/json;odata=verbose’, ‘x-requestdigest’ : formDigestValue}
bytes_to_write = bytearray(file.read())
put_response = requests.put( url="https://.com/sites//YourFile.xlsx’,
data=bytes_to_write,
auth=auth_data,
headers=headers
)
print(put_response.status_code)

if put_response.status_code == 201:
checkin_url = “https://.com/sites//_api/Web/GetFileByServerRelativeUrl(’/sites//YourFile.xlsx’)/CheckIn(comment=‘Updated%20by%20USER:MSSR’,checkintype=0)”
checkinResponse = requests.post( checkin_url,
auth=auth_data,
data=’’,
headers=headers
)
print( "The Check-In response is: " + str( checkinResponse.status_code ) + " Reason: " + str(checkinResponse.reason ) )
else:
print(“What happened?”)