In this SharePoint tutorial, we will discuss how to get lists and document libraries programmatically using the client object model (CSOM) in SharePoint Online. Here we will retrieve using C#.Net client object model code in SharePoint Online.
Before we start, we need to create a console application in Visual Studio. Here I am using Visual studio 2010 but you can do the same thing in higher version of Visual Studio.
Get all lists and document libraries programmatically using CSOM in SharePoint Online
Step 1: Open your Visual Studio
Step 2: Create a new console application and enter your solution Name and save it in your any drive location.
Step 3: Before we start , we need to add the SharePoint client object model dll in our application. For adding the dll we can add the dll manually under references or we can download it from NuGet gallery.
Step 4: For download the dll from NuGet , we have to go the manage NuGet packages under Tool in Visual Studio.
Step 5: Download the references which is required to connect SharePoint online. Below is the screenshot of reference file which is required to download.
Step 6: Next click on Install button. Once it will done, we will get an screen to select the project.
Step 7: Once you click on OK button , again you will get an screen to click Accept or Decline. Please click on Accept button to go forward.
Step 8: Once you click on Accept button, Download will start and you will get your all references inside reference file in your application.
Step 9: Next open your Program.cs file and write the below code under Main function.
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
namespace getDataFromSharePointOnline
{
class Program
{
static void Main(string[] args)
{
string userName = "r[email protected]";
Console.WriteLine("Enter Your Password Please ------- ");
SecureString password = GetPasswordOfYourSite();
// ClienContext - Get the context for the SharePoint Online Site
using (var clientContext = new
ClientContext("https://pikasha12.sharepoint.com/sites/DLH"))
{
// SharePoint Online Credentials
clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
// Get the SharePoint web
Web web = clientContext.Web;
clientContext.Load(web.Lists,
lists => lists.Include(list => list.Title, // For each list, retrieve Title and Id.
list => list.Id));
clientContext.ExecuteQuery();
foreach (List list in web.Lists)
{
Console.WriteLine(list.Title);
}
Console.ReadLine();
}
}
private static SecureString GetPasswordOfYourSite()
{
ConsoleKeyInfo info;
//Get the user's password as a SecureString
SecureString securePassword = new SecureString();
do
{
info = Console.ReadKey(true);
if (info.Key != ConsoleKey.Enter)
{
securePassword.AppendChar(info.KeyChar);
}
}
while (info.Key != ConsoleKey.Enter);
return securePassword;
}
}
}
Step 10 : Test the application:
Hit F5 to run the application. Enter the password and then click on Enter button.
You may like following SharePoint csom tutorials:
- How to Use CAML Query for Boolean field for SharePoint 2013/2016/Online Object Model CSOM
- How to create an index in SharePoint online list using CSOM .Net managed object model code?
- SharePoint online Read CSV file from document library using .Net managed client object model csom
- Create Update Delete a list using Client Object Model CSOM in SharePoint 2013
- Different ways to hide/disable quick edit in a list in SharePoint 2013/2016/Online
- 20+Â SharePoint .Net Client Object Model Examples
- Copy list items from one site collection to another programmatically using CSOM in SharePoint Online
- Cannot contact web site or the web site does not support sharepoint online credentials
Hope this SharePoint tutorial explains, how to retrieve all list and document libraries in SharePoint Online using CSOM (Client side object model).
Rajkiran is currently working as a SharePoint Consultant in India . Rajkiran having 7+ years of experience in Microsoft Technologies such as SharePoint 2019/2016/2013/2010, MOSS 2007,WSS 3.0, Migration, Asp.Net, C#.Net, Sql Server, Ajax, jQuery etc.He is C#Corner MVP (2 Times).
This code will not work if I have to fetch multiple properties where my website contains 1lakh list. Will it be possible for you fetch solution for the same.
Here is my context.
WebCtx.Load(
collList, lists => lists.Include(
list => list.Title, list => list.BaseType, list => list.BaseTemplate, list => list.ImageUrl, list => list.ParentWebUrl, list => list.Id, list => list.LastItemModifiedDate, list => list.Created, list => list.DefaultView, list => list.DefaultViewUrl, list => list.Author.UserId, list => list.Author.UserPrincipalName, list => list.Author.Title, list => list.ParentWeb, list => list.ParentWeb.Author.UserId, list => list.ParentWeb.Author.UserPrincipalName, list => list.ParentWeb.Author.Title));