Microsoft Graph Beta - 用户更新 - 目标实体集当前不支持请求不支持、实体、目标、用户

2023-09-06 17:47:51 作者:俺媳妇长得丑不会听你乱说

I get the following error when Updating a user using Microsoft Graph Beta. Permissions should not be an issue as I'm using the following permissions: user.readwrite.all, directory.readwrite.all. Also, HireDate is of type DateTimeOffSet that I'm getting by converting local DateTime to DateTimeOffSet. So, that should not be an issue either. Question: What could be the cause of the error and how can it be fixed?

Microsoft Graph 桌面应用程序

Error:

Microsoft.Graph.ServiceException
  HResult=0x80131500
  Message=Code: BadRequest
Message: The request is currently not supported on the targeted entity set

Inner error:
    AdditionalData:
    date: 2020-08-31T13:59:10
    request-id: 4b62576f-6572-414c-b9ef-07ea9a61c101
ClientRequestId: 4b62576f-6572-414c-b9ef-07ea9a61c101

Code:

private async void DataGridUserUpdateButton_Click(object sender, RoutedEventArgs e)
{
  GraphServiceClient graphClient = new GraphServiceClient(authProvider);

  User user = (sender as Button).DataContext as User;

  string id = user.UserPrincipalName;

   var user = new User
   {
     Department = "IT",
     DisplayName = "Bob Doe",
     HireDate = new DateTime(2020, 8, 31, 2, 30, 0),
     JobTitle = "IT Manager"
   };

  await graphClient.Users[sId]
            .Request()
            .UpdateAsync(user);
}

解决方案

According to some test, I met the same issue as yours. I test it in code with both client_credential grant flow and username/password grant flow(and add enough permissions) but none of them succeeded. Then I test it in graph explorer but also failed. It seems the property hireDate can't be updated success.

For a wordaround, I found another property maybe you can use, you can use employeeHireDate. I already test to update this property, it works fine. This property just exists when we use beta graph api and we can't find it neither in v1.0 user properties nor in beta user properties document.

To update employeeHireDate in code, please refer to the code below:

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var user = new User
{
    AdditionalData = new Dictionary<string, object>()
    {
        {"employeeHireDate", "2019-01-01T00:00:00Z"}
    }
};

await graphClient.Users["userId"]
    .Request()
    .UpdateAsync(user);

By the way, you need to pay attention to the properties in beta version because it may be changed in future.

==================================Update==================================