How to limit and orderby desc items returned by query parameters in a Sharepoint site

ReneRam 0 Reputation points
2023-08-17T11:51:56.1533333+00:00

I'm currently developing API rest that retrieves a list of documents created with a template in a Sharepoint site. I'm using the MS Graph SDK. I'm able to retrieve the list of items, but I'm not able to limit the list to the last 3 o 5 items added. I have been through the documentation, but still getting no changes. This is the code in GraphHelper class:

        // <MakeGraphCallSnippet>
        public async Task<List<Dictionary<string, object>>> GetSiteItems()
        {
            // Ensure Graph has been initialized
            InitializeGraphForAppOnlyAuth();

            // Ensure client isn't null
            _ = _appClient ??
                throw new System.NullReferenceException("Graph has not been initialized for app-only auth");

            var listId = "xxxxxxxx";
            var itemsResponse = await _appClient.Sites["xxxxxx"]
                .Lists[listId]
                .Items
                .GetAsync((requestConfiguration) =>
                {
                    // set order by desc
                    requestConfiguration.QueryParameters.Orderby = new string[] { "createdDateTime desc" };
                    // set number of items to retrieve
                    requestConfiguration.QueryParameters.Top = 3;
                    // set selected fields to retrieve
                    requestConfiguration.QueryParameters.Expand = new string[] { "fields($select=Title,Description,Class,Class_x003a__x0020_ID_x0020_Class,StartDate,EndDate)" };
                });

            //var items = itemsResponse.Value;
            var items = itemsResponse.Value.ToList();

            var result = new List<Dictionary<string, object>>();

            foreach (var item in items)
            {
                var itemData = new Dictionary<string, object>();
                if (item.Fields?.AdditionalData != null)
                {
                    var fields = item.Fields.AdditionalData;

                    if (fields.TryGetValue("Title", out var title))
                        itemData["Title"] = title;

                    if (fields.TryGetValue("Description", out var description))
                        itemData["Description"] = description;

                    if (fields.TryGetValue("Class", out var classValue))
                        itemData["Class"] = classValue;

                    if (fields.TryGetValue("Class_x003a__x0020_ID_x0020_Class", out var classId))
                        itemData["Class_x003a__x0020_ID_x0020_Class"] = classId;

                    //if (fields.TryGetValue("DateandTime", out var dateAndTime))
                    //    itemData["Date and Time"] = dateAndTime;

                    if (fields.TryGetValue("StartDate", out var startDate))
                        itemData["Start Date"] = startDate;

                    if (fields.TryGetValue("EndDate", out var endDate))
                        itemData["End Date"] = endDate;

                    // Retrieve and add the unique ID
                    if (item.Id != null)
                        itemData["ID"] = item.Id;

                    result.Add(itemData);
                }
            }

            return result;

        }
        // </MakeGraphCallSnippet

Any help apreciated.

Thanks

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,504 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,230 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,417 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. TH-4749-MSFT 3,315 Reputation points
    2023-08-17T14:40:15.1633333+00:00

    Hello ReneRam,

    Thanks for reaching out. For Graph SDK related issues please post the query on the respective Graph SDK Github Forum. . Are you able to reproduce the issue using Graph Explorer or Postman?

    Thanks.

    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.