Get ListItems not getting the items using SharePoint AppOnly Authentication

Suresh S 1 Reputation point
2021-09-18T17:52:22.463+00:00

Hi All,

I am using App only SharePoint AppOnly Authentication. I have created "ClientID and Client Secret ID" as well. Based on that, I can authenticate the using the below and trying to get the document library list items but the below code returning 0 count. Please suggest me to resolve this issue.

var ctx = new OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(repositoryUrl, "fsfsd-sdsdsf-353453-sdgsds", "vsdsdUOUnklglkn=sdgsgdUTIUTIISDSDGsdgsPOP=");

Site site = ctx.Site;
ctx.Load(site);
ctx.ExecuteQuery();

            Microsoft.SharePoint.Client.ListItemCollection listitems = null;
            Microsoft.SharePoint.Client.List docList = ctx.Web.Lists.GetByTitle("Test Lib");
            ctx.Load(docList);
            ctx.ExecuteQuery();
            ctx.Load(docList.RootFolder);
            ctx.ExecuteQuery();

            List<Microsoft.SharePoint.Client.ListItem> items = new List<Microsoft.SharePoint.Client.ListItem>();
            int i = 0;
            ListItemCollectionPosition position = null;
            int rowLimit = 0;
            var camlQuery = new CamlQuery();
            camlQuery.ViewXml = @"<View><Query><OrderBy Override=""TRUE""><FieldRef Name='ID'/></OrderBy></Query><RowLimit Paged=""TRUE"">" + rowLimit + "</RowLimit></View>";
            camlQuery.FolderServerRelativeUrl = docList.RootFolder.ServerRelativeUrl + "/" + "2000/O003";
            camlQuery.ListItemCollectionPosition = position;
            listitems = docList.GetItems(camlQuery);
            ctx.Load(listitems);
            ctx.ExecuteQuery();
            position = listitems.ListItemCollectionPosition;
            items.AddRange(listitems.ToList());

Thanks & Regards
Suresh S

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.
2,691 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,576 questions
SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,821 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 31,686 Reputation points Microsoft Vendor
    2021-09-20T06:43:42.46+00:00

    Hi @Suresh S ,
    Per my test,The issue occurs when we didn't grant enough permission.We have authenticate the site but still don't have the permission to the list.I suggest to set full control of the site collection or Website.
    Please try following App's Permission Request XML in the url
    https://contoso-admin.sharepoint.com/sites/abc/_layouts/15/appinv.aspx

    <AppPermissionRequests AllowAppOnlyPolicy="true">  
      <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl" />  
    </AppPermissionRequests>  
    

    Please refer to the following link
    https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/add-in-permissions-in-sharepoint


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.