Skip to content
Menu
SharePoint Gems
  • Power Platform
  • Development
    • SharePoint 2013
  • Migration
  • Administration
  • SP Online
  • Contact
  • About
    • Privacy Policy
SharePoint Gems

Retrieve SharePoint List Items using CSOM

Posted on December 24, 2020September 29, 2021

Hello SharePointers,
Today, in this article we will see How we can retrieve SharePoint List items using CSOM.

Client Side Object Model (CSOM) can be used in both SharePoint Online as well as SharePoint On-Premise environments. Now let us move on our main topic.

Contents hide
1 Requirement
1.1 Environment:
1.2 Solution to Retrieve SharePoint List Items using CSOM
1.2.1 Declare Variables
1.3 Share this:
1.4 Like this:
1.5 Related

Requirement

Get SharePoint online list items using CSOM.

Environment:

We have a SharePoint online site. Create a custom list named “Issue Tracker”.

We need to retrieve all items from the “Issue tracker” List using CSOM Code.

Solution to Retrieve SharePoint List Items using CSOM

We are going to write a code to get all list items using CSOM in SharePoint Online list.

Below are the steps

  • Open Visual Studio on your computer
  • Select Console application template and give it a name as per your project.
  • Add references to your project, right-click on References, select Add reference and add reference of Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.runtime.dll

Our Solution structure is ready, now we need to write some CSOM code to get SharePoint list items.

Declare Variables

Declare some variable constants which may require in entire solution.

 string userName = "<username>@sharepointgems.com";
            string password = "Mail_123";
            string siteUrl = "https://sharepointgems.sharepoint.com";
            SecureString securePassword = new SecureString();
            foreach (char c in password)
            {
                securePassword.AppendChar(c);
            }
            
            var credentials = new SharePointOnlineCredentials(userName, securePassword);   //SharePointOnlineCredentials(userName, securePassword)
            string sorList = "Issue Tracker";

Once our constants are ready, now we need to create ClientContext and get our site in Context. After getting the site context we will fire a CAML query to get items from SharePoint List.

Once we get the results in ListItemCollection, iterate through each item and write it on the console.

using (ClientContext clientContext = new ClientContext(siteUrl))
{
      clientContext.Credentials = onlineCredentials;
      clientContext.RequestTimeout = -1;

      List oList = clientContext.Web.Lists.GetByTitle(SorList);
      var query = new CamlQuery() 
      { 
          ViewXml = "<View><Query><Where><IsNotNull>                                                       <FieldRef Name='ID'/></IsNotNull></Where></Query></View>" 
};
                ListItemCollection results = oList.GetItems(query);
                clientContext.Load(results);
                clientContext.ExecuteQuery();

                
                foreach (var item in results)
                {
                     Console.WriteLine("ID: {0} \nTitle: {1} \nLoggedBy: {2}", item.Id, item["Title"], item["IssueLoggedBy"]);

                }

By this way we can get all SharePoint List items using CSOM.

You can find some related articles below.

  • Delete all items from SharePoint list with CSOM
  • Connect MFA enabled SharePoint site using CSOM
  • Get Current Context from SharePoint Modern Site Page
  • Retrieve all Communication Sites using PnP PowerShell
  • Solved: Send approval request to a group in MS flow.
  • Validate phone number column in SharePoint
  • SharePoint Online: Delete Folders using PnP PowerShell
  • MS-Flow Interview Questions and Answers
  • Programmatically Upload a File in SharePoint Document Library.
Retrieve SharePoint List Items using CSOM
Merry Christmas from SharePoint Gems

Share this:

  • Print
  • Twitter
  • Facebook
  • LinkedIn
  • WhatsApp
  • Telegram
  • Pinterest
  • Reddit

Like this:

Like Loading...

Related

1 thought on “Retrieve SharePoint List Items using CSOM”

  1. Pingback: SharePoint Online: Identify Current Page is Modern page or Classic page - SharePoint Gems

Leave a ReplyCancel reply

  • Development
  • Migration
  • Poweer Apps
  • Power Automate
  • Power Platform
  • SharePoint 2013
  • SharePoint Administration
  • SharePoint Online
  • Tips
  • Uncategorized

Best of Computers

Blog Stats

  • 67,509 hits

Subscribe

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,433 other subscribers
Buy Me Coffee
©2025 SharePoint Gems | Powered by WordPress and Superb Themes!
%d