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();
	}  

Microsoft 365 and Office | SharePoint | For business | Windows
Developer technologies | C#
{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 51,836 Reputation points Microsoft External Staff
    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) 77,766 Reputation points Volunteer Moderator
    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

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.