vgrem / Office365-REST-Python-Client

Microsoft 365 & Microsoft Graph Library for Python
MIT License
1.33k stars 334 forks source link

SharePoint EnsureUser Endpoint Returning Error for New User #832

Open matt-davis27 opened 8 months ago

matt-davis27 commented 8 months ago

I'm attempting to add a user to my SharePoint site who does not already exist using the ensure_user function in the office365.sharepoint.webs.web file. However, I'm facing an error that the user doesn't exist which is the whole purpose of the function according to its docstring "Checks whether the specified logon name belongs to a valid user of the website, and if the logon name does not already exist, adds it to the website". Can you please check to see if this is a bug?

Code:

from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext

# authenticate using User's credentials
site_url = "https://domain.sharepoint.com/sites/<my-site>"
user_credentials = UserCredential(username, password)
ctx = ClientContext(site_url).with_credentials(user_credentials)

ctx.web.ensure_user(login_name='username@domain.com').execute_query()

Error:

---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
File ~/Library/Python/3.12/lib/python/site-packages/office365/runtime/client_request.py:38, in ClientRequest.execute_query(self, query)
     37 response = self.execute_request_direct(request)
---> 38 response.raise_for_status()
     39 self.process_response(response, query)

File ~/Library/Python/3.12/lib/python/site-packages/requests/models.py:1021, in Response.raise_for_status(self)
   1020 if http_error_msg:
-> 1021     raise HTTPError(http_error_msg, response=self)

HTTPError: 400 Client Error: Bad Request for url: https://domain.sharepoint.com/sites/<my_site>/_api/Web/EnsureUser('username@domain.com')

During handling of the above exception, another exception occurred:

ClientRequestException                    Traceback (most recent call last)
Cell In[15], line 1
----> 1 ctx.web.ensure_user(login_name='username@domain.com').execute_query()

File ~/Library/Python/3.12/lib/python/site-packages/office365/runtime/client_object.py:52, in ClientObject.execute_query(self)
     49 def execute_query(self):
     50     # type: () -> Self
     51     """Submit request(s) to the server."""
---> 52     self.context.execute_query()
     53     return self

File ~/Library/Python/3.12/lib/python/site-packages/office365/runtime/client_runtime_context.py:185, in ClientRuntimeContext.execute_query(self)
    183 while self.has_pending_request:
    184     qry = self._get_next_query()
--> 185     self.pending_request().execute_query(qry)
    186 return self

File ~/Library/Python/3.12/lib/python/site-packages/office365/runtime/client_request.py:42, in ClientRequest.execute_query(self, query)
     40     self.afterExecute.notify(response)
     41 except HTTPError as e:
---> 42     raise ClientRequestException(*e.args, response=e.response)

ClientRequestException: ('-2146232832, Microsoft.SharePoint.SPException', 'The specified user username@domain.com could not be found.', "400 Client Error: Bad Request for url: https://domain.sharepoint.com/sites/<my_site>/_api/Web/EnsureUser('username@domain.com')")

Environment configuration: Python 3.12.1 Office365-REST-Python-Client 2.5.3

jussihi commented 8 months ago

@matt-davis27 , did you get this problem solved? I'm facing similar issue.