Advertisement
Guest User

Untitled

a guest
Feb 18th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. using System;
  2. using System.Security;
  3. using Microsoft.SharePoint.Client;
  4.  
  5. namespace SharePointOnlineAuthTest
  6. {
  7.     internal class Program
  8.     {
  9.         private static void Main(string[] args)
  10.         {
  11.             const string serverRelativeUrl = "/site";
  12.             const string userName = "user@vccqa.onmicrosoft.com";
  13.             const string password = "password";
  14.  
  15.             using (var ctx = GetSharePointOnlineContext(serverRelativeUrl, userName, password))
  16.             {
  17.                 var web = ctx.Web;
  18.                 ctx.Load(web);
  19.                 ctx.ExecuteQuery();
  20.  
  21.                 Console.WriteLine(web.Title);
  22.                 Console.ReadLine();
  23.             }
  24.         }
  25.  
  26.         private static ClientContext GetSharePointOnlineContext(string serverRelativeUrl, string userName, string password)
  27.         {
  28.             var baseUri = new Uri("https://vccqa.sharepoint.com/");
  29.             var siteUri = new Uri(baseUri, serverRelativeUrl);
  30.  
  31.             var securePassword = new SecureString();
  32.             foreach (var c in password) securePassword.AppendChar(c);
  33.  
  34.             return new ClientContext(siteUri) {Credentials = new SharePointOnlineCredentials(userName, securePassword)};
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement