I am new to azure function. i am creating a function to call a SharePoint site. But Stuck at execute query.

Here is my code -

var password = "Somepassword";

                var username = "Some login name";

                var Url = "Site URL";
                using (var context = new ClientContext(Url))
                    var securepass = new SecureString();
                    foreach (char c in password)
                    log.LogInformation("C# HTTP");
                    SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(username, securepass);
                    context.Credentials = credentials;
                    Web web = context.Web;
                    log.LogInformation("web object load");

At execute query I am getting an exception:-

[Information] The remote server returned an error: (400) Bad Request. [Information] at System.Net.HttpWebRequest.GetResponse() at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute() at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate()
at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest() at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery() at Submission#0.d__1.MoveNext() in D:\home\site\wwwroot\HttpTrigger3\run.csx:line 61 [Information] Executed 'Functions.HttpTrigger3' (Succeeded, Id=Some Id)

I am not understanding where I went wrong. Could anyone please help. Thanks

  • you need to use Azure functions v1. Looks like you are using v2, just recreate it using v1 and it should work – Gautam Sheth Mar 19 at 7:41
  • How to change that? – Dixit Soni Mar 19 at 7:45
  • you need to recreate it again as i mentioned in comments. – Gautam Sheth Mar 19 at 8:10
  • One more thing, do I have to use execute query only once? – Dixit Soni Mar 19 at 9:11

You need to create App ID and App Secret ID from SharePoint and connect via that. Since you are connecting from Azure Function app.

I did this with PnP PowerShell, you can check how to do the same for CSOM.

For PowerShell:

Connect-PnPOnline -AppId -AppSecret

  • I was using different version of function app. Changed the version and it worked. – Dixit Soni Mar 19 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.