ctx.web.site_users.get_by_email(user_email) returns with E404 - how to fix?

Tamás Kőhegyi 1 Reputation point
2024-10-06T16:49:02.5033333+00:00

Dear Support,

I use python to work with Sharepoint lists, and need to fill a "Users and Groups" type of column from the code. For this, the e-mail need to be transferred into user id. To lookup the user id I use this code

from office365.sharepoint.client_context import ClientContext
....
user = ctx.web.site_users.get_by_email(user_email)
ctx.load(user)
ctx.execute_query()
user_id = user.properties['Id']

This works in general, but only in case the user is available in the user list of https://[company-sharepoint]/sites/[site-name]/_catalogs/users/simple.aspx page, but if the user is not there, it does not work - that case the execute_query() call returns with office365.runtime.client_request_exception.ClientRequestException: ('-2130575276, Microsoft.SharePoint.SPException', 'User cannot be found.', "404 Client Error:...
Why it is a problem? Because the user exists!
From the UI, when I try to fill the column manually, I can select the User (so the user exists) and I can find and select the user even if the person is not yet mentioned in the site catalog.
So - could you pls help how I can do it by enhancing the python code, meanwhile the only info I have is the person's e-mail + the fact, that I can do it manually on the UI?

Microsoft 365 and Office | SharePoint | Development
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Emily Du-MSFT 51,836 Reputation points Microsoft External Staff
    2024-10-07T08:19:54.2833333+00:00

    Per my knowledge, in the SharePoint, it's by designed that when using Python or Graph, you could only through user ID in the site collection user information list to update a person column. But when using UI, you could choose all users in the tenant to update a person column.

    First, query site collection user information list to get user ID.

    https://graph.microsoft.com/v1.0/sites/{site collection guid}/lists/{user info list guid}/items?$select=Fields&$filter=Fields/Title eq 'User Display name'&$expand=Fields
    

    Then, use Python or Graph to update a person column.


    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.