How to read SharePoint List With Token and Python?

PABLO TERRA SANTOS 0 Reputation points
2023-07-07T13:27:19.09+00:00

Hi! All sites in web return "How connect sharepoint list with token, like this, but, all return to read a Site Title!

How can I get item from list?

site_url = 'https://site.sharepoint.com/sites/site'
client_id = 'client_id'
client_secret = 'client_secret '

app_principal = {'client_id': client_id, 'client_secret': client_secret}

context_auth = AuthenticationContext(url=site_url)

token = context_auth.acquire_token_for_app(client_id=app_principal['client_id'], client_secret=app_principal['client_secret'])
print(token)

ctx = ClientContext(site_url, context_auth)

web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Web site title: {0}".format(web.properties['Title']))
Microsoft 365 and Office SharePoint For business Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-07-10T01:46:01.04+00:00

    Hi @PABLO TERRA SANTOS,

    I will recommend you to use Office365-REST-Python-Client to read the items from SharePoint list. Please refer to the following code

    import datetime
    
    from office365.sharepoint.client_context import ClientContext
    from office365.sharepoint.listitems.listitem import ListItem
    from tests import test_client_credentials, test_team_site_url
    
    
    ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
    list_title = "Site Pages"
    site_pages = ctx.web.lists.get_by_title(list_title)
    from_datetime = datetime.datetime(2022, 1, 20, 0, 0)
    filter_text = "Created gt datetime'{0}'".format(from_datetime.isoformat())
    include_fields = ["Created", "EncodedAbsUrl"]
    items = site_pages.items.filter(filter_text).select(include_fields).get().execute_query()
    print("Loaded items count: {0}".format(len(items)))
    for index, item in enumerate(items):  # type: int, ListItem
        print("{0}: {1}".format(index, item.properties['EncodedAbsUrl']))
    
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.