Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds cmdlet Set-PnPTeamsChannelUser #1865

Merged
merged 2 commits into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion documentation/Add-PnpTeamsChannelUser.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ The id or name of the channel to retrieve.
Type: TeamsChannelPipeBind
Parameter Sets: (All)

Required: False
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Expand Down
105 changes: 105 additions & 0 deletions documentation/Set-PnpTeamsChannelUser.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
Module Name: PnP.PowerShell
schema: 2.0.0
applicable: SharePoint Online
online version: https://pnp.github.io/powershell/cmdlets/Set-PnPTeamsChannelUser.html
external help file: PnP.PowerShell.dll-Help.xml
title: Set-PnPTeamsChannelUser
---

# Set-PnPTeamsChannelUser

## SYNOPSIS

**Required Permissions**

* Microsoft Graph API: ChannelMember.ReadWrite.All

Updates the role of a user in an existing Microsoft Teams private channel.

## SYNTAX

```powershell
Set-PnPTeamsChannelUser -Team <TeamsTeamPipeBind> -Channel <TeamsChannelPipeBind> -Identity <TeamsChannelMemberPipeBind> -Role <String> [<CommonParameters>]
```

## DESCRIPTION

## EXAMPLES

### EXAMPLE 1
```powershell
Set-PnPTeamsChannelUser -Team 4efdf392-8225-4763-9e7f-4edeb7f721aa -Channel "19:[email protected]" -Identity MCMjMiMjMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwIyMxOTowMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMEB0aHJlYWQuc2t5cGUjIzAwMDAwMDAwLTAwMDAtMDAwMC0wMDAwLTAwMDAwMDAwMDAwMA== -Role Owner
```

Updates the user with specific membership ID as owner of the specified Teams private channel.

### EXAMPLE 2
```powershell
Set-PnPTeamsChannelUser -Team "My Team" -Channel "My Private Channel" -Identity [email protected] -Role Member
```

Updates the user [email protected] as member of the specified Teams private channel.

## PARAMETERS

### -Team
Specify the group id, mailNickname or display name of the team to use.

```yaml
Type: TeamsTeamPipeBind
Parameter Sets: (All)

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -Channel
The id or name of the channel to retrieve.

```yaml
Type: TeamsChannelPipeBind
Parameter Sets: (All)

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -Identity
Specify membership id, UPN or user ID of the channel member.

```yaml
Type: TeamsChannelMemberPipeBind
Parameter Sets: (All)

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -Role
Specify the role of the user

```yaml
Type: String
Parameter Sets: (All)
Accepted values: Owner, Member

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

## RELATED LINKS

[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
63 changes: 63 additions & 0 deletions src/Commands/Teams/SetTeamsChannelUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using PnP.PowerShell.Commands.Attributes;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Model.Graph;
using PnP.PowerShell.Commands.Utilities;
using System.Management.Automation;

namespace PnP.PowerShell.Commands.Teams
{
[Cmdlet(VerbsCommon.Set, "PnPTeamsChannelUser")]
[RequiredMinimalApiPermissions("ChannelMember.ReadWrite.All")]
public class SetTeamsChannelUser : PnPGraphCmdlet
{
[Parameter(Mandatory = true)]
public TeamsTeamPipeBind Team;

[Parameter(Mandatory = true)]
public TeamsChannelPipeBind Channel;

[Parameter(Mandatory = true)]
public TeamsChannelMemberPipeBind Identity;

[Parameter(Mandatory = true)]
[ValidateSet("Owner", "Member")]
public string Role;

protected override void ExecuteCmdlet()
{
var groupId = Team.GetGroupId(HttpClient, AccessToken);
if (groupId == null)
{
throw new PSArgumentException("Group not found");
}

var channelId = Channel.GetId(HttpClient, AccessToken, groupId);
if (channelId == null)
{
throw new PSArgumentException("Channel not found");
}

var membershipId = Identity.GetIdAsync(HttpClient, AccessToken, groupId, channelId).GetAwaiter().GetResult();
if (string.IsNullOrEmpty(membershipId))
{
throw new PSArgumentException("User was not found in the specified Teams channel");
}

try
{
var updatedMember = TeamsUtility.UpdateChannelMemberAsync(HttpClient, AccessToken, groupId, channelId, membershipId, Role).GetAwaiter().GetResult();
WriteObject(updatedMember);
}
catch (GraphException ex)
{
if (ex.Error != null)
{
throw new PSInvalidOperationException(ex.Error.Message);
}

throw;
}
}
}
}
15 changes: 15 additions & 0 deletions src/Commands/Utilities/TeamsUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,21 @@ public static async Task<HttpResponseMessage> DeleteChannelMemberAsync(HttpClien
return await GraphHelper.DeleteAsync(httpClient, $"v1.0/teams/{groupId}/channels/{channelId}/members/{membershipId}", accessToken);
}

/// <summary>
/// Update the role of a specific member of a Microsoft Teams channel.
/// </summary>
/// <returns>Updated membership object.</returns>
public static async Task<TeamChannelMember> UpdateChannelMemberAsync(HttpClient httpClient, string accessToken, string groupId, string channelId, string membershipId, string role)
{
var channelMember = new TeamChannelMember();

// User role. Empty for member, 'owner' for owner.
if (role.Equals("owner", StringComparison.OrdinalIgnoreCase))
channelMember.Roles.Add("owner");

return await GraphHelper.PatchAsync(httpClient, accessToken, $"v1.0/teams/{groupId}/channels/{channelId}/members/{membershipId}", channelMember);
}

#endregion

#region Tabs
Expand Down