2

I am new to Azure Functions. 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)
    {
        securepass.AppendChar(c);
    }
    log.LogInformation("C# HTTP");
    SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(username, securepass);
    context.Credentials = credentials;
    Web web = context.Web;
    context.Load(web);
    log.LogInformation("web object load");
    context.ExecuteQuery();

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 don't understand what's wrong. Could anyone please help? Thanks!

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

1 Answer 1

0

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

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

PowerShell solution:

Connect-PnPOnline -AppId <app-id> -AppSecret <app-secret-id>
2
  • I was using different version of function app. Changed the version and it worked.
    – Dixit Soni
    Mar 19, 2019 at 13:38
  • 1
    @DixitSoni you can post it and accept it as an answer for future reference. Aug 29, 2019 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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