How to Read a SharePoint List Using .NET 8?

Artillis Prado 0 Reputation points
2024-07-17T18:10:37.2833333+00:00

System.Net.WebException: 'The remote server returned an error: (403) FORBIDDEN.'
I am using .NET 8, and I've already tried with GetACSAppOnlyContext but it wouldn't work because I need to access more than 5 SharePoint sites simultaneously. There was an error in CC.ExecuteQuery();

    using (CC = authManager.GetWebLoginClientContext(siteUrl))
    {
        List list = CC.Web.Lists.GetByTitle("TT_002_Test");
        CamlQuery oQuery = CamlQuery.CreateAllItemsQuery();
        ListItemCollection oCollection = list.GetItems(oQuery);
        CC.Load(oCollection);
        CC.ExecuteQuery();
	}  

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,273 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.
10,640 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Emily Du-MSFT 44,151 Reputation points Microsoft Vendor
    2024-07-18T09:48:13.12+00:00

    Whether the account enabled MFA?

    Make sure the account has permission for 5 sites then run following codes. Replace username, password, and domain with the appropriate values.

    using (ClientContext context = new ClientContext(siteUrl)) 
    { 
        string userName = "username"; 
        string password = "password"; 
        string domain = "domain"; 
        context.Credentials = new NetworkCredential(userName, password, domain); 
        List list = context.Web.Lists.GetByTitle("TT_002_Test"); 
        CamlQuery oQuery = CamlQuery.CreateAllItemsQuery(); 
        ListItemCollection oCollection = list.GetItems(oQuery); 
        context.Load(oCollection); context.ExecuteQuery(); 
    }
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.


  3. Bruce (SqlWork.com) 61,491 Reputation points
    2024-07-23T21:28:48.4866667+00:00

    it sounds like your sharepoint site is using oauth, and you are trying to use an application id (clientid/secret). in sharepoint the appid are part of the site, so you will have a unique clients/secret per site. you need a unique connection for each site anyway, so each url will have its login.

    0 comments No comments