Package: sharepoint-tool
Sharepoint API ClientThis is a quick tool for interfacing with Sharepoint APIs without user
credentials. It includes a library to be used as well as a basic toolNOTE:this is only known to work with MS/Cloud hosted instance, i.e.
instances that have urls in form ofhttps://<tenant>.sharepoint.com/.
It may work with self-hosted sharepoint, but currently the domain is
hardcoded into the libChangelog1.0.1 - Fix import, not code changes1.0.0 - Official initial release0.9.4 -removed0.9.3 -removedInstallationQuickest way to install is via PyPipip3 install sharepoint-toolKnown IssuesWhen creating folders usingadd_folder_path()orupload_file(), the
library will attempt to create all parent folders, and it may not have
permissions at certain level - causing403errors in the logs like these:sp_tool.sharepoint.connector:E:API ERROR: 403: '{"error":{"code":"-2147024891, System.UnauthorizedAccessException","message":{"lang":"en-US","value":"Access denied."}}}'These are usually safe to ignore, assuming folder already exists. In the future
we will handle this better.Pre-RequisitesThis library leverages "AppOnly" Add-In OAuth authentication. Before you
can use this API, you will need to register your "app" and get a client_id
and a security token for authentication.To do this you need to be admin on the space you are going to use.TIP: You can generate these urls by running the app withregistercommand like so:sp-tool --tenant mycompany --site MyTeam registerStep 1 - Register your "App"Go to URI_layouts/15/appregnew.aspxrelative to your site - so if your
tenant id ismycompanyand your site isMyTeamit will be:https://mycompany.sharepoint.com/sites/MyTeam/_layouts/15/appregnew.aspxFill in:Client Id: Just use Generate button to generate a good id. Save this
value as the client id parameter for the tool.Client Secret: Also use generate. Save this value as your API key(secret)
for the toolTitle: name of the toolApp Domain: Not sure this matters - use hostname from your company url
or somethingRedirect URI: Not sure this matters - use your company url or somethingStep 2 - Grant PermissionsNow, go to uri_layouts/15/appinv.aspx. Going with example above it will be:https://mycompany.sharepoint.com/sites/MyTeam/_layouts/15/appinv.aspxOnce there, enter the client id from step 1 and do a lookup
If all went well, it will populate all the fields except permissions
from values you set in the app.ForPermission Request XMLenter something like (specific permissions
depend on your needs):<AppPermissionRequests AllowAppOnlyPolicy="true">  
    <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" 
    Right="Write" />
</AppPermissionRequests>See these links to better understand permissions:https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/add-in-permissions-in-sharepointhttps://medium.com/ng-sp/sharepoint-add-in-permission-xml-cheat-sheet-64b87d8d7600Step 3 - Accept Trust ConfirmationUsing as binaryThis library comes with a simple implementation as a tool to publish a
directory of files to a SharePoint.Current help screen:Usage: sp_tool [OPTIONS] ACTION

  Microsoft-hosted Sharepoint API interface tool

Options:
  --version                   Show the version and exit.
  --dry-run / --no-dry-run    If set, do not actually upload
  --debug                     Debug mode
  --site TEXT                 Number of greetings.
  --tenant TEXT               Sharepoint Tenant id
  --recurse / --no-recurse    If true, recurse into subdirectories
  --checkout / --no-checkout  If true, recurse into subdirectories
  --base-path TEXT            Base for uploading to sharepoint
  --path TEXT                 Path relative to base for uploading to
                              sharepoint
  --client_id TEXT            Sharepoint Client ID
  --secret TEXT               Sharepoint Secret Token
  -h                          Show this message and exit.
  --help                      Show this message and exit.

Commands:
  config   Show effective config and exit
  publish  Publish commandEvery parameter above can be set via env variables as such:AuthenticationSP_TOOL_TENANT- tenant in MS system (the hostname)SP_TOOL_SITE- Site if uploading to a sub-site (leave blank to
upload to main site)SP_TOOL_SECRET- secret token after registrationSP_TOOL_CLIENT_ID- client id after registrationConnectionSP_TOOL_DRY_RUN- (true/false) set to "true" or "false"SP_TOOL_RECURSE- (true/false) Recursively upload files in source dirSP_TOOL_BASE_PATH- Base Path for all files. Usually this is just ""SP_TOOL_PATH- Base Path for all filesSP_TOOL_CHECKOUT- (true/false) if set, files are checked out
after upload to reduce changesSP_TOOL_EXCLUDE- one or more names or globs for files to exclude
If blank, no files are excluded. If more than one, use path
separator to separate (;on windows,:everywhere else).
Example, to exclude tmp and backup files on unix, use*.tmp:*.bakSP_TOOL_EXCLUDE_DIR- one or more names or globs for dir to
exclude. If blank, no directories are excluded. if more than one,
use path separator to separate (;on windows,:everywhere
else). Example, to exclude tmp and backup files on unix, use*.tmp:*.bakSP_TOOL_INCLUDE- one or more names or globs for files to include
If blank, all files are included. If more than one, use path
separator to separate (;on windows,:everywhere else).
Example, to include all PDF and MD files on unix,
use*.pdf:*.md:*.MDSP_TOOL_INCLUDE_DIR- one or more names or globs for dir to
include. If blank, all directories are included. If more than one,
use path separator to separate (;on windows,:everywhere
else). Example, to exclude tmp and backup files on unix, use*.tmp:*.bakUsing as libraryTo use this as a library in your code all you need to do is to
import and create SharepointConnection object:from sp_tool.sharepoint import SharepointConnector

# Create Client
sharepoint = SharepointConnector(
    tenant=TENANT,
    client_id=CLIENT_ID,
    client_secret=SECRET,
    site_name=SITE
)Available APIsHere is a quick dump of available commands as of right now:api_call(action, url, headers=None, **kwargs)low level api callfolder_contents(folder=DEFAULT_FOLDER)-  ???connected- return true if connection is verified (executes a test call)list_folders(folder=DEFAULT_FOLDER)-  list folder namesfolders(folder=DEFAULT_FOLDER)-  list folder objects in the specifiedfolderlist_files(folder=DEFAULT_FOLDER)-  list filenames in current folderfiles(folder=DEFAULT_FOLDER)-  list file objects in the specifiedfolderlist_folder_contents(folder=DEFAULT_FOLDER, sort=True)-  list folder
and file names in the specified folder)file_exists(filename, folder=DEFAULT_FOLDER)-  Return true if filename
exists on the serverget_file_info(filename, folder=DEFAULT_FOLDER)-  Get file object for
filenameget_file(filename, folder=DEFAULT_FOLDER)-  (UNFINISHED) get file contents
from sharepointcheck_out_file(filename, folder=DEFAULT_FOLDER)-  Checkout a  filecheck_in_file(filename, folder=DEFAULT_FOLDER, comment="Auto Updated")-
check in a checked out fileadd_folder_path(full_folder, known_folders=None)-  Create this folder
and all parent folders, if missingadd_folder(folder=DEFAULT_FOLDER)-  Create folderupload_file(filename, target_file=None, folder=DEFAULT_FOLDER, check_out=True)- upload a file to folder. Creates folder path and
optionally checks out the file.upload_page(filename, target_file=None, folder=DEFAULT_FOLDER,  check_out=True)- attempts to upload file as a web templateTODO: More documentation