Blog Post: Custom integration between Dynamics 365/CDS and SharePoint using C# and SharePoint REST API ? Learn how to create a SharePoint Add-in and generate authentication token–Part 3

   2020-06-30 11:06

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at [email protected]

If you are directly on this blog post, I suggest you start from the first blog post of this series to get the context.



So I am on the final blog of this series. And here I am going to generate the authentication token to finally connect to SharePoint. Here we will deal with two most important properties of the Add-in, Add-In ID and Add-In Secret. Using these two properties now we will construct the access token.

Let us achieve this in the following steps

  • Open your VS and create a Console Application. ( I am using VS 2017 and created a console application with .Net framework 4.6.2)
  • After your project is created we need to add the SharePoint Dependencies. To do this right click on References and Manage NuGet packages and look for a package named AppForSharePointOnlineWebToolkit and install the package.

While trying to add the reference you may face an issue saying Microsoft.IdentityModel.dll was not found so package could not be installed.

If that’s the case, add Microsoft.IdentityModel.dll using NuGet package.

  • Post doing step 3 as well you will not be able to reference SharePoint Dependencies since it again looks for another dll named Microsoft.IdentityModel.Extensions dll.
  • Include the reference for Microsoft.IdentityModel.Extensions.dll
  • Now let us try to install the package named AppForSharePointOnlineWebToolkit. Not only this package will add the SharePoint Dependencies but also includes the class files TokenHelper.cs and SharePointContext.cs
  • Now we have included all the dependencies required for the operation. Make sure to include the below configuration in App.config file
  • Use the Client ID and Client Secret of the Add-In and replace its values in App.Config file. This is because TokenHelper.cs class will grab the id and secret from the application’s configuration file. Add the following piece of code in your application to generate the token.

string siteUrl = "https://xrm20208.sharepoint.com";

//Get the realm for the URL

string realm = TokenHelper.GetRealmFromTargetUrl(new Uri(siteUrl));

//Get the access token for the URL.  

string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, new Uri(siteUrl).Authority, realm).AccessToken;

  • As you can see in the below screenshot my token is generated successfully.

Yahoo! Now that is some relief right? The token is now generated and you can passing it as a bearer token while making any calls to SharePoint REST API. I am not going to show you how you are now going to consume the REST API. We have innumerable good SharePoint blogs for the same. So I leave the remaining part for you to explore.

Hope this helps!

Debajit Dutta

(Microsoft MVP)


Original Source


CryptoCurrencyUSDChange 1hChange 24hChange 7d
Bitcoin11,971 0.44 % 2.28 % 8.18 %
Ethereum395.48 0.26 % 0.17 % 6.68 %
XRP0.2934 0.13 % 0.46 % 2.48 %
Tether1.000 0.10 % 0.07 % 0.10 %
Bitcoin Cash306.62 0.27 % 0.17 % 7.93 %
ChainLink13.09 0.42 % 3.39 % 58.03 %
Cardano0.1454 0.05 % 2.24 % 8.69 %
Bitcoin SV225.54 0.74 % 0.71 % 3.81 %
Litecoin58.22 0.01 % 0.19 % 2.70 %
Binance Coin22.64 0.16 % 0.34 % 8.29 %
Crypto.com Coin0.1709 0.27 % 2.62 % 4.45 %
EOS3.060 0.15 % 0.05 % 4.05 %
Tezos3.660 1.31 % 7.55 % 27.36 %
Stellar0.1057 0.54 % 1.53 % 2.88 %
Monero93.74 0.60 % 0.57 % 10.23 %
OKB5.890 0.70 % 0.63 % 3.41 %
TRON0.02069 0.31 % 1.23 % 8.88 %
VeChain0.02116 1.40 % 2.44 % 30.88 %
Cosmos5.330 0.30 % 20.14 % 37.20 %
LEO Token1.270 0.50 % 1.29 % 0.08 %