8

I knew how to make a RESTful API call to SharePoint 2013 OnLine from SharePoint APP (Provided-Host App). Like get List from Site. There are already many samples in here.

Because the TokenHelper can handle the OAuth authentication for the APP. But I wondered If this can be made from a C# Console Program. If it can't be made by OAuth. Any other ways of Authentication? Thanks.

  • Why not use straight CSOM? You just need to set the ClientContext.Credentials property to a SharePointOnlineCredentials object. – wjervis Sep 3 '14 at 11:47
  • Could you give me a sample code to help me more understanding ? Thanks in advance. – Joe.wang Sep 3 '14 at 11:51
  • Expand on which part? How to accomplish what you request with CSOM instead of REST? Or how to set the credentials? – wjervis Sep 3 '14 at 11:58
  • I think what you mean is this way. Right ? thanks. – Joe.wang Sep 3 '14 at 12:04
  • Yes, that is indeed the way I meant (needed to meet the minimum character count). – wjervis Sep 3 '14 at 12:07
20

In order to consume SharePoint REST service using .NET you could consider the following approaches:

  • HttpClient - Provides a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI. (.NET Framework 4.5)
  • WebClient - provides common methods for sending data to and receiving data from a resource identified by a URI. (.NET Framework 1.1)
  • HttpWebRequest - provides an HTTP-specific implementation of the WebRequest class, more low-level then the previous ones

All of them allows to perform CRUD operations in SharePoint Online.

About SharePoint Online authentication

Since SharePoint Online (SPO) uses claims based authentication, you could consider the following options:

Example

The following example demonstrate how to retrieve List using WebClient:

    public static JToken GetList(Uri webUri, ICredentials credentials,string listTitle)
    {
        using (var client = new WebClient())
        {
            client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
            client.Credentials = credentials;
            client.Headers.Add(HttpRequestHeader.ContentType, "application/json;odata=verbose");
            client.Headers.Add(HttpRequestHeader.Accept, "application/json;odata=verbose");
            var endpointUri = new Uri(webUri, string.Format("/_api/web/lists/getbytitle('{0}')", listTitle));
            var result = client.DownloadString(endpointUri);
            var t = JToken.Parse(result);
            return t["d"];
        }
    }

Dependency: Json.NET library

Usage

The example demonstrates how to retrieve Contacts List and print its title:

static void Main(string[] args)
{
      var webUri = new Uri("https://contoso.sharepoint.com");
      const string userName = "username@contoso.onmicrosoft.com";
      const string password = "password";
      var securePassword = new SecureString();
      foreach (var c in password)
      {
          securePassword.AppendChar(c);
      }
      var credentials = new SharePointOnlineCredentials(userName, securePassword);

      var list = GetList(webUri,credentials,"Contacts");
      //print List title
      Console.WriteLine(list["Title"]);
}
  • 3
    Is there a way to do it without using any third party dll or Sharepoint SDK/ CSOM? – Shakti Prakash Singh Nov 19 '15 at 7:07

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.