Search code examples
c#microsoft-graph-apimicrosoft-teamsmicrosoft-graph-teams

Problem with add channel to Microsoft Team (C# code, cast channel type to interface)


i have a application in Microsoft teams, and i want add channel from Visual Studio (c#). In the examples in the documentation, we can see this lines:

Channels = (ITeamChannelsCollectionPage)new List<Channel>()
{
    new Channel
    {
        DisplayName = "Class Announcements ",
        IsFavoriteByDefault = true
    },
    new Channel
    {
        DisplayName = "Homework ",
        IsFavoriteByDefault = true
    }
},

But if i try do that in Visual Studio i get a InvalidCastExeption

exeption

my code and Example code are equal

i think the SDK(Microsoft Graph) was updated, but documentation - not

My Microsoft.Graph SDK version - 3.9.0

PS. problem with cast type "Channel" to interface "ITeamChannelsCollectionPage"


Solution

  • So the documentation there is completely wrong - you cannot cast like that. The correct way to use the Graph SDK is like this:

    var team = new Team
    {
        Channels = new TeamChannelsCollectionPage
        {
            new Channel
            {
                DisplayName = "Class Announcements"
            },
            new Channel
            {
                DisplayName = "Homework "
            }
        }
    }
    

    Note: The IsFavoriteByDefault property does not exist, it was perhaps valid in an older version of the SDK.