Update user using C# client for Graph API

Eivind Gussiås Løkseth 21 Reputation points
2022-08-02T21:26:10.603+00:00

So many questions about this on Stack Overflow. Some back in 2016 says that an application permission on Directory is necessary to update User profile, but that permission no longer exists.

I think it should be sufficient to have User.ReadWrite permission to be able to update profile information on the current user. Shouldn't this code just work with that permission set?

        [HttpPost]  
        [ValidateAntiForgeryToken]  
        public async Task<IActionResult> Index([Bind("Id,Name,FirstName,LastName,City,Email,HomePage,Biography,BirthDate")] ProfileViewModel viewModel)  
        {  
            if (ModelState.IsValid)  
            {  
                var user = await _graphServiceClient.Me.Request().GetAsync();  
                var updatedValues = new User  
                {  
                    City = viewModel.City  
                };  
                var updatedUser = await _graphServiceClient.Users[user.Id].Request().UpdateAsync(updatedValues);  
  
                return RedirectToAction(nameof(Index));  
            }  
  
            return View(viewModel);  
        }  

updatedUser is null, and I don't get any exception. Looks like a bug to me. I've wasted a day on this now, so I think I'll just create my own storage for user profile data and implement my own API.

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
22,199 questions
{count} votes

Accepted answer
  1. 2022-08-05T06:34:53.727+00:00

    Hi @ eivind1980 , Sure i can understand that this is a kind of limitation more than a bug with the permissions, but as per document for updating user details these are the permissions required.

    228319-image.png

    Reference document:https://learn.microsoft.com/en-us/graph/api/user-update?view=graph-rest-1.0&tabs=csharp#permissions

    But if you want you raise a Feature request for this in the Microsoft portal and upvote it: https://techcommunity.microsoft.com/t5/microsoft-365-developer-platform/idb-p/Microsoft365DeveloperPlatform

    ----------

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

    1 person found this answer helpful.

0 additional answers

Sort by: Most 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.