Close this search box.

How to retrieve list elements from a SharePoint Online list programatically

SharePoint Online and Office365 have many great features, but until a few months ago, a serious limitation existed. There was absolutely no way for a service to interact with SharePoint Online.

Yes, you could use the Office365 API to log on to SharePoint and retrieve a token that could be used witht he API. But you HAD TO enter the user name and password manually, as the authentication is done using the logon assistant.

This means that if you wanted to integrate SharePoint with another app and for example needed to retrieve the list elements of a certain list from outside SharePoint, there was no way to do that without having a human actually log in.

In April 2015 Microsoft launched a new version of their client SDK, containing amongst others a class called SharePointOnlineCredentials, which provides access to SharePoint Online using simply a user name and a password. The SDK is available as a Nuget package, and makes it possible to interact with SharePoint Online using CSOM. Apparently, you can do anything that you can do OnPremise, with a few exceptions.

For me, this is the final corner piece of the puzzle. I can now write a service that reads and writes list elements and documents from and to SharePoint Online lists and libraries. That is all I need to bring my integration services to a new level. I have played around with the SDK a little bit, and it works absolutely great!

Here is a small sample that shows how to retrieve the list elements of the list “MyList” and display them, simply using a console app. Make sure you have installed the Nuget package first.

PS: I apologize for the code formatting. Windows live writer (that they make us use) is a piece of crap. I will see if I can find another tool for writing the blog.

class Program


static void Main(string[] args)


string tenant = "";

string userName = "";

string passwordString = "mypassword";

TestConnect(tenant, userName, passwordString);


private static void TestConnect(string tenant, string userName, string passwordString)


// Get access to source site

using (var ctx = new ClientContext(tenant))


//Provide count and pwd for connecting to the source

var passWord = new SecureString();

foreach (char c in passwordString.ToCharArray()) passWord.AppendChar(c);

ctx.Credentials = new SharePointOnlineCredentials(userName, passWord);

// Actual code for operations

Web web = ctx.Web;



Console.WriteLine(string.Format("Connected to site with title of {0}", web.Title));


//Get my list

CamlQuery query = new CamlQuery();

List myList = web.Lists.GetByTitle("MyList
ListItemCollection kundeItems = myList.GetItems(query);


Console.WriteLine("Getting list items");

foreach (var spItem in kundeItems)


string title = (string)spItem["Title"];





This article is part of the GWB Archives. Original Author: Thorvald Bøe

Related Posts