How to use CAML query to get Sharepoint list using Graph API in .Net

Marcelo Lorenzetti 0 Reputation points
2024-12-26T12:49:12.0566667+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 because 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,663 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,158 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ling Zhou_MSFT 19,940 Reputation points Microsoft Vendor
    2024-12-27T02:31:54.0933333+00:00

    Hi @Marcelo Lorenzetti,

    Thanks for reaching out to us. We are very pleased to support you.

    According to the information you provided and the research I have done, I may understand that you want to use CAML Query in the Graph API. If there is anything wrong, please correct me.

    According to the description and the research I have done, the Graph API does not support CAML queries. We usually use CAML queries in the SharePoint REST API or PowerShell.

    Reading the code you provided, we did not initially find any issues, are you experiencing any problems with the implementation of your code? If so, please let us know your problem in detail.

    If there is any question, please feel free to contact me.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. By doing so, it will benefit all community members who are having this similar issue. Your contribution is highly appreciated.

    0 comments No comments

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.