April 3, 2020

Remove Wiki tab during provisioning of Teams channels

Task

Remove Wiki tab during provisioning of Teams channels.

Solution

As I’m not cloning my Team, but instead enumerating channels and tabs of the source Team used as a template, it was quite straightforward to include the Wiki tab remove logic to the stage where I’m anyway fetching Tabs for each source Team Channel.

This one includes Polly retry-logic for the DELETE operation (my first time using Polly so there’s probably lot of room for improvement).

It would also be nice to bake the Polly retry-logic into the GraphServiceClient itself instead of surrounding all calls. If you know it has already been done, please comment below. I’m always looking forward to improve my code.

I’m currently using version 3.1.0 of Microsoft.Graph library.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// init polly
var retry = Policy
    .Handle<ServiceException>(ex =>
        (ex.StatusCode == System.Net.HttpStatusCode.NotFound
        || ex.StatusCode == System.Net.HttpStatusCode.TooManyRequests
        || ex.StatusCode == System.Net.HttpStatusCode.BadGateway)
        )
    .WaitAndRetryAsync(60, _ => TimeSpan.FromSeconds(5)); // retry every 5 secs for 5 minutes
    
// create new team

// get new team channels
var newTeamId = newTeam.Id;
var newTeamChannels = await graphClient.Groups[newTeamId].Team.Channels.Request().GetAsync();

foreach (var chan in newTeamChannels)
{
    chan.Tabs = await graphClient.Teams[newTeamId].Channels[chan.Id].Tabs.Request().Expand("TeamsApp").GetAsync();

    // remove wikis right here, right now
    for (int i = chan.Tabs.Count - 1; i >= 0; i--)
    {
        if (chan.Tabs[i].TeamsApp.Id == "com.microsoft.teamspace.tab.wiki")
        {
            try
            {
                await retry.ExecuteAsync(async () =>
                {
                    await graphClient.Teams[newTeamId].Channels[chan.Id].Tabs[chan.Tabs[i].Id].Request().DeleteAsync();
                });

                chan.Tabs.RemoveAt(i);
            }
            catch (Exception ex)
            {
                logger?.LogInformation($"Error removing wiki tab on channel: {chan.DisplayName}. {ex.Message}");
            }
        }                            
    }
}

No comments:

Post a Comment