Skip to content

Commit 71ba889

Browse files
gautamdshethGautam Sheth
and
Gautam Sheth
authored
Add -User parameter to Get-PnPTeamsTeam cmdlet and enhance team model with summary details (#4894)
Co-authored-by: Gautam Sheth <[email protected]>
1 parent 42670cb commit 71ba889

File tree

11 files changed

+115
-14
lines changed

11 files changed

+115
-14
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
1111

1212
### Added
1313
- Added `-NewFileName` parameter to `Convert-PnPFile` cmdlet to choose custom output file name.
14+
- Added `-User` parameter to `Get-PnPTeamsTeam` cmdlet to allow fetching list of teams a user has access to.
1415

1516
### Fixed
1617
- Fix `Set-PnPView -Aggregations` parameter not showing aggregations in SharePoint online. [#4868](https://github.com/pnp/powershell/pull/4868)

documentation/Get-PnPTeamsTeam.md

+22-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Gets one Microsoft Teams Team or a list of Teams.
2020
## SYNTAX
2121

2222
```powershell
23-
Get-PnPTeamsTeam [-Identity <TeamsTeamPipeBind>] [-Filter <String>]
23+
Get-PnPTeamsTeam [-Identity <TeamsTeamPipeBind>] [-Filter <String>] [-User <AzureADUserPipeBind>]
2424
```
2525

2626
## DESCRIPTION
@@ -64,6 +64,13 @@ Get-PnPTeamsTeam -Filter "startswith(description, 'contoso')"
6464

6565
Retrieves all Microsoft Teams instances with Description starting with "contoso". This example demonstrates using Advanced Query capabilities (see: https://learn.microsoft.com/en-us/graph/aad-advanced-queries?tabs=http#group-properties).
6666

67+
### EXAMPLE 6
68+
```powershell
69+
Get-PnPTeamsTeam -User "[email protected]"
70+
```
71+
72+
Retrieves all Microsoft Teams instances which the specified user has access to.
73+
6774
## PARAMETERS
6875

6976
### -Identity
@@ -94,8 +101,20 @@ Accept pipeline input: False
94101
Accept wildcard characters: False
95102
```
96103
104+
### -User
105+
Specify the name of the user to fetch the list of teams user as access to.
97106
98-
## RELATED LINKS
107+
```yaml
108+
Type: AzureADUserPipeBind
109+
Parameter Sets: User
110+
111+
Required: False
112+
Position: Named
113+
Default value: None
114+
Accept pipeline input: False
115+
Accept wildcard characters: False
116+
```
99117
100-
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
118+
## RELATED LINKS
101119
120+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)

src/Commands/Model/Teams/Team.cs

+42
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,21 @@ public class Team
101101
/// </summary>
102102
public DateTimeOffset? CreatedDateTime { get; set; }
103103

104+
/// <summary>
105+
/// The summary for the Team
106+
/// </summary>
107+
public TeamSummary Summary { get; set; }
108+
109+
/// <summary>
110+
/// Tenant ID of the team
111+
/// </summary>
112+
public string TenantId { get; set; }
113+
114+
/// <summary>
115+
/// Whether the Team membership is limited to owners
116+
/// </summary>
117+
public bool? IsMembershipLimitedToOwners { get; set; }
118+
104119
#endregion
105120
}
106121

@@ -130,4 +145,31 @@ public enum TeamSpecialization
130145
/// </summary>
131146
EducationStaff
132147
}
148+
149+
150+
public class JoinedTeam
151+
{
152+
#region Public Members
153+
154+
public Guid Id { get; set; }
155+
156+
public string DisplayName { get; set; }
157+
158+
/// <summary>
159+
/// Declares whether the Team is archived or not
160+
/// </summary>
161+
public bool? IsArchived { get; set; }
162+
163+
/// <summary>
164+
/// Declares the description for the team
165+
/// </summary>
166+
public string Description { get; set; }
167+
168+
/// <summary>
169+
/// Tenant ID of the team
170+
/// </summary>
171+
public Guid TenantId { get; set; }
172+
173+
#endregion
174+
}
133175
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace PnP.PowerShell.Commands.Model.Teams
2+
{
3+
public partial class TeamSummary
4+
{
5+
#region Public Members
6+
7+
public int GuestsCount { get; set; }
8+
9+
public int MembersCount { get; set; }
10+
11+
public int OwnersCount { get; set; }
12+
13+
#endregion
14+
}
15+
}

src/Commands/Teams/GetTeamsTeam.cs

+18-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class GetTeamsTeam : PnPGraphCmdlet
1313
{
1414
private const string ParameterSet_Identity = "Identity";
1515
private const string ParameterSet_Filter = "Filter";
16+
private const string ParameterSet_User = "User";
1617

1718
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_Identity)]
1819
public TeamsTeamPipeBind Identity;
@@ -25,6 +26,9 @@ public class GetTeamsTeam : PnPGraphCmdlet
2526
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_Filter)]
2627
public string Filter = null;
2728

29+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_User)]
30+
public AzureADUserPipeBind User;
31+
2832
protected override void ExecuteCmdlet()
2933
{
3034
if (ParameterSpecified(nameof(Identity)))
@@ -41,7 +45,20 @@ protected override void ExecuteCmdlet()
4145
}
4246
else
4347
{
44-
WriteObject(TeamsUtility.GetTeamUsingFilter(GraphRequestHelper, Filter));
48+
if (ParameterSpecified(nameof(User)))
49+
{
50+
var user = User.GetUser(AccessToken, Connection.AzureEnvironment);
51+
if (user == null)
52+
{
53+
LogWarning("Provided user not found");
54+
return;
55+
}
56+
WriteObject(TeamsUtility.GetJoinedTeams(GraphRequestHelper, user.Id.Value));
57+
}
58+
else
59+
{
60+
WriteObject(TeamsUtility.GetTeamUsingFilter(GraphRequestHelper, Filter));
61+
}
4562
}
4663
}
4764
}

src/Commands/ToDo/GetTodoList.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected override void ExecuteCmdlet()
3636
LogWarning("Provided user not found");
3737
return;
3838
}
39-
url = $"/v1.0/users/{user.Id}/todo/lists";
39+
url = $"/v1.0/users/{user.Id.Value}/todo/lists";
4040
}
4141
if (ParameterSpecified(nameof(Identity)))
4242
{

src/Commands/ToDo/NewTodoList.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected override void ExecuteCmdlet()
3636
LogWarning("Provided user not found");
3737
return;
3838
}
39-
url = $"/v1.0/users/{user.Id}/todo/lists";
39+
url = $"/v1.0/users/{user.Id.Value}/todo/lists";
4040
}
4141

4242
var stringContent = new StringContent($"{{'displayName':'{DisplayName}'}}");

src/Commands/ToDo/RemoveTodoList.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected override void ExecuteCmdlet()
2828
LogWarning("Provided user not found");
2929
return;
3030
}
31-
url = $"/v1.0/users/{user.Id}/todo/lists/{Identity}";
31+
url = $"/v1.0/users/{user.Id.Value}/todo/lists/{Identity}";
3232
}
3333

3434
var graphResult = GraphRequestHelper.Delete(url);

src/Commands/ToDo/UpdateTodoList.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected override void ExecuteCmdlet()
3232
LogWarning("Provided user not found");
3333
return;
3434
}
35-
url = $"/v1.0/users/{user.Id}/todo/lists/{Identity}";
35+
url = $"/v1.0/users/{user.Id.Value}/todo/lists/{Identity}";
3636
}
3737

3838
var stringContent = new StringContent($"{{'displayName':'{DisplayName}'}}");

src/Commands/Utilities/REST/ApiRequestHelper.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Management.Automation;
88
using System.Net;
99
using System.Net.Http;
10-
using System.Text;
1110
using System.Text.Json;
1211
using System.Text.Json.Serialization;
1312
using System.Threading;
@@ -215,7 +214,7 @@ public IEnumerable<T> GetResultCollection<T>(string url, bool camlCasePolicy = t
215214
var results = new List<T>();
216215
var request = Get<RestResultCollection<T>>(url, camlCasePolicy, propertyNameCaseInsensitive, additionalHeaders);
217216

218-
if (request.Items.Any())
217+
if (request != null && request.Items.Any())
219218
{
220219
results.AddRange(request.Items);
221220
while (!string.IsNullOrEmpty(request.NextLink))
@@ -264,7 +263,7 @@ public T Get<T>(string url, bool camlCasePolicy = true, bool propertyNameCaseIns
264263
}
265264
catch (Exception e)
266265
{
267-
LogError($"Failed to parse response from server. Error message: '{e.Message}'. Received content: '{stringContent}'. Model type to parse it to: '{typeof(T)}'.");
266+
LogError($"Failed to parse response from server. Error message: '{e.Message}'. Model type to parse it to: '{typeof(T)}'.");
268267
//Cmdlet.LogWarning($"Failed to parse response from server. Error message: '{e.Message}'. Received content: '{stringContent}'. Model type to parse it to: '{typeof(T)}'.");
269268
return default;
270269
}
@@ -311,7 +310,7 @@ public T Delete<T>(Cmdlet cmdlet, PnPConnection connection, string url, bool cam
311310

312311
#region PATCH
313312
public T Patch<T>(string url, T content, IDictionary<string, string> additionalHeaders = null, bool camlCasePolicy = true)
314-
{
313+
{
315314
var serializerSettings = new JsonSerializerOptions() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull };
316315
if (camlCasePolicy)
317316
{
@@ -387,7 +386,7 @@ private void PostInternal(string url, HttpContent content, IDictionary<string, s
387386
{
388387
var message = GetMessage(url, HttpMethod.Post, content, additionalHeaders);
389388
GetResponseMessage(message);
390-
}
389+
}
391390

392391
private T PostInternal<T>(string url, HttpContent content, IDictionary<string, string> additionalHeaders = null, bool propertyNameCaseInsensitive = false)
393392
{
@@ -455,7 +454,7 @@ public HttpResponseMessage Put2(string url, HttpContent content, string accessTo
455454
{
456455
var message = GetMessage2(url, accessToken, HttpMethod.Put, content, additionalHeaders);
457456
return GetResponseMessage2(message);
458-
}
457+
}
459458

460459
#endregion
461460

src/Commands/Utilities/TeamsUtility.cs

+8
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,14 @@ public static IEnumerable<DeletedTeam> GetDeletedTeam(ApiRequestHelper requestHe
455455
}
456456
return null;
457457
}
458+
459+
public static List<JoinedTeam> GetJoinedTeams(ApiRequestHelper requestHelper, Guid userId)
460+
{
461+
string requestUrl = $"v1.0/users/{userId}/joinedTeams";
462+
var collection = requestHelper.GetResultCollection<JoinedTeam>(requestUrl);
463+
return collection.ToList();
464+
465+
}
458466
#endregion
459467

460468
#region Users

0 commit comments

Comments
 (0)