Geeks With Blogs

News Cloud development with Azure, Visual Studio and SharePoint Online!
Thorvald Bøe

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 = "https://mytenant.sharepoint.com";

string userName = "myusername@mytenant.onmicrosoft.com";

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;

ctx.Load(web);

ctx.ExecuteQuery();

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

Console.ReadLine();

//Get my list

CamlQuery query = new CamlQuery();

List myList = web.Lists.GetByTitle("MyList");

ListItemCollection kundeItems = myList.GetItems(query);

ctx.Load<List>(myList);

ctx.Load<ListItemCollection>(kundeItems);

ctx.ExecuteQuery();

Console.WriteLine("Getting list items");

foreach (var spItem in kundeItems)

{

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

Console.WriteLine(title);

}

Console.ReadLine();

}

}

Posted on Tuesday, July 7, 2015 10:15 AM | Back to top


Comments on this post: How to retrieve list elements from a SharePoint Online list programatically

# re: How to retrieve list elements from a SharePoint Online list programatically
Requesting Gravatar...
Hi,
Thank you very much for this post. It works great. Do you have anything similar for web app cause I want to retrieve some of the SharePoint online list items and publish them as xml in order to use them in different application.
Thank you,
Besiana
Left by besiana peni on Mar 07, 2017 10:48 AM

# re: How to retrieve list elements from a SharePoint Online list programatically
Requesting Gravatar...
can I handle the database of a list in sharepoint online 2013 using c #?
Left by bassma battikh on May 10, 2017 12:49 PM

# re: How to retrieve list elements from a SharePoint Online list programatically
Requesting Gravatar...
Hi Bassma,
are you asking if you have the above capabilities with SP 2013 OnPremise? You certainly can, you can use either the CSOM or REST apis to do so. I would also check out the PnP Core Nuget packages that offers extended convenient capabilities for doing common operations.

If you are asking if it is possible to access the database directly, you can also do that (its just a standard MSSQL db), but I would certainly recommend against that.

-Thorvald
Left by Thorvald on May 10, 2017 2:47 PM

Your comment:
 (will show your gravatar)


Copyright © Thorvald Bøe | Powered by: GeeksWithBlogs.net