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
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!
|CryptoCurrency||USD||Change 1h||Change 24h||Change 7d|
|Bitcoin||0.46 %||2.30 %||7.96 %|
|Ethereum||0.34 %||0.42 %||6.60 %|
|XRP||0.20 %||0.17 %||2.48 %|
|Tether||0.02 %||0.15 %||0.12 %|
|Bitcoin Cash||0.59 %||0.10 %||7.26 %|
|ChainLink||3.93 %||8.46 %||65.40 %|
|Cardano||0.95 %||2.12 %||7.76 %|
|Bitcoin SV||0.30 %||0.18 %||3.85 %|
|Litecoin||0.01 %||0.19 %||2.70 %|
|Binance Coin||0.15 %||0.06 %||8.16 %|
|Crypto.com Coin||0.50 %||2.33 %||3.98 %|
|EOS||0.10 %||0.29 %||3.97 %|
|Tezos||0.16 %||9.32 %||28.56 %|
|Stellar||0.72 %||1.28 %||2.35 %|
|Monero||0.04 %||0.70 %||10.20 %|
|OKB||0.61 %||0.98 %||2.95 %|
|TRON||0.16 %||2.05 %||9.22 %|
|VeChain||1.24 %||2.48 %||29.88 %|
|Cosmos||4.25 %||25.90 %||43.08 %|
|LEO Token||0.22 %||0.77 %||0.50 %|