Add MS Teams website tab using graph API error

Viewed 1297

Context:

I am trying to add a new website tab to an existing channel in MS Teams and then get the id of newly-created tab.

Problem:

I am able to create new tab but I am getting a "BadRequest" exception from the Graph:

Message: Value cannot be null. Parameter name: entity

The interesting part is that the tab is created and visible in MS Teams in the correct team and channel but I cannot get it's id in any way.

My code:

var tab = await _graphClient.Teams[teamId].Channels[channelId].Tabs.Request().WithMaxRetry(3).AddAsync(
    new TeamsTab
    {
        DisplayName = "New Tab",
        AdditionalData = new Dictionary<string, object>
        {
            ["teamsApp@odata.bind"] =
                $"{_teamsFactory.GraphV1Endpoint}/appCatalogs/teamsApps/com.microsoft.teamspace.tab.web"
        },
        Configuration = new TeamsTabConfiguration
        {
            EntityId = null,
            WebsiteUrl = $"{_appUrl}/1",
            ContentUrl = $"{_appUrl}/1",
            RemoveUrl = null,

        }
    }
);

Like I wrote above, this code works and the tab is created but GraphServiceClient throws an exception before the tab variable is assigned.

And when I tried to get the tab list in Graph Explorer

https://graph.microsoft.com/v1.0/teams/{teamid}/channels/{channelid}/tabs

I received an error response:

{
    "error": {
        "code": "InternalServerError",
        "message": "Failed to execute request.",
        "innerError": {
            "request-id": "a03654e8-37a7-4fbb-8052-6a1b11721234",
            "date": "2020-02-24T15:11:54"
        }
    }
}
3 Answers

I think you might need to set a value for "EntityId" - basically just a string value to uniquely "name" your tab. It's not the "DisplayName", more a string "id" for the tab.

POST https://graph.microsoft.com/v1.0/teams/{id}/channels/{id}/tabs
{
  "displayName": "My Contoso Tab",
  "teamsApp@odata.bind" : "https://graph.microsoft.com/v1.0/appCatalogs/teamsApps/06805b9e-77e3-4b93-ac81-525eb87513b8",
  "configuration": {
    "entityId": "2DCA2E6C7A10415CAF6B8AB6661B3154",
    "contentUrl": "https://www.contoso.com/Orders/2DCA2E6C7A10415CAF6B8AB6661B3154/tabView",
    "websiteUrl": "https://www.contoso.com/Orders/2DCA2E6C7A10415CAF6B8AB6661B3154",
    "removeUrl": "https://www.contoso.com/Orders/2DCA2E6C7A10415CAF6B8AB6661B3154/uninstallTab"
  }
}

Please take a look at Add Tab to a channel using Graph API

Edit 1: Could you please check you have appropriate permissions to add the Tab?

Edit2: Could you please try below piece of code?

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var teamsTab = new TeamsTab
{
    DisplayName = "WebsiteTab",
    AdditionalData = new Dictionary<string, object>()
    {
        {"teamsApp@odata.bind","https://graph.microsoft.com/v1.0/appCatalogs/teamsApps/com.microsoft.teamspace.tab.web"}
    },
    Configuration = new TeamsTabConfiguration
    {
        EntityId = null,
        ContentUrl = "https://docs.microsoft.com/en-us/microsoftteams/platform/resources/bot-v3/bots-context",
        RemoveUrl = null,
        WebsiteUrl = "https://docs.microsoft.com/en-us/microsoftteams/platform/resources/bot-v3/bots-context"
    }
};

await graphClient.Teams["TeamId"].Channels["ChannelId"].Tabs
    .Request()
    .AddAsync(teamsTab); 

Finally I found the "solution" though a better name is a workaround for my issue. To make it work I had to set ODataType to null in TeamsTabConfiguration. That's all. The code should look like this:

var tab = await _graphClient.Teams[teamId].Channels[channelId].Tabs.Request().WithMaxRetry(3).AddAsync(
new TeamsTab
{
    DisplayName = TabTitle,
    ODataBind = $"{_teamsFactory.GraphV1Endpoint}/appCatalogs/teamsApps/com.microsoft.teamspace.tab.web",
    Configuration = new TeamsTabConfiguration
    {
        ODataType = null,
        EntityId = null,
        WebsiteUrl = $"{_appUrl}/1",
        ContentUrl = $"{_appUrl}/1",
        RemoveUrl = null
    }
});

Like I mentioned it is only a workaround. It is labeled as "service bug" on GitHub (issue#598)

Related