Initially I got the **same error**:
![enter image description here](https://i.imgur.com/YuFfAr1.png)
**To resolve the error** to update profile photo of user, you need to get user by UPN by passing below code snippet:
```csharp
Var user = await GraphClient.Users[userPrincipalName].GetAsync();
```
**To update the user's profile picture by passing UPN, modify the code like below:**
```csharp
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models.ODataErrors;
namespace UserProperties
{
public class GraphHandler
{
public GraphServiceClient GraphClient { get; set; }
public GraphHandler()
{
var tenantId = "XXX";
var clientId = "XXX";
var clientSecret = "XXX";
GraphClient = CreateGraphClient(tenantId, clientId, clientSecret);
}
public GraphServiceClient CreateGraphClient(string tenantId, string clientId, string clientSecret)
{
var options = new TokenCredentialOptions
{
AuthorityHost = Azure.Identity.AzureAuthorityHosts.AzurePublicCloud
};
var clientSecretCredential = new Azure.Identity.ClientSecretCredential(tenantId, clientId, clientSecret, options);
var scopes = new[] { "https://graph.microsoft.com/.default" };
return new GraphServiceClient(clientSecretCredential, scopes);
}
public async Task<bool> UpdateProfilePictureByUPN(string userPrincipalName, string imagePath)
{
try
{
using (var stream = new FileStream(imagePath, FileMode.Open))
{
// Get user by UPN
var user = await GraphClient.Users[userPrincipalName].GetAsync();
if (user != null)
{
// Update profile picture
await GraphClient.Users[user.Id].Photo.Content.PutAsync(stream);
Console.WriteLine("Profile picture updated successfully.");
return true;
}
else
{
Console.WriteLine($"User with UPN '{userPrincipalName}' not found.");
return false;
}
}
}
catch (ODataError odataError)
{
Console.WriteLine($"OData error details:");
Console.WriteLine($"Code: {odataError.Error?.Code}");
Console.WriteLine($"Message: {odataError.Error?.Message}");
throw;
}
catch (ServiceException ex)
{
Console.WriteLine($"Error updating profile picture: {ex.Message}");
return false;
}
}
}
class Program
{
static async Task Main(string[] args)
{
var handler = new GraphHandler();
var userPrincipalName = "rukminitest@XXX.onmicrosoft.com"; // Replace with the desired user's UPN
await handler.UpdateProfilePictureByUPN(userPrincipalName, "C:\\Users\\rukmini\\Downloads\\ruk.jpg");
}
}
}
```
![enter image description here](https://i.imgur.com/yB5z1m2.png)
**The profile picture updated successfully like below:**
![enter image description here](https://i.imgur.com/7Ui0oGh.png)
Make sure to grant **`User.ReadWrite.All`** application type API permission:
![enter image description here](https://i.imgur.com/ijeQSXo.png)
**To update the profile photo of a user, please use the below code:**
```csharp
using System;
using System.IO;
using System.Threading.Tasks;
using Azure.Identity;
using Microsoft.Graph;
namespace UserProperties
{
public class GraphHandler
{
public GraphServiceClient GraphClient { get; set; }
public GraphHandler()
{
var tenantId = "TenantID";
var clientId = "ClientID";
var clientSecret = "ClientSecret";
GraphClient = CreateGraphClient(tenantId, clientId, clientSecret);
}
public GraphServiceClient CreateGraphClient(string tenantId, string clientId, string clientSecret)
{
var options = new TokenCredentialOptions
{
AuthorityHost = Azure.Identity.AzureAuthorityHosts.AzurePublicCloud
};
var clientSecretCredential = new Azure.Identity.ClientSecretCredential(tenantId, clientId, clientSecret, options);
var scopes = new[] { "https://graph.microsoft.com/.default" };
return new GraphServiceClient(clientSecretCredential, scopes);
}
public async Task<bool> UpdateProfilePicture(string userId, string imagePath)
{
try
{
using (var stream = new FileStream(imagePath, FileMode.Open))
{
await GraphClient.Users[userId].Photo.Content.PutAsync(stream);
Console.WriteLine("Profile picture updated successfully.");
return true;
}
}
catch (ServiceException ex)
{
Console.WriteLine($"Error updating profile picture: {ex.Message}");
return false;
}
}
}
class Program
{
static async Task Main(string[] args)
{
var handler = new GraphHandler();
var userId = "UserID"; // Replace with the desired user's ID
await handler.UpdateProfilePicture(userId, "C:\\Users\\rukmini\\Downloads\\ruk.jpg");
}
}
}
```
![enter image description here](https://i.imgur.com/tBO7z0M.png)
Profile photo updated successfully:
![enter image description here](https://i.imgur.com/2Wehql8.png)