-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathGetTenantSite.cs
117 lines (104 loc) · 4.73 KB
/
GetTenantSite.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Linq;
using System.Management.Automation;
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Base;
using System.Collections.Generic;
using Microsoft.Online.SharePoint.TenantManagement;
using PnP.PowerShell.Commands.Base.PipeBinds;
namespace PnP.PowerShell.Commands
{
[Cmdlet(VerbsCommon.Get, "PnPTenantSite")]
public class GetTenantSite : PnPSharePointOnlineAdminCmdlet
{
private const string ParameterSet_BYURL = "By URL";
private const string ParameterSet_ALL = "All Sites";
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ParameterSetName = ParameterSet_BYURL)]
[Alias("Url")]
public SPOSitePipeBind Identity;
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_ALL)]
public string Template;
[Parameter(Mandatory = false)]
public SwitchParameter Detailed;
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_ALL)]
public SwitchParameter IncludeOneDriveSites;
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_ALL)]
public string Filter;
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_BYURL)]
public SwitchParameter DisableSharingForNonOwnersStatus;
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_ALL)]
public bool? GroupIdDefined;
protected override void ExecuteCmdlet()
{
AdminContext.ExecuteQueryRetry();
if (ParameterSpecified(nameof(Identity)))
{
SiteProperties siteProperties;
if(Identity.Id.HasValue)
{
siteProperties = Tenant.GetSitePropertiesById(Identity.Id.Value, Detailed, Connection.TenantAdminUrl);
if(siteProperties == null) return;
}
else
{
siteProperties = Tenant.GetSitePropertiesByUrl(Identity.Url, Detailed);
AdminContext.Load(siteProperties);
AdminContext.ExecuteQueryRetry();
}
Model.SPOSite site = null;
if (ParameterSpecified(nameof(DisableSharingForNonOwnersStatus)))
{
var office365Tenant = new Office365Tenant(AdminContext);
var clientResult = office365Tenant.IsSharingDisabledForNonOwnersOfSite(Identity.Url);
AdminContext.ExecuteQueryRetry();
site = new Model.SPOSite(siteProperties, clientResult.Value);
}
else
{
site = new Model.SPOSite(siteProperties, null);
}
WriteObject(site, true);
}
else
{
SPOSitePropertiesEnumerableFilter filter = new SPOSitePropertiesEnumerableFilter()
{
IncludePersonalSite = IncludeOneDriveSites.IsPresent ? PersonalSiteFilter.Include : PersonalSiteFilter.UseServerDefault,
IncludeDetail = Detailed,
Template = Template,
Filter = Filter,
};
if (AdminContext.ServerVersion >= new Version(16, 0, 7708, 1200))
{
if (ParameterSpecified(nameof(GroupIdDefined)))
{
filter.GroupIdDefined = GroupIdDefined.Value == true ? 1 : 2;
}
}
else if (ParameterSpecified(nameof(GroupIdDefined)))
{
throw new PSArgumentException("Filtering by Group Id is not yet available for this tenant.");
}
SPOSitePropertiesEnumerable sitesList = null;
var sites = new List<SiteProperties>();
do
{
sitesList = Tenant.GetSitePropertiesFromSharePointByFilters(filter);
Tenant.Context.Load(sitesList);
Tenant.Context.ExecuteQueryRetry();
sites.AddRange(sitesList.ToList());
filter.StartIndex = sitesList.NextStartIndexFromSharePoint;
} while (!string.IsNullOrWhiteSpace(sitesList.NextStartIndexFromSharePoint));
if (Template != null)
{
WriteObject(sites.Where(t => t.Template.ToLower() == Template.ToLower()).OrderBy(x => x.Url).Select(s => new Model.SPOSite(s, null)), true);
}
else
{
WriteObject(sites.OrderBy(x => x.Url).Select(s => new Model.SPOSite(s, null)), true);
}
}
}
}
}