Example #1
0
        protected override void ExecuteCmdlet()
        {
            if (SPOnlineConnection.CurrentConnection.ConnectionType == ConnectionType.OnPrem)
            {
                WriteObject(ClientContext.Site);
            }
            else
            {
                if (!string.IsNullOrEmpty(Url))
                {
                    var list = Tenant.GetSitePropertiesByUrl(Url, Detailed);
                    list.Context.Load(list);
                    list.Context.ExecuteQueryRetry();
                    WriteObject(list, true);
                }
                else
                {
                    SPOSitePropertiesEnumerableFilter filter = null;
                    if (IncludeOneDriveSites || WebTemplate != null || Filter != null)
                    {
                        filter = new SPOSitePropertiesEnumerableFilter()
                        {
                            IncludePersonalSite = IncludeOneDriveSites.IsPresent ? PersonalSiteFilter.Include : PersonalSiteFilter.UseServerDefault,
                            StartIndex          = "0",
                            IncludeDetail       = Detailed,
                            Template            = WebTemplate,
                            Filter = Filter,
                        };
                    }

                    SPOSitePropertiesEnumerable list = null;
                    var sites = new List <SiteProperties>();
                    do
                    {
                        list = filter == null?Tenant.GetSiteProperties(list?.NextStartIndex ?? 0, Detailed) : Tenant.GetSitePropertiesFromSharePointByFilters(filter);

                        Tenant.Context.Load(list);
                        Tenant.Context.ExecuteQueryRetry();
                        sites.AddRange(list.ToList());
                        if (filter != null)
                        {
                            filter.StartIndex = list.NextStartIndex.ToString();
                        }
                    } while (list.NextStartIndex > 0);

                    if (Template != null)
                    {
                        WriteObject(sites.Where(t => t.Template == Template).OrderBy(x => x.Url), true);
                    }
                    else
                    {
                        WriteObject(sites.OrderBy(x => x.Url), true);
                    }
                }
            }
        }
Example #2
0
        protected override void ExecuteCmdlet()
        {
            if (!string.IsNullOrEmpty(Url))
            {
                var list = Tenant.GetSitePropertiesByUrl(Url, Detailed);
                list.Context.Load(list);
                list.Context.ExecuteQueryRetry();
                WriteObject(list, true);
            }
            else
            {
#if !ONPREMISES
                SPOSitePropertiesEnumerableFilter filter = new SPOSitePropertiesEnumerableFilter()
                {
                    IncludePersonalSite = IncludeOneDriveSites.IsPresent ? PersonalSiteFilter.Include : PersonalSiteFilter.UseServerDefault,
                    IncludeDetail       = Detailed,
#pragma warning disable CS0618 // Type or member is obsolete
                    Template = Template ?? WebTemplate,
#pragma warning restore CS0618 // Type or member is obsolete
                    Filter = Filter,
                };
#endif
                SPOSitePropertiesEnumerable sitesList = null;
                var sites = new List <SiteProperties>();
#if !ONPREMISES
                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));
#else
                sitesList = Tenant.GetSiteProperties(0, Detailed);
                Tenant.Context.Load(sitesList);
                Tenant.Context.ExecuteQueryRetry();
                sites.AddRange(sitesList.ToList());
#endif

#if !ONPREMISES
                if (Template != null)
                {
                    WriteObject(sites.Where(t => t.Template == Template).OrderBy(x => x.Url), true);
                }
                else
                {
                    WriteObject(sites.OrderBy(x => x.Url), true);
                }
#else
                WriteObject(sites.OrderBy(x => x.Url), true);
#endif
            }
        }
        protected override void ExecuteCmdlet()
        {
            if (!string.IsNullOrEmpty(Url))
            {
                var siteProperties = Tenant.GetSitePropertiesByUrl(Url, Detailed);
                ClientContext.Load(siteProperties);
                ClientContext.ExecuteQueryRetry();
                Model.SPOSite site = null;
                if (ParameterSpecified(nameof(DisableSharingForNonOwnersStatus)))
                {
                    var office365Tenant = new Office365Tenant(ClientContext);
                    var clientResult    = office365Tenant.IsSharingDisabledForNonOwnersOfSite(Url);
                    ClientContext.ExecuteQuery();
                    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,
#pragma warning disable CS0618 // Type or member is obsolete
                    Template = Template,
#pragma warning restore CS0618 // Type or member is obsolete
                    Filter = Filter,
                };
                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 == Template).OrderBy(x => x.Url), true);
                }
                else
                {
                    WriteObject(sites.OrderBy(x => x.Url), true);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Returns all site collections in the current Tenant based on a startIndex. IncludeDetail adds additional properties to the SPSite object. EndIndex is the maximum number based on chunkcs of 300.
        /// </summary>
        /// <param name="tenant">Tenant object to operate against</param>
        /// <param name="startIndex">Start getting site collections from this index. Defaults to 0</param>
        /// <param name="endIndex">The index of the last site. Defaults to 100.000</param>
        /// <param name="includeDetail">Option to return a limited set of data</param>
        /// <returns>An IList of SiteEntity objects</returns>
        public static IList <SiteEntity> GetSiteCollections(this Tenant tenant, int startIndex = 0, int endIndex = 500000, bool includeDetail = true, bool includeOD4BSites = false)
        {
            var sites = new List <SiteEntity>();
            SPOSitePropertiesEnumerable props = null;

            while (props == null || !string.IsNullOrEmpty(props.NextStartIndexFromSharePoint))
            {
                // approach to be used as of Feb 2017
                SPOSitePropertiesEnumerableFilter filter = new SPOSitePropertiesEnumerableFilter()
                {
                    IncludePersonalSite = includeOD4BSites ? PersonalSiteFilter.Include : PersonalSiteFilter.UseServerDefault,
                    StartIndex          = props == null ? null : props.NextStartIndexFromSharePoint,
                    IncludeDetail       = includeDetail
                };
                props = tenant.GetSitePropertiesFromSharePointByFilters(filter);

                // Previous approach, being replaced by GetSitePropertiesFromSharePointByFilters which also allows to fetch OD4B sites
                //props = tenant.GetSitePropertiesFromSharePoint(props == null ? null : props.NextStartIndexFromSharePoint, includeDetail);
                tenant.Context.Load(props);
                tenant.Context.ExecuteQueryRetry();

                foreach (var prop in props)
                {
                    var siteEntity = new SiteEntity();
                    siteEntity.Lcid                = prop.Lcid;
                    siteEntity.SiteOwnerLogin      = prop.Owner;
                    siteEntity.StorageMaximumLevel = prop.StorageMaximumLevel;
                    siteEntity.StorageWarningLevel = prop.StorageWarningLevel;
                    siteEntity.Template            = prop.Template;
                    siteEntity.TimeZoneId          = prop.TimeZoneId;
                    siteEntity.Title               = prop.Title;
                    siteEntity.Url = prop.Url;
                    siteEntity.UserCodeMaximumLevel    = prop.UserCodeMaximumLevel;
                    siteEntity.UserCodeWarningLevel    = prop.UserCodeWarningLevel;
                    siteEntity.CurrentResourceUsage    = prop.CurrentResourceUsage;
                    siteEntity.LastContentModifiedDate = prop.LastContentModifiedDate;
                    siteEntity.StorageUsage            = prop.StorageUsage;
                    siteEntity.WebsCount = prop.WebsCount;
                    SiteLockState lockState;
                    if (Enum.TryParse(prop.LockState, out lockState))
                    {
                        siteEntity.LockState = lockState;
                    }
                    sites.Add(siteEntity);
                }
                Console.WriteLine(props.NextStartIndexFromSharePoint);
                Console.WriteLine($"Added {props.Count} sites, total sites to scan is now {sites.Count}");
            }

            return(sites);
        }
Example #5
0
        /// <summary>
        /// Get the site collections for ONE location in a Multi-Geo tenant
        /// </summary>
        /// <param name="tenant">Tenant instance</param>
        /// <param name="startIndex">Start from </param>
        /// <param name="endIndex">End at</param>
        /// <param name="includeDetail">Also retrieve details</param>
        /// <param name="includeOD4BSites">Also retrieve OD4B sites</param>
        /// <param name="geoLocation">Geo location for which we're retrieving site collections</param>
        /// <returns></returns>
        public IList <GeoSites> GetSiteCollectionsInGeo(Tenant tenant, int startIndex = 0, int endIndex = 500000, bool includeDetail = true, bool includeOD4BSites = false, string geoLocation = null)
        {
            var sites = new List <GeoSites>();
            SPOSitePropertiesEnumerable props = null;

            while (props == null || props.NextStartIndexFromSharePoint != null)
            {
                // approach to be used as of Feb 2017
                SPOSitePropertiesEnumerableFilter filter = new SPOSitePropertiesEnumerableFilter()
                {
                    IncludePersonalSite = includeOD4BSites ? PersonalSiteFilter.Include : PersonalSiteFilter.UseServerDefault,
                    StartIndex          = props == null ? null : props.NextStartIndexFromSharePoint,
                    IncludeDetail       = includeDetail
                };
                props = tenant.GetSitePropertiesFromSharePointByFilters(filter);

                tenant.Context.Load(props);
                tenant.Context.ExecuteQuery();

                foreach (var prop in props)
                {
                    var siteEntity = new GeoSites()
                    {
                        GeoLocation         = geoLocation,
                        Lcid                = prop.Lcid,
                        SiteOwnerLogin      = prop.Owner,
                        StorageMaximumLevel = prop.StorageMaximumLevel,
                        StorageWarningLevel = prop.StorageWarningLevel,
                        Template            = prop.Template,
                        TimeZoneId          = prop.TimeZoneId,
                        Title               = prop.Title,
                        Url = prop.Url,
                        UserCodeMaximumLevel    = prop.UserCodeMaximumLevel,
                        UserCodeWarningLevel    = prop.UserCodeWarningLevel,
                        CurrentResourceUsage    = prop.CurrentResourceUsage,
                        LastContentModifiedDate = prop.LastContentModifiedDate,
                        StorageUsage            = prop.StorageUsage,
                        WebsCount = prop.WebsCount
                    };
                    SiteLockState lockState;
                    if (Enum.TryParse(prop.LockState, out lockState))
                    {
                        siteEntity.LockState = lockState;
                    }
                    sites.Add(siteEntity);
                }
            }

            return(sites);
        }
Example #6
0
        /// <summary>
        /// Returns all site collections in the current Tenant based on a startIndex. IncludeDetail adds additional properties to the SPSite object.
        /// </summary>
        /// <param name="tenant">Tenant object to operate against</param>
        /// <param name="startIndex">Not relevant anymore</param>
        /// <param name="endIndex">Not relevant anymore</param>
        /// <param name="includeDetail">Option to return a limited set of data</param>
        /// <returns>An IList of SiteEntity objects</returns>
        public static IList <SiteEntity> GetSiteCollections(this Tenant tenant, int startIndex = 0, int endIndex = 500000, bool includeDetail = true, bool includeOD4BSites = false)
        {
            var sites = new List <SiteEntity>();
            SPOSitePropertiesEnumerable props = null;

            while (props == null || props.NextStartIndexFromSharePoint != null)
            //while (props == null || props.NextStartIndex > -1)
            {
                SPOSitePropertiesEnumerableFilter filter = new SPOSitePropertiesEnumerableFilter()
                {
                    IncludePersonalSite = includeOD4BSites ? PersonalSiteFilter.Include : PersonalSiteFilter.UseServerDefault,
                    StartIndex          = props == null ? null : props.NextStartIndexFromSharePoint,
                    IncludeDetail       = includeDetail
                };

                //props = tenant.GetSitePropertiesFromSharePointByFilters(filter);
                props = tenant.GetSitePropertiesFromSharePoint(props == null?null:props.NextStartIndexFromSharePoint, includeDetail);
                //props = tenant.GetSiteProperties(props == null ? 0 : props.NextStartIndex, includeDetail);
                tenant.Context.Load(props);
                tenant.Context.ExecuteQueryRetry();

                foreach (var prop in props)
                {
                    var siteEntity = new SiteEntity();
                    siteEntity.Lcid                = prop.Lcid;
                    siteEntity.SiteOwnerLogin      = prop.Owner;
                    siteEntity.StorageMaximumLevel = prop.StorageMaximumLevel;
                    siteEntity.StorageWarningLevel = prop.StorageWarningLevel;
                    siteEntity.Template            = prop.Template;
                    siteEntity.TimeZoneId          = prop.TimeZoneId;
                    siteEntity.Title               = prop.Title;
                    siteEntity.Url = prop.Url;
                    siteEntity.UserCodeMaximumLevel    = prop.UserCodeMaximumLevel;
                    siteEntity.UserCodeWarningLevel    = prop.UserCodeWarningLevel;
                    siteEntity.CurrentResourceUsage    = prop.CurrentResourceUsage;
                    siteEntity.LastContentModifiedDate = prop.LastContentModifiedDate;
                    siteEntity.StorageUsage            = prop.StorageUsage;
                    siteEntity.WebsCount = prop.WebsCount;
                    var lockState = SiteLockState.Unlock;
                    if (Enum.TryParse(prop.LockState, out lockState))
                    {
                        siteEntity.LockState = lockState;
                    }
                    sites.Add(siteEntity);
                }
            }

            return(sites);
        }
Example #7
0
        public static void Test(ClientContext context)
        {
            Tenant tenant = new Tenant(context);

            SPOSitePropertiesEnumerableFilter filter = new SPOSitePropertiesEnumerableFilter
            {
                IncludeDetail       = true,
                IncludePersonalSite = PersonalSiteFilter.Include,
                Template            = "SPSPERS"
            };
            var sitePropertyEnum = tenant.GetSitePropertiesFromSharePointByFilters(filter);

            context.Load(sitePropertyEnum);
            context.ExecuteQuery();
        }
Example #8
0
        protected override void ExecuteCmdlet()
        {
            if (SPOnlineConnection.CurrentConnection.ConnectionType == ConnectionType.OnPrem)
            {
                WriteObject(ClientContext.Site);
            }
            else
            {
                if (!string.IsNullOrEmpty(Url))
                {
                    var list = Tenant.GetSitePropertiesByUrl(Url, Detailed);
                    list.Context.Load(list);
                    list.Context.ExecuteQueryRetry();
                    WriteObject(list, true);
                }
                else
                {
                    SPOSitePropertiesEnumerableFilter filter = new SPOSitePropertiesEnumerableFilter()
                    {
                        IncludePersonalSite = IncludeOneDriveSites.IsPresent ? PersonalSiteFilter.Include : PersonalSiteFilter.UseServerDefault,
                        StartIndex          = null,
                        IncludeDetail       = true,
                        Template            = WebTemplate,
                        Filter = Filter,
                    };

                    var list = Tenant.GetSitePropertiesFromSharePointByFilters(filter);

                    Tenant.Context.Load(list);
                    Tenant.Context.ExecuteQueryRetry();
                    var siteProperties = list.ToList();

                    if (Template != null)
                    {
                        WriteObject(siteProperties.Where(t => t.Template == Template).OrderBy(x => x.Url), true);
                    }
                    else
                    {
                        WriteObject(siteProperties.OrderBy(x => x.Url), true);
                    }
                }
            }
        }
Example #9
0
        private static List <string> FilterSiteCollections(AddMultipleItemsOptions option)
        {
            logger.Info($"Start searh site collections with {option.KeyWord}");
            var siteUrls      = new List <string>();
            var tenantContext = tokenHelper.GetClientContextForServiceAccount(option.AdminUrl, option.UserName, option.Password);

            tenantContext.RequestTimeout = 6000 * 1000;
            Tenant tenant = new Tenant(tenantContext);
            SPOSitePropertiesEnumerableFilter filter = new SPOSitePropertiesEnumerableFilter
            {
                //IncludeDetail = true,
                IncludePersonalSite = PersonalSiteFilter.Exclude,
                Template            = "STS",
                Filter = $"Url -like '{option.KeyWord}'",
            };

            string tempIndex = null;

            do
            {
                filter.StartIndex = tempIndex;
                var sitePropertyEnum = tenant.GetSitePropertiesFromSharePointByFilters(filter);
                tenantContext.Load(sitePropertyEnum);
                tenantContext.ExecuteQuery();
                foreach (SiteProperties siteProperty in sitePropertyEnum)
                {
                    siteUrls.Add(siteProperty.Url.TrimEnd('/'));
                }
                tempIndex = sitePropertyEnum.NextStartIndexFromSharePoint;
                if (option.LimiteCount > 0 && siteUrls.Count >= option.LimiteCount)
                {
                    logger.Info($"stop search site collections with {option.KeyWord}, beacuse site collection count exceed {option.LimiteCount}");
                    break;
                }
            }while (tempIndex != null);
            logger.Info($"Finish searh site collections with {option.KeyWord}, site collection count: {siteUrls.Count}, limite count: {option.LimiteCount}");

            return(option.LimiteCount > 0 ? siteUrls.Take(option.LimiteCount).ToList() : siteUrls);
        }
        /// <summary>
        /// Returns all site collections in the current Tenant based on a startIndex. IncludeDetail adds additional properties to the SPSite object. 
        /// </summary>
        /// <param name="tenant">Tenant object to operate against</param>
        /// <param name="startIndex">Not relevant anymore</param>
        /// <param name="endIndex">Not relevant anymore</param>
        /// <param name="includeDetail">Option to return a limited set of data</param>
        /// <returns>An IList of SiteEntity objects</returns>
        public static IList<SiteEntity> GetSiteCollections(this Tenant tenant, int startIndex = 0, int endIndex = 500000, bool includeDetail = true, bool includeOD4BSites = false)
        {
            var sites = new List<SiteEntity>();
            SPOSitePropertiesEnumerable props = null;

            while (props == null || props.NextStartIndexFromSharePoint != null)
            //while (props == null || props.NextStartIndex > -1)
            {

                SPOSitePropertiesEnumerableFilter filter = new SPOSitePropertiesEnumerableFilter()
                {
                    IncludePersonalSite = includeOD4BSites ? PersonalSiteFilter.Include : PersonalSiteFilter.UseServerDefault,
                    StartIndex = props == null ? null : props.NextStartIndexFromSharePoint,
                    IncludeDetail = includeDetail
                };

                //props = tenant.GetSitePropertiesFromSharePointByFilters(filter);
                props = tenant.GetSitePropertiesFromSharePoint(props == null?null:props.NextStartIndexFromSharePoint, includeDetail);
                //props = tenant.GetSiteProperties(props == null ? 0 : props.NextStartIndex, includeDetail);
                tenant.Context.Load(props);
                tenant.Context.ExecuteQueryRetry();

                foreach (var prop in props)
                {
                    var siteEntity = new SiteEntity();
                    siteEntity.Lcid = prop.Lcid;
                    siteEntity.SiteOwnerLogin = prop.Owner;
                    siteEntity.StorageMaximumLevel = prop.StorageMaximumLevel;
                    siteEntity.StorageWarningLevel = prop.StorageWarningLevel;
                    siteEntity.Template = prop.Template;
                    siteEntity.TimeZoneId = prop.TimeZoneId;
                    siteEntity.Title = prop.Title;
                    siteEntity.Url = prop.Url;
                    siteEntity.UserCodeMaximumLevel = prop.UserCodeMaximumLevel;
                    siteEntity.UserCodeWarningLevel = prop.UserCodeWarningLevel;
                    siteEntity.CurrentResourceUsage = prop.CurrentResourceUsage;
                    siteEntity.LastContentModifiedDate = prop.LastContentModifiedDate;
                    siteEntity.StorageUsage = prop.StorageUsage;
                    siteEntity.WebsCount = prop.WebsCount;
                    var lockState = SiteLockState.Unlock;
                    if (Enum.TryParse(prop.LockState, out lockState)) {
                        siteEntity.LockState = lockState;
                    }
                    sites.Add(siteEntity);
                }
            }

            return sites;
        }
Example #11
0
        protected override void ExecuteCmdlet()
        {
            ClientContext.ExecuteQueryRetry();
            if (ParameterSpecified(nameof(Identity)))
            {
                var siteProperties = Tenant.GetSitePropertiesByUrl(Identity.Url, Detailed);
                ClientContext.Load(siteProperties);
                ClientContext.ExecuteQueryRetry();
                Model.SPOSite site = null;
                if (ParameterSpecified(nameof(DisableSharingForNonOwnersStatus)))
                {
                    var office365Tenant = new Office365Tenant(ClientContext);
                    var clientResult    = office365Tenant.IsSharingDisabledForNonOwnersOfSite(Identity.Url);
                    ClientContext.ExecuteQuery();
                    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,
#pragma warning disable CS0618 // Type or member is obsolete
                    Template = Template,
#pragma warning restore CS0618 // Type or member is obsolete
                    Filter = Filter,
                };

                if (ClientContext.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 == Template).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);
                }
            }
        }