How to generate App ID & Secret key to access SharePoint online through console application using Access Token – Part 2


Ahamed Fazil Buhari
SharePoint Developer
Published On :   27 Aug 2018
Visit Count
Today :  6    Total :   366



Hello,

This is continuation article; in part 1 we have seen how to create App Id in SharePoint. In part 2 we will see how to access SharePoint Online site from Console Application using App Id and Secret key.

Creating new Console Application,

clip_image002

I need to add SharePointPnPCore2013 package from nuget package manager

clip_image004

And add the AppForSharePointOnlineWebToolkit package to have TokenHelper.cs file which have necessary function for authentication.

clip_image006

I have my site name, App Id and Secret key in App.config file so that it’s easy to access and update.

clip_image007

In Program.cs we requesting user to choose the environment based on the value we are taking URL from app.config file


 using System;
 using System.Configuration;
 using Microsoft.SharePoint.Client;
 
 namespace TestSP_access
 {
     class Program
     {
         static void Main(string[] args)
         {
             Console.WriteLine("\nSelect The Environment to Provision- ");
             Console.WriteLine("\nnote* - The Site URL and other properties will be taken from App.config file- \n");
             Console.WriteLine("\n'dev' or 'test' or 'qa' or 'prod'");
             Console.WriteLine("\nEnter Your Choice : ");
 
             var envValue = Console.ReadLine();
             if (envValue != "dev" && envValue != "test" && envValue != "qa" && envValue != "prod")
             {
                 Console.WriteLine("\nInvalid Environment value. Please select'dev' or 'test' or 'qa' or 'prod'");
             }
             else
             {
                 try
                 {
                     var siteUri = new Uri(ConfigurationManager.AppSettings["SiteUrl-" + envValue]);
                     var realm = TokenHelper.GetRealmFromTargetUrl(siteUri);
                     var accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal,
                         siteUri.Authority, realm).AccessToken;
 
                     using (var ctx = TokenHelper.GetClientContextWithAccessToken(siteUri.ToString(), accessToken))
                     {
                         Console.WriteLine("\nAccessing SP...");
                         ctx.Load(ctx.Web);
                         ctx.ExecuteQueryRetry();
                         Console.WriteLine("The site name is " + ctx.Web.Title);
                     }
                     Console.ReadLine();
                 }
                 catch (Exception ex)
                 {
                     Console.WriteLine("Something went wrong. " + ex.Message);
                     Console.ReadLine();
                 }
             }
         }
     }
 }
 


This is my test SharePoint site

clip_image009

Now I can access SP online site from my console application using App Id Access Token

clip_image011

Happy Coding

Ahamed

Categories