C# to retrieve list items from sharePoint online

Bommisetty, Gayatri 21 Reputation points
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
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-04-06T01:51:02.7666667+00:00

    Hi @Bommisetty, Gayatri, I will recommend you to use CSOM to access SharePoint Online. You can refer to following code

                using (var clientContext = new ClientContext("https://company.sharepoint.com/sites/xxx")){
                    // SharePoint Online Credentials
                    var pwd = "************";
                    var passWord = new SecureString();
                    foreach (char c in pwd.ToCharArray()) passWord.AppendChar(c);
                    clientContext.Credentials = new SharePointOnlineCredentials("******@wxx.onmicrosoft.com", passWord);
                    // Get the SharePoint web
                    Web web = clientContext.Web;
                    clientContext.Load(web);
                    clientContext.ExecuteQuery();
                    List targetList = clientContext.Web.Lists.GetByTitle("List Name");
                    CamlQuery oQuery = CamlQuery.CreateAllItemsQuery();
    
                    ListItemCollection oCollection = targetList.GetItems(oQuery);
                    clientContext.Load(oCollection);
                    clientContext.ExecuteQuery();
    
                    foreach (ListItem oItem in oCollection)
                    {
                        Console.WriteLine(oItem["Title"].ToString());
                    }
                }	
    
    

    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.


0 additional answers

Sort by: Most helpful

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.