public List<PSADGroup> FilterGroups(ADObjectFilterOptions options)
{
List<PSADGroup> groups = new List<PSADGroup>();
if (!string.IsNullOrEmpty(options.Id))
{
try
{
// use GetObjectsByObjectId to handle Redirects in the CSP scenario
PSADGroup group = this.GetObjectsByObjectId(new List<string> { options.Id }).FirstOrDefault() as PSADGroup;
if (group != null)
{
groups.Add(group);
}
}
catch { /* The group does not exist, ignore the exception */ }
}
else
{
Rest.Azure.IPage<ADGroup> result = null;
Rest.Azure.OData.ODataQuery<ADGroup> odataQuery = null;
if (options.Paging)
{
if (string.IsNullOrEmpty(options.NextLink))
{
if (options.Mail != null)
{
odataQuery = new Rest.Azure.OData.ODataQuery<ADGroup>(g => g.Mail == options.Mail);
}
else
{
odataQuery = new Rest.Azure.OData.ODataQuery<ADGroup>(g => g.DisplayName.StartsWith(options.SearchString));
}
result = GraphClient.Groups.List(odataQuery);
}
else
{
result = GraphClient.Groups.ListNext(options.NextLink);
}
groups.AddRange(result.Select(g => g.ToPSADGroup()));
options.NextLink = result.NextPageLink;
}
else
{
if (options.Mail != null)
{
odataQuery = new Rest.Azure.OData.ODataQuery<ADGroup>(g => g.Mail == options.Mail);
}
else
{
odataQuery = new Rest.Azure.OData.ODataQuery<ADGroup>(g => g.DisplayName.StartsWith(options.SearchString));
}
result = GraphClient.Groups.List(odataQuery);
groups.AddRange(result.Select(g => g.ToPSADGroup()));
while (!string.IsNullOrEmpty(result.NextPageLink))
{
result = GraphClient.Groups.ListNext(result.NextPageLink);
groups.AddRange(result.Select(g => g.ToPSADGroup()));
}
}
}
return groups;
}