Skip to content

Commit

Permalink
Merge pull request #2419 from gautamdsheth/feature/teamwork-tag-list
Browse files Browse the repository at this point in the history
Feature: Add Set/Remove-PnPTeamsTag cmdlets
  • Loading branch information
KoenZomers authored Oct 6, 2022
2 parents 3fa27c3 + 1aab699 commit 885b82e
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added `Add-PnPPageImageWebPart` cmdlet to allow users to easily add Image to a modern page.
- Added Managed Identity support for SharePoint Online cmdlets. [#2354](https://github.com/pnp/powershell/pull/2354)
- Added `Get-PnPTeamsTag` cmdlet to retrieve Team tags information. [#2414](https://github.com/pnp/powershell/pull/2414)
- Added `Set-PnPTeamsTag` cmdlet to update Team tags information. [#2419](https://github.com/pnp/powershell/pull/2419)
- Added `Remove-PnPTeamsTag` cmdlet to delete a Team tag. [#2419](https://github.com/pnp/powershell/pull/2419)

### Changed
- Changed to no longer require `https://` to be prefixed when using `Connect-PnPOnline -Url tenant.sharepoint.com` [#2139](https://github.com/pnp/powershell/pull/2139)
Expand Down
2 changes: 1 addition & 1 deletion documentation/Get-PnPTeamsTag.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Retrieves a tag with the specified Id from the specified team.
Specify the id of the tag

```yaml
Type: String
Type: TeamsTagPipeBind
Parameter Sets: (All)

Required: False
Expand Down
83 changes: 83 additions & 0 deletions documentation/Remove-PnPTeamsTag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
Module Name: PnP.PowerShell
title: Remove-PnPTeamsTag
schema: 2.0.0
applicable: SharePoint Online
external help file: PnP.PowerShell.dll-Help.xml
online version: https://pnp.github.io/powershell/cmdlets/Remove-PnPTeamsTag.html
---

# Remove-PnPTeamsTag

## SYNOPSIS

**Required Permissions**

* Microsoft Graph API: TeamworkTag.ReadWrite, Group.Read.All

Removes a Microsoft Teams Tag in a Team.

## SYNTAX

```powershell
Remove-PnPTeamsTag -Team <TeamsTeamPipeBind> -Identity <TeamsTagPipeBind> [-Force]
```

## DESCRIPTION

## EXAMPLES

### EXAMPLE 1
```powershell
Remove-PnPTeamsTag -Team 5beb63c5-0571-499e-94d5-3279fdd9b6b5 -Identity "ZmY1ZjdmMjctZDhiNy00MWRkLTk2ZDQtYzcyYmVhMWIwOGYxIyM3ZTVhNGRmZS1kNWNlLTRkOTAtODM4MC04ZDIxM2FkYzYzOGIjI3RiVlVpR01rcg=="
```
Removes the Tag with the specified Id from the Teams team.

## PARAMETERS

### -Force
Specifying the Force parameter will skip the confirmation question.

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

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -Identity
Specify the id of the Tag
```yaml
Type: TeamsTagPipeBind
Parameter Sets: (All)

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -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
```
## RELATED LINKS
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
83 changes: 83 additions & 0 deletions documentation/Set-PnPTeamsTag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
Module Name: PnP.PowerShell
title: Set-PnPTeamsTag
schema: 2.0.0
applicable: SharePoint Online
external help file: PnP.PowerShell.dll-Help.xml
online version: https://pnp.github.io/powershell/cmdlets/Set-PnPTeamsTag.html
---

# Set-PnPTeamsTag

## SYNOPSIS

**Required Permissions**

* Microsoft Graph API: TeamworkTag.ReadWrite, Group.Read.All

Sets the Microsoft Teams Tag in a Team.

## SYNTAX

```powershell
Set-PnPTeamsTag -Team <TeamsTeamPipeBind> -Identity <TeamsTagPipeBind> -DisplayName <String>
```

## DESCRIPTION

## EXAMPLES

### EXAMPLE 1
```powershell
Set-PnPTeamsTag -Team 5beb63c5-0571-499e-94d5-3279fdd9b6b5 -Identity "ZmY1ZjdmMjctZDhiNy00MWRkLTk2ZDQtYzcyYmVhMWIwOGYxIyM3ZTVhNGRmZS1kNWNlLTRkOTAtODM4MC04ZDIxM2FkYzYzOGIjI3RiVlVpR01rcg==" -DisplayName "Updated Tag"
```
Sets the Tag with the specified Id from the Teams team.

## PARAMETERS

### -DisplayName
The updated display name of the Teams tag

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

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -Identity
Specify the id of the Tag
```yaml
Type: TeamsTagPipeBind
Parameter Sets: (All)

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -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
```
## RELATED LINKS
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
38 changes: 38 additions & 0 deletions src/Commands/Base/PipeBinds/TeamsTagPipeBind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using PnP.PowerShell.Commands.Model.Teams;
using PnP.PowerShell.Commands.Utilities;
using System;
using System.Linq;
using System.Net.Http;

namespace PnP.PowerShell.Commands.Base.PipeBinds
{
public sealed class TeamsTagPipeBind
{
private readonly string _id;
public TeamsTagPipeBind()
{
}

public TeamsTagPipeBind(string input)
{
_id = input;
}

public TeamsTagPipeBind(TeamTag tag)
{
_id = tag.Id;
}


public TeamTag GetTag(PnPConnection connection, string accessToken, string groupId)
{
var tags = TeamsUtility.GetTagsAsync(accessToken, connection, groupId).GetAwaiter().GetResult();
if (tags != null && tags.Any())
{
return tags.FirstOrDefault(c => c.Id.Equals(_id, StringComparison.OrdinalIgnoreCase));
}
return null;
}

}
}
12 changes: 6 additions & 6 deletions src/Commands/Teams/GetTeamsTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ public class GetTeamsTag : PnPGraphCmdlet
public TeamsTeamPipeBind Team;

[Parameter(Mandatory = false, ValueFromPipeline = true)]
public string Identity;
public TeamsTagPipeBind Identity;

protected override void ExecuteCmdlet()
{
var group = Team.GetGroupId(Connection, AccessToken);
if (string.IsNullOrEmpty(group))
var groupId = Team.GetGroupId(Connection, AccessToken);
if (string.IsNullOrEmpty(groupId))
{
throw new PSArgumentException("Team not found");
}

if (!string.IsNullOrEmpty(Identity))
if (ParameterSpecified(nameof(Identity)))
{
var tags = TeamsUtility.GetTagsWithIdAsync(AccessToken, Connection, group, Identity).GetAwaiter().GetResult();
var tags = Identity.GetTag(Connection, AccessToken, groupId);
WriteObject(tags, false);
}
else
{
var tags = TeamsUtility.GetTagsAsync(AccessToken, Connection, group).GetAwaiter().GetResult();
var tags = TeamsUtility.GetTagsAsync(AccessToken, Connection, groupId).GetAwaiter().GetResult();
WriteObject(tags, true);
}
}
Expand Down
62 changes: 62 additions & 0 deletions src/Commands/Teams/RemoveTeamsTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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 PnP.PowerShell.Commands.Utilities.REST;
using System.Management.Automation;

namespace PnP.PowerShell.Commands.Graph
{
[Cmdlet(VerbsCommon.Remove, "PnPTeamsTag")]
[RequiredMinimalApiPermissions("TeamworkTag.ReadWrite")]
public class RemoveTeamsTag : PnPGraphCmdlet
{
[Parameter(Mandatory = true)]
public TeamsTeamPipeBind Team;

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

[Parameter(Mandatory = false)]
public SwitchParameter Force;

protected override void ExecuteCmdlet()
{
var groupId = Team.GetGroupId(Connection, AccessToken);
if (groupId != null)
{
var tag = Identity.GetTag(Connection, AccessToken, groupId);
if (tag != null)
{
if (Force || ShouldContinue("Do you want to remove this tag ?", Properties.Resources.Confirm))
{
var response = TeamsUtility.DeleteTagAsync(Connection, AccessToken, groupId, tag.Id).GetAwaiter().GetResult();
if (!response.IsSuccessStatusCode)
{
if (GraphHelper.TryGetGraphException(response, out GraphException ex))
{
if (ex.Error != null)
{
throw new PSInvalidOperationException(ex.Error.Message);
}
}
else
{
throw new PSInvalidOperationException("Tag remove failed");
}
}
}
}
else
{
throw new PSArgumentException("Tag not found");
}
}
else
{
throw new PSArgumentException("Team not found", nameof(Team));
}
}
}
}
46 changes: 46 additions & 0 deletions src/Commands/Teams/SetTeamsTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using PnP.PowerShell.Commands.Attributes;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Utilities;
using System.Management.Automation;

namespace PnP.PowerShell.Commands.Graph
{
[Cmdlet(VerbsCommon.Set, "PnPTeamsTag")]
[RequiredMinimalApiPermissions("TeamworkTag.ReadWrite")]
public class SetTeamsTag : PnPGraphCmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true)]
public TeamsTeamPipeBind Team;

[Parameter(Mandatory = true, ValueFromPipeline = true)]
public TeamsTagPipeBind Identity;

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

protected override void ExecuteCmdlet()
{
var groupId = Team.GetGroupId(Connection, AccessToken);
if (groupId != null)
{
var tag = Identity.GetTag(Connection, AccessToken, groupId);
if (tag != null)
{
if (ParameterSpecified(nameof(DisplayName)) && tag.DisplayName != DisplayName)
{
TeamsUtility.UpdateTagAsync(Connection, AccessToken, groupId, tag.Id, DisplayName).GetAwaiter().GetResult();
}
}
else
{
throw new PSArgumentException("Tag not found");
}
}
else
{
throw new PSArgumentException("Team not found");
}
}
}
}
11 changes: 11 additions & 0 deletions src/Commands/Utilities/TeamsUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,17 @@ public static async Task<TeamTag> GetTagsWithIdAsync(string accessToken, PnPConn
return tagInformation;
}

public static async Task UpdateTagAsync(PnPConnection connection, string accessToken, string groupId, string tagId, string displayName)
{
var body = new { displayName = displayName };
await GraphHelper.PatchAsync(connection, accessToken, $"v1.0/teams/{groupId}/tags/{tagId}", body);
}

public static async Task<HttpResponseMessage> DeleteTagAsync(PnPConnection connection, string accessToken, string groupId, string tagId)
{
return await GraphHelper.DeleteAsync(connection, $"v1.0/teams/{groupId}/tags/{tagId}", accessToken);
}


#endregion
}
Expand Down

0 comments on commit 885b82e

Please sign in to comment.