Skip to content

Impersonate Sharepoint File Checkout #919

Closed

Description

@jlaiph

Is it possible to impersonate as site user to checkout file on behalf of site user.

Activity

  1. office365-maintainer commented on Oct 3, 2026

    @office365-maintainer

    Checkout and check-in are supported through the SharePoint REST v1 client:

    from office365.sharepoint.files.checkin_type import CheckinType
    
    file = ctx.web.get_file_by_server_relative_path("Shared Documents/draft.docx")
    file.checkout().execute_query()
    file.checkin("Updated via API", CheckinType.MajorCheckIn).execute_query()
    # or, to discard the check-out:
    # file.undocheckout().execute_query()

    A runnable end-to-end example is in examples/sharepoint/files/checkout_checkin.py.

    On impersonation: SharePoint REST has no "run as user" switch — a request executes as whoever the access token represents, so a check-out is always attributed to the authenticated identity. To check out as a given site user you must authenticate as that user with a delegated flow, then call file.checkout():

    • Delegated: with_username_and_password(...), with_interactive(...), with_device_flow(...), or with_access_token(...) if you obtain an on-behalf-of token in a middle-tier service.
    • App-only: with_client_certificate(...) runs as the application, so it cannot be used to attribute a check-out to another user. (with_client_secret(...) is also not accepted by SharePoint /_api.)

    If the goal is to clear a file that someone else left checked out, list the library's checked-out files and take over the check-out — this requires sufficient permissions (e.g. site collection admin):

    for item in ctx.web.default_document_library().get_checked_out_files().execute_query():
        item.takeover_checkout().execute_query()

    Closing this as answered — please reopen if you had a different scenario in mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requestedsharepointSharePoint REST v1 API, ClientContext

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions