How to use CAML query using Graph api in .Net

Marcelo Lorenzetti 0 Reputation points
2024-12-26T12:53:45.7966667+00:00

Below is the Current code, i want to add CAML Query here to filter Field "AppCode", "User" and "Group". Here need to check "Group" using OR condition becault we have list of group here

GraphServiceClient _graphServiceClient = getGraphClient(configValues);

var aclList = new HashSet<string>();

Microsoft.Graph.Models.GroupCollectionResponse? response = await _graphServiceClient.Users[userId].MemberOf.GraphGroup.GetAsync(requestConfiguration =>

{

requestConfiguration.QueryParameters.Select = ["id", "displayName"];

requestConfiguration.QueryParameters.Top = 100;

});

var pageIteratorG = PageIterator<

Microsoft.Graph.Models.Group,

Microsoft.Graph.Models.GroupCollectionResponse?

>.CreatePageIterator(_graphServiceClient, response, async (group) =>

{

groupsQuery.Add(group.DisplayName);

return true;

});

await pageIteratorG.IterateAsync();

ListItemCollectionResponse? _listItems = await _graphServiceClient.Sites[configValues.SiteId].Lists[configValues.clientsListID].Items.GetAsync(requestConfiguration =>

{

requestConfiguration.Headers.Add("ConsistencyLevel", "eventual");

requestConfiguration.Headers.Add("Prefer", "HonorNonIndexedQueriesWarningMayFailRandomly");

requestConfiguration.QueryParameters.Select = new[] { "id" };

requestConfiguration.QueryParameters.Expand = new[] { "fields($select=AppCode,User,Group,ACL)" };

requestConfiguration.QueryParameters.Filter = $"fields/AppCode eq '{departmentName}' and (fields/User eq '{currentUser}' or fields/User eq null)";

});

var pageIterator = PageIterator<ListItem, ListItemCollectionResponse>.CreatePageIterator(_graphServiceClient, _listItems, item =>

{

bool check = (bool)item.Fields?.AdditionalData.ContainsKey("Group");

if (check)

{

var groupName = item.Fields?.AdditionalData["Group"];

if (groupName != null)

{

if (groupsQuery.IndexOf(groupName.ToString()) > -1)

{

bool checkACL = (bool)item.Fields?.AdditionalData.ContainsKey("ACL");

if (checkACL)

{

var acl = item.Fields?.AdditionalData["ACL"];

Console.WriteLine("ACL : " + acl);

aclList.Add(acl.ToString());

isValidateWithAdmin = true;

}

}

}

}

return true;

});

await pageIterator.IterateAsync();

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,800 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.