CSOM Sharepoint: Why do all examples use async method?

gsaunders 96 Reputation points
2021-04-02T15:47:20.22+00:00

VS 2019 C#
CSOM

I have noticed all of the examples I have seen demonstrating authenticating and getting back the basic info like web.title from an online sharepoint site are using the public static async type of method.

Why are they using async at all?

When getting the access token they use the await command as that method is also setup as async.

Just curious if there was a reason these examples were all done this way in order for it to function or if it was even necessary. I don't really see anything needing to run asynchronously... at least just getting authenticated and reading some web site info.

Thanks in advance for your thoughts and feedback!

// Why is this even async
public static async Task CSOMAuthenticate(string[] args)
            {
                string siteURL = "https://oursite.sharepoint.com/sites/ourname";
                string clientId = "our clientID"; // Get client ID from Azure AD
                string certThumprint = "ourthumbprint";
                string tenantId = "ourtenantID";

                //For SharePoint app only auth, the scope will be the SharePoint tenant name followed by /.default
                var scopes = new string[] { "https://oursite.sharepoint.com/.default" };
                // GetAppliationAuthenticatedClient is also async.  They use await here so it doesn't move on which makes me ask why make it async in the first place.
                var accessToken = await GetApplicationAuthenticatedClient(clientId, certThumprint, scopes, tenantId);
                var clientContext = GetClientContextWithAccessToken(siteURL, accessToken);

                Web web = clientContext.Web;
                clientContext.Load(web);

                clientContext.ExecuteQuery();

                MessageBox.Show(web.Title, "Title");

            }
Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

Accepted answer
  1. gsaunders 96 Reputation points
    2021-04-02T16:02:05.07+00:00

    I think I figured it out.

    Looks like the method to get the accessToken uses IConfidentialClientApplication and the execution for it is called with ExecuteAsync().

    I don't see an execute that isn't async which leads to needing this method to be async and the method calling it to be async it seems.

    internal static async Task<string> GetApplicationAuthenticatedClient(string clientId, string certThumprint, string[] scopes, string tenantId)
            //internal static string GetApplicationAuthenticatedClient(string clientId, string certThumprint, string[] scopes, string tenantId)
            {
                X509Certificate2 certificate = GetAppOnlyCertificate(certThumprint);
                IConfidentialClientApplication clientApp = ConfidentialClientApplicationBuilder
                                                .Create(clientId)
                                                .WithCertificate(certificate)
                                                .WithTenantId(tenantId)
                                                .Build();
    
                // Looks like this is an Async call for clientApp
                AuthenticationResult authResult = await clientApp.AcquireTokenForClient(scopes).ExecuteAsync();
                string accessToken = authResult.AccessToken;
                return accessToken;
            }
    
    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.