I am trying to write a simple Console Application in .NET using the CSOM object model.

I write the following code (omitted the client context declaration and the authentication parts from below):

var lib = context.Web.Lists.GetByTitle("A");


However, when I debug the code and examine the "lib" object, all the properties have a PropertyOrFieldNotInitializedException. What's the correct way to write code to not get that error?



What is "sdf"?

Add this line context.Load(lib); into your code before context.ExecuteQuery();, compare the results.

With the Load(lib), it informs the client object model to load the information of SP.List object when the application calls the ExecuteQuery method.

Here is demo to connect to SharePoint Online and get list/library by title:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using System.Security;

namespace TestGetFiles
    class Program

        private class Configuration
            public static string ServiceSiteUrl = "https://tenant.sharepoint.com/<site>";
            public static string ServiceUserName = "admin@tenant.onmicrosoft.com";
            public static string ServicePassword = "<password>";

        static ClientContext GetonlineContext()
            var securePassword = new SecureString();
            foreach (char c in Configuration.ServicePassword)
            var onlineCredentials = new SharePointOnlineCredentials(Configuration.ServiceUserName, securePassword);
            var context = new ClientContext(Configuration.ServiceSiteUrl);
            context.Credentials = onlineCredentials;
            return context;

        static void Main(string[] args)  
            var clientContext=GetonlineContext();  
            List list = clientContext.Web.Lists.GetByTitle("<library>"); 
            var fileName = "test.docx";
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml =
                       <Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='File'>" + fileName + @"</Value></Eq></Where> 
                       <ViewFields><FieldRef Name='FileRef' /><FieldRef Name='FileLeafRef' /></ViewFields> 
            ListItemCollection listItems = list.GetItems(camlQuery);
            foreach (ListItem oListItem in listItems)
                //do something


Some helpful References:

Connecting Office 365 / SharePoint Online Site Using CSOM (Client Object Model)

How to Connect SharePoint Online site using CSOM

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.