user11956646
user11956646

Reputation:

How to check in uploaded files to Sharepoint using office365 API?

So I'm able to upload a file in SharePoint using office365 package, however in SharePoint it is not checked in and I had to manually do it. Is there any way function in that package to do check in?

here's my code:

def csv_to_sp(spfile_path,output):
    ctx = Extract.connect()
    target_folder =  ctx.web.get_folder_by_server_relative_url(spfile_path)
    with open(output,'rb') as content_file:
        file_content = content_file.read()

    target_folder.upload_file(output,file_content).execute_query()
    response = 'Uploaded to sharepoint!'
    return response

Upvotes: 0

Views: 377

Answers (2)

Mxblsdl
Mxblsdl

Reputation: 514

I just got this to work for me. You need to do a second call to the object you just uploaded and call the checkin method on it. See the three lines I added to your function. You will need to extract or supply the file name.

def csv_to_sp(spfile_path,output):
    ctx = Extract.connect()
    target_folder =  ctx.web.get_folder_by_server_relative_url(spfile_path)
    with open(output,'rb') as content_file:
        file_content = content_file.read()

    target_folder.upload_file(output,file_content).execute_query()
    response = 'Uploaded to sharepoint!'
    
    object_path = ctx.web.get_file_by_server_relative_url(spfile_path + <YOUR_FILE_NAME>)
    ctx.load(object_path).execute_query()
    object_path.checkin(comment="upload", checkin_type=2).execute_query()
    
    return response

There are three checkin_types for minor, major, and overwrite. https://learn.microsoft.com/en-us/previous-versions/office/sharepoint-csom/ee542953(v=office.15)

Upvotes: 0

Cornelius J. van Dyk
Cornelius J. van Dyk

Reputation: 728

It sounds like the target library has one of the following going on which would cause the uploaded document to be checked out after upload:

  1. Require Checkout is enabled. When this setting is enabled, new documents you upload are checked out to you by default. You can toggle this option from the Library Settings/Versioning Settings page as described here.
  2. There are required fields without default values. When additional metadata fields are added to the library and are set to be required, there is an option to specify a default value. If no default value is specified, any new items uploaded to said library, will be checked out and visible only to the user who uploaded the document. These items will not become visible to anyone else (including admins) until the user does a manual check in, at which time the user would be forced to provide a value for the required field.

Since you did not reference having to specify values for fields, I suspect you probably have #1 going on in your library. Try turning off the "Require Check Out" setting and retest.

Upvotes: 0

Related Questions