From 384617c83d1c4ba8181457b40722e758d6f32609 Mon Sep 17 00:00:00 2001 From: Gautam Sheth Date: Tue, 4 Oct 2022 22:09:35 +0300 Subject: [PATCH 1/2] Feature: added Set/Remove-PnPTeamsTag cmdlet --- documentation/Remove-PnPTeamsTag.md | 83 +++++++++++++++++++ documentation/Set-PnPTeamsTag.md | 83 +++++++++++++++++++ .../Base/PipeBinds/TeamsTagPipeBind.cs | 38 +++++++++ src/Commands/Teams/GetTeamsTag.cs | 12 +-- src/Commands/Teams/RemoveTeamsTag.cs | 62 ++++++++++++++ src/Commands/Teams/SetTeamsTag.cs | 46 ++++++++++ src/Commands/Utilities/TeamsUtility.cs | 11 +++ 7 files changed, 329 insertions(+), 6 deletions(-) create mode 100644 documentation/Remove-PnPTeamsTag.md create mode 100644 documentation/Set-PnPTeamsTag.md create mode 100644 src/Commands/Base/PipeBinds/TeamsTagPipeBind.cs create mode 100644 src/Commands/Teams/RemoveTeamsTag.cs create mode 100644 src/Commands/Teams/SetTeamsTag.cs diff --git a/documentation/Remove-PnPTeamsTag.md b/documentation/Remove-PnPTeamsTag.md new file mode 100644 index 000000000..256d6c75b --- /dev/null +++ b/documentation/Remove-PnPTeamsTag.md @@ -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 -Identity [-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) + diff --git a/documentation/Set-PnPTeamsTag.md b/documentation/Set-PnPTeamsTag.md new file mode 100644 index 000000000..31e708b1c --- /dev/null +++ b/documentation/Set-PnPTeamsTag.md @@ -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 -Identity -DisplayName +``` + +## 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) + diff --git a/src/Commands/Base/PipeBinds/TeamsTagPipeBind.cs b/src/Commands/Base/PipeBinds/TeamsTagPipeBind.cs new file mode 100644 index 000000000..643e9e8f7 --- /dev/null +++ b/src/Commands/Base/PipeBinds/TeamsTagPipeBind.cs @@ -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; + } + + } +} diff --git a/src/Commands/Teams/GetTeamsTag.cs b/src/Commands/Teams/GetTeamsTag.cs index e9dbd38ee..e99b385d9 100644 --- a/src/Commands/Teams/GetTeamsTag.cs +++ b/src/Commands/Teams/GetTeamsTag.cs @@ -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); } } diff --git a/src/Commands/Teams/RemoveTeamsTag.cs b/src/Commands/Teams/RemoveTeamsTag.cs new file mode 100644 index 000000000..97e22a04b --- /dev/null +++ b/src/Commands/Teams/RemoveTeamsTag.cs @@ -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)); + } + } + } +} \ No newline at end of file diff --git a/src/Commands/Teams/SetTeamsTag.cs b/src/Commands/Teams/SetTeamsTag.cs new file mode 100644 index 000000000..09f4ad423 --- /dev/null +++ b/src/Commands/Teams/SetTeamsTag.cs @@ -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"); + } + } + } +} \ No newline at end of file diff --git a/src/Commands/Utilities/TeamsUtility.cs b/src/Commands/Utilities/TeamsUtility.cs index cb38af34e..e3a5d3520 100644 --- a/src/Commands/Utilities/TeamsUtility.cs +++ b/src/Commands/Utilities/TeamsUtility.cs @@ -948,6 +948,17 @@ public static async Task 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 DeleteTagAsync(PnPConnection connection, string accessToken, string groupId, string tagId) + { + return await GraphHelper.DeleteAsync(connection, $"v1.0/teams/{groupId}/tags/{tagId}", accessToken); + } + #endregion } From d41925705a5c68d7e42f3b599b413fa5271dd4be Mon Sep 17 00:00:00 2001 From: Gautam Sheth Date: Tue, 4 Oct 2022 22:13:34 +0300 Subject: [PATCH 2/2] Added changelog + updated docs --- CHANGELOG.md | 2 ++ documentation/Get-PnPTeamsTag.md | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7355f007..be31b9e11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/documentation/Get-PnPTeamsTag.md b/documentation/Get-PnPTeamsTag.md index f3c6f9f6e..e80d7207a 100644 --- a/documentation/Get-PnPTeamsTag.md +++ b/documentation/Get-PnPTeamsTag.md @@ -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