C# to retrieve list items from sharePoint online

Bommisetty, Gayatri 1 Reputation point
2023-04-05T23:25:24.9466667+00:00

I have console application running on my local machine, got error "System.Net.WebException: 'The remote server returned an error:(403) Forbidden.' " when executed below code Below code is working fine for SharePoint server sites but not for SharePoint online sites.

 var webUrl = new Uri("https://SharePointOnline123.com");
            using (ClientContext ctx = new ClientContext(webUrl))
            {              
                Web webObj = ctx.Web;
                List listObj = webObj.Lists.GetByTitle("AbcList");
                CamlQuery cqObj = CamlQuery.CreateAllItemsQuery(100);
                ListItemCollection colObj = listObj.GetItems(cqObj);

                ctx.Load(colObj);
                ctx.ExecuteQuery();

                if (colObj.Count != 0)
                {
                    foreach (ListItem item in colObj)
                    {
                        Console.Write($"ID: {item.Id}");
                        //Console.Write($"Title: {item.Client_Title}");
                    }
                }
             };
Microsoft 365 and Office | Install, redeem, activate | For business | Windows
Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments

1 answer

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.

    3 deleted comments

    Comments have been turned off. Learn more

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.