Skip to content

Commit

Permalink
Merge pull request #1735 from milanholemans/add-channel-user-command
Browse files Browse the repository at this point in the history
Created Add-PnpTeamsChannelUser cmdlet
  • Loading branch information
KoenZomers authored Apr 14, 2022
2 parents 8633b5a + eb948c2 commit a10a860
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

### Added
- Added `-Wait` and `-Verbose` optional paramarers to `New-PnPUPABulkImportJob` [#1752](https://github.com/pnp/powershell/pull/1752)
- Added `Add-PnpTeamsChannelUser` which allows members and owners to be added to private channels in Teams

### Changed
- Changed `Sync-PnPSharePointUserProfilesFromAzureActiveDirectory` to map users based on their Ids instead which should resolve some issues around user identities reporting not to exist. You can use the new `-IdType` option to switch it back to `PrincipalName` if needed. [#1752](https://github.com/pnp/powershell/pull/1752)
Expand All @@ -18,7 +19,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
### Removed

### Contributors

- Milan Holemans [milanholemans]
- Arleta Wanat [PowershellScripts]
- Koen Zomers [koenzomers]
- James May [fowl2]
Expand Down
105 changes: 105 additions & 0 deletions documentation/Add-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/Add-PnPTeamsChannelUser.html
external help file: PnP.PowerShell.dll-Help.xml
title: Add-PnPTeamsChannelUser
---

# Add-PnPTeamsChannelUser

## SYNOPSIS

**Required Permissions**

* Microsoft Graph API: ChannelMember.ReadWrite.All

Adds a user to an existing Microsoft Teams private channel.

## SYNTAX

```powershell
Add-PnPTeamsChannelUser -Team <TeamsTeamPipeBind> -Channel <TeamsChannelPipeBind> -User <String> -Role <String> [<CommonParameters>]
```

## DESCRIPTION

## EXAMPLES

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

Adds user as an owner to the private channel.

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

Adds user as a member to the 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: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -User
Specify the UPN (e.g. [email protected])
```yaml
Type: String
Parameter Sets: (User)

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)
2 changes: 0 additions & 2 deletions src/Commands/Model/Teams/TeamChannelMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,4 @@ public class TeamChannelMember
[JsonPropertyName("email")]
public string email { get; set; }
}


}
56 changes: 56 additions & 0 deletions src/Commands/Teams/AddTeamsChannelUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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.Add, "PnPTeamsChannelUser")]
[RequiredMinimalApiPermissions("ChannelMember.ReadWrite.All")]
public class AddTeamsChannelUser : PnPGraphCmdlet
{
[Parameter(Mandatory = true)]
public TeamsTeamPipeBind Team;

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

[Parameter(Mandatory = true)]
public string User;

[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");
}

try
{
TeamsUtility.AddChannelUserAsync(HttpClient, AccessToken, groupId, channelId, User, Role).GetAwaiter().GetResult();
}
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 @@ -12,6 +12,7 @@
using System.Text.Json;
using System.Threading.Tasks;
using Group = PnP.PowerShell.Commands.Model.Graph.Group;
using Team = PnP.PowerShell.Commands.Model.Teams.Team;
using TeamChannel = PnP.PowerShell.Commands.Model.Teams.TeamChannel;
using User = PnP.PowerShell.Commands.Model.Teams.User;

Expand Down Expand Up @@ -609,6 +610,20 @@ public static async Task<TeamChannel> AddChannelAsync(string accessToken, HttpCl
}
}

public static async Task<TeamChannelMember> AddChannelUserAsync(HttpClient httpClient, string accessToken, string groupId, string channelId, string upn, string role)
{
var channelMember = new TeamChannelMember
{
UserIdentifier = $"https://graph.microsoft.com/v1.0/users('{upn}')",
};

// The role for the user. Must be owner or empty.
if (role.Equals("Owner"))
channelMember.Roles.Add("owner");

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

public static async Task PostMessageAsync(HttpClient httpClient, string accessToken, string groupId, string channelId, TeamChannelMessage message)
{
await GraphHelper.PostAsync(httpClient, $"v1.0/teams/{groupId}/channels/{channelId}/messages", message, accessToken);
Expand Down

0 comments on commit a10a860

Please sign in to comment.