简体   繁体   中英

Get the last modified date/time for a Sharepoint File using python Office 365 API

I need to get the last modified date of a document in the SharePoint using SharePoint rest API for python

Basically I want to get the most recently modified file from a SharePoint site I want to get the last modified date to verify if the current document is the latest

I am trying to find if there is any method which can help get the last modified date of documents.

I suggest using O365 Python package.

https://github.com/O365/python-o365

Below example shows how you can print modified time and then download the file. In this example I am downloading excel files.

Note: I have all my passwords and etc stored in a dictionary within a settings file I created hence why you see me importing that file. This is not needed and is a personal preference.

from O365 import Account
from settings import settings

save_path = "\path\to\save\file\\"

# user log in information using client credentials and client id for Microsoft Graph API

credentials = (settings['client_credentials']['client_id'],
               settings['client_credentials']['client_secret'])

# the default protocol will be Microsoft Graph
account = Account(credentials, auth_flow_type='credentials',
                  tenant_id=settings['client_credentials'
                  ]['client_tenant_id'])
if account.authenticate():
    print('Authenticated!')

# initiate share_point account

share_point = account.sharepoint()

# change this site id to whatever your SharePoint site id is

site_id = 'your site id'

# get the site

site = share_point.get_site(site_id)

# create drive item

my_drive = site.get_default_document_library()

# navigate to root folder of the drive

root_folder = my_drive.get_root_folder()

# this will get all the folders under your root folder

child_folders = root_folder.get_child_folders()

for folder in child_folders:
    if folder.name == 'Your SharePoint folder name':
        for item in folder.get_items():
            try:
                if item.name.lower().endswith(('.xls', '.xlsx')):
                    # print last modified date
                    print(item.modified)
                    # download files
                    item.download(save_path)
            except Exception, e:
                print('File not found!')
                print(e)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM