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.
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.
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.
1 thought on “Retrieve SharePoint List Items using CSOM”