Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SharePoint EnsureUser Endpoint Returning Error for New User #832

Open
matt-davis27 opened this issue Feb 27, 2024 · 1 comment
Open

SharePoint EnsureUser Endpoint Returning Error for New User #832

matt-davis27 opened this issue Feb 27, 2024 · 1 comment
Labels

Comments

@matt-davis27
Copy link

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

@matt-davis27 matt-davis27 changed the title SharePoint EnsureUser Function Returning Error for New User SharePoint EnsureUser Endpoint Returning Error for New User Feb 28, 2024
@vgrem vgrem added the question label Mar 4, 2024
@jussihi
Copy link
Contributor

jussihi commented Mar 7, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants