Navoneel Talukdar
Navoneel Talukdar

Reputation: 4598

Update sensitivity label to office365 group using graph api

I am trying to update the sensitivity label on a office 365 group using graph api as shown here, however I always keep receiving error.

I am refering this link(https://learn.microsoft.com/en-us/microsoft-365/compliance/sensitivity-labels-teams-groups-sites?view=o365-worldwide) where I saw recently microsoft has released public preview of this sensitivity label feature which can be applied on groups or teams. In my tenant the sensitivity feature has been enabled.

enter image description here

Below is the code which I am using for updating label. I am using latest version of Microsoft.Graph package from nuget.

MSGraph.GraphServiceClient graphClient = GetAuthenticatedClient();   
string grpId = "<group id here>"; // irrelevant not shown here how group id is acquired
var groupToUpdate = new Group
{
       AssignedLabels = new List<AssignedLabel>()
       {
             new AssignedLabel
             {
                LabelId = "480dd7e5-2378-47bc-a023-511ad6a967ce"                                           
             }
        }
};     
graphClient.Groups[grpId].Request().UpdateAsync(groupToUpdate).Wait();

The error I receive is

enter image description here

and full stacktrace

enter image description here

Can anyone throw some light on what I am missing here?

Upvotes: 1

Views: 4671

Answers (1)

Allen Wu
Allen Wu

Reputation: 16458

I can hardly tell that this is an NullReferenceException.

I have tested it in both Microsoft Graph Explorer and code and both of them work fine from my side.

So for troubleshooting, you could test it in Microsoft Graph Explorer. If it gives the same issue, there should be something wrong with Microsoft Graph service in your region. Just wait for repair.

If it works fine in Graph explorer but still doesn't work in code, you could get the code snippets from Graph explorer and test it in your code.

enter image description here

BTW, here is my code for your reference (I use Username/password provider for convenience).

        IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
        .Create(clientId)
        .WithTenantId(tenantID)
        .Build();

        string[] scopes = new string[] { "https://graph.microsoft.com/.default" };

        UsernamePasswordProvider authProvider = new UsernamePasswordProvider(publicClientApplication, scopes);

        GraphServiceClient graphClient = new GraphServiceClient(authProvider);

        var str = "{my password here}";
        var password = new SecureString();
        foreach (char c in str) password.AppendChar(c);

        User me = await graphClient.Me.Request()
                        .WithUsernamePassword("{my user name here}", password)
                        .GetAsync();

        var group = new Group
        {
            AssignedLabels = new List<AssignedLabel>()
            {
                new AssignedLabel
                {
                    LabelId = "38feb82c-de40-4a15-b706-5faa1202e103"
                }
            }
        };
        string grpId = "45e095a6-5c43-4832-b82c-6aa586176d31";
        graphClient.Groups[grpId].Request().UpdateAsync(group).Wait();

Upvotes: 0

Related Questions