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.21.0 of Microsoft.Graph library.

Code updated to latest version March 30, 2021.

 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var team = new Team()
{
    ODataType = null,
    MessagingSettings = sourceTeam.MessagingSettings,
    FunSettings = sourceTeam.FunSettings,
    GuestSettings = sourceTeam.GuestSettings,
    MemberSettings = sourceTeam.MemberSettings
};

team.MessagingSettings.ODataType = null;
team.FunSettings.ODataType = null;
team.GuestSettings.ODataType = null;
team.MemberSettings.ODataType = null;

Team newTeam;

try
{
    newTeam = await retry.ExecuteAsync(async () =>
    {
        return await graphClient.Groups[newGroup.Id].Team
        .Request()
        .PutAsync(team);
    });
}
catch (Exception ex)
{
    logger?.LogError($"Error adding team to group: {ex.Message} {newGroup.Id}");
    return;
}

// get new team channels
var newTeamId = newTeam.Id;

logger?.LogInformation($"Getting new team channels {newTeamId}");

var newTeamChannels = await retry.ExecuteAsync(async () =>
{
    return await graphClient.Teams[newTeamId].Channels.Request().GetAsync();
});

logger?.LogInformation($"Getting new team tabs {newTeamId}");

// and tabs
foreach (var chan in newTeamChannels)
{
    chan.Tabs = await retry.ExecuteAsync(async () =>
    {
        return await graphClient.Teams[newTeamId].Channels[chan.Id].Tabs.Request().Expand("TeamsApp").GetAsync();
    });

    await Content.RemoveWikiTab(newTeamId, chan, retry, graphClient, logger);
}

// Define RemoveWikiTab somewhere else

/// <summary>
/// Removes wiki tab from given channel
/// </summary>
/// <param name="newTeamId"></param>
/// <param name="channel"></param>
/// <param name="retry"></param>
/// <param name="graphClient"></param>
/// <param name="logger"></param>
/// <returns></returns>
public static async Task RemoveWikiTab(string newTeamId, Microsoft.Graph.Channel channel, AsyncRetryPolicy retry, GraphServiceClient graphClient, ILogger logger)
{
    for (int i = channel.Tabs.Count - 1; i >= 0; i--)
    {
        if (channel.Tabs[i].TeamsApp.Id == TeamsAppId.Wiki)
        {
            try
            {
                await retry.ExecuteAsync(async () =>
                {
                    logger?.LogInformation($"Removing wiki on channel {channel.DisplayName}. {newTeamId}");

                    await graphClient.Teams[newTeamId].Channels[channel.Id].Tabs[channel.Tabs[i].Id].Request().DeleteAsync();
                });

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

2 comments:

  1. Did you find a way to improve the script yet or is this the one you use today still? Thanks!

    ReplyDelete
    Replies
    1. I didn't put the Polly retry logic into GraphServiceClient, but did do some refactoring and separated the Wiki tab removal into own function.

      Delete