question

gsaunders-2123 avatar image
0 Votes"
gsaunders-2123 asked MichaelHan-MSFT commented

CSOM Sharepoint: Why do all examples use async method?

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");
        
             }
office-sharepoint-online
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

gsaunders-2123 avatar image
1 Vote"
gsaunders-2123 answered MichaelHan-MSFT commented

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;
         }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @gsaunders-2123,

You are right. Thanks for the sharing.

You could accept your answer via the button "Accept Answer", it will help others who have similar issues in the future.

0 Votes 0 ·