Is there way to retrive data from sharepoint list, modify the list and upload list back to sharepoint using python?

Jenifa Thomas 20 Reputation points
2023-08-01T11:55:00.6633333+00:00

I have a sharepoint list which stores data from power apps. Whenever a user fills the form in power apps,while submitting the form I want a python script to run which will create some output file. Is there any way through which I can achieve it? I tried to access the sharepoint list using REST API, but got this error below,

{'error': 'invalid_request', 'error_description': "AADSTS900023: Specified tenant identifier 'tenant_id' is neither a valid DNS name, nor a valid external domain.\r\nTrace ID: 20f02c71-c802-467a-a3fe-0b45a08e4c00\r\nCorrelation ID: 03f90547-f507-4595-85a6-003c6e658171\r\nTimestamp: 2023-08-01 09:09:56Z", 'error_codes': [900023], 'timestamp': '2023-08-01 09:09:56Z', 'trace_id': '20f02c71-c802-467a-a3fe-0b45a08e4c00', 'correlation_id': '03f90547-f507-4595-85a6-003c6e658171', 'error_uri': 'https://accounts.accesscontrol.windows.net/error?code=900023'}
Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint Server Development
Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-08-02T01:52:58.31+00:00

    Hi @Jenifa Thomas,

    Since you are using python, I will recommend you to use **Office365-REST-Python-Client**to retrieve and modify the data.

    You can read the data by following code

    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)
    tasks_list = ctx.web.lists.get_by_title("Company Tasks")
    items = tasks_list.items.get().execute_query()
    for item in items:  # type:ListItem
        print("{0}".format(item.properties.get("Title")))
    
    
    

    and modify the item by following code

    import sys
    from random import randint
    
    from office365.sharepoint.client_context import ClientContext
    from office365.sharepoint.taxonomy.field_value import TaxonomyFieldValue
    from tests import test_team_site_url, test_client_credentials
    
    ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
    
    list_tasks = ctx.web.lists.get_by_title("Tasks")
    items = list_tasks.items.get().top(1).execute_query()
    if len(items) == 0:
        sys.exit("No items found")
    
    item_to_update = items[0]
    task_prefix = str(randint(0, 10000))
    # tax_field_value = TaxonomyFieldValue("Sweden", "f9a6dae9-633c-474b-b35e-b235cf2b9e73")
    # item_to_update.set_property("Country", tax_field_value).update().execute_query()
    item_to_update.set_property("Title", f"Task {task_prefix}").update().execute_query()
    print("Item has been updated")
    

    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.


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.