Michael Pedersen
Michael Pedersen

Reputation: 91

Access Sharepoint Online document list through ASP.NET

I have been searching for quite sometime on this topic without finding the silver bullet. We have an intranet solution in ASP.NET where we simply want to show some files from our Sharepoint Online in Office 365.

There are a number of hits when you search for it on Google but nothing seems to be the "simple" way to go. I'm not a Sharepoint developer, but have a good understanding of the concepts as I manage our company's Office 365 and Sharepoint Online installations.

I have found current answers that tell me that REST services is the way to go, but here I need an OATH token to access them, and this is where my Sharepoint knowledge is limited. From what I have read the token can only be granted through an installed Sharepoint App, but I know for a fact that it can be done without it too. I have purchased a synchronization tool that syncs our file share with a Sharepoint Document List and for this I don't need any tokens, I just type in my credentials and I don't have to install anything in Sharepoint.

So what am I seeking to get from this question?

Any help is greatly appreciated and thanks in advance! :)

Upvotes: 2

Views: 3942

Answers (2)

Matt
Matt

Reputation: 6050

Microsoft has two set of APIs that can access SharePoint Online:

  1. SharePoint Client Object Model (CSOM):

    With this one you can just use username and password to do authentication. follow the link you can find examples of how to access document list.

  2. SharePoint REST API:

    With this you need to use an OAuth token to do the authentication.

For you case, you should use the first one.

Upvotes: 1

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59358

SharePoint Online also supports Claims-Based authentication mode. The general idea here is to obtain authentication cookies (apart from OAuth authentication flow where access token is issued) from the user credentials. Once the authentication cookie is obtained, you could perform authenticated SOAP (for example via CSOM API) or REST requests against SharePoint Online resources as demonstrated below.

Since you are developing ASP.NET web application, SharePointOnlineCredentials class from SharePoint Online Client Components SDK could be utilized which basically implements claims-based authentication scheme. It could be installed via nuget

Example 1: request list items via CSOM API

using (var ctx = GetContext(webUri.ToString(), userName, password))
{

     var list = ctx.Web.Lists.GetByTitle("Documents");
     var items = list.GetItems(CamlQuery.CreateAllItemsQuery());
     ctx.Load(items);
     ctx.ExecuteQuery();

     foreach (var item in items)
     {
        Console.WriteLine(item.FieldValues["FileRef"]);
     }

}

where

private static ClientContext GetContext(string url,string username, string password)
{
     var ctx = new ClientContext(url);
     var securePassword = new SecureString();
     foreach (char c in password) securePassword.AppendChar(c);
     ctx.Credentials = new SharePointOnlineCredentials(username, securePassword);
     return ctx;
 }

Example 2: request list items via REST API

using (var client = new SPHttpClient(webUri, userName, password))
{
    var listTitle = "Tasks";
    var endpointUrl = string.Format("{0}/_api/web/lists/getbytitle('{1}')/items",webUri,listTitle);
    var data = client.ExecuteJson(endpointUrl);
    foreach (var item in data["value"])
    {
        Console.WriteLine(item["Title"]);
    }
}

where

SPHttpClient.cs - implements HTTP client for SharePoint Online (SPHttpClient class)

SPHttpClientHandler.cs - implements HTTP handler for SharePoint Online

Upvotes: 0

Related Questions