How to get list of SharePoint Lists in SharePoint 2013 Online using CSOM

Home | Uncategorized | How to get list of SharePoint Lists in SharePoint 2013 Online using CSOM

In this post, we’ll see how to get the list of the SharePoint List in SharePoint 2013 Online using CSOM.

Open Visual Studio 2013.

File –> New –> Project –> Visual C# –> Console Application  and name it as ‘ReadSharePointLists’

Add a reference to assemblies ‘Microsoft.SharePoint.Client’ and ‘Microsoft.SharePoint.Client.Runtime’.

Import the following two namespaces.

using Microsoft.SharePoint.Client;
using System.Security;

Copy and paste the following snippet of the code that will help us to fetch the list of SharePoint lists in the SharePoint Online site.

namespace ReadSharePointLists
{
    class Program
    {
        static void Main(string[] args)
        {
            //Replace it with the url of your tenant or your site-collection
            string SiteUrl = "https://yoursite.sharepoint.com";

            System.Uri oUri = new System.Uri(SiteUrl);

            using (ClientContext oClientContext = new ClientContext(SiteUrl))
            {
                //Replace it with your user id for SharePoint Online
                string UserName = "userid@yoursite.onmicrosoft.com";


                //Replace it with your password
                string Password = "password";

                //Create a SecureString object from password string, needed for SharePointOnlineCredentials class
                SecureString SecurePassword = GetSecureString(Password);
                oClientContext.Credentials = new SharePointOnlineCredentials(UserName, SecurePassword);

                //load the properties of web object
                Web oWeb = oClientContext.Web;
                
                //Get all the lists in the web
                oClientContext.Load(oWeb.Lists);
                oClientContext.ExecuteQuery();


                foreach (List oList in oWeb.Lists)
                {
                    Console.WriteLine(oList.Title.ToString());
                
                }
                Console.ReadLine();


            
            }

        }

        private static SecureString GetSecureString(String Password)
        {
            SecureString oSecurePassword = new SecureString();

            foreach (Char c in Password.ToCharArray())
            {
                oSecurePassword.AppendChar(c);

            }
            return oSecurePassword;
        }

    }
}

 

After executing this we’ll see the below result, which displays the names of the SharePoint lists in the SharePoint 2013 Online site.clip_image002

 Subscribe to my blog

Leave a Reply