1

Any help is much appreciated. I'm trying to save data from a html form to a list in SharePoint online. I'm using SharePoint REST API to add items to the list. Ajax call to generate access token is throwing cors error.

$.ajax({
url: "https://accounts.accesscontrol.windows.net/tenant_id/tokens/OAuth/2",
type: "POST",
data: JSON.stringify({
"grant_type": "client_credentials",
"client_id": "<client_id>",
"client_secret": "<client_secret>",
"resource": "00000003-0000-0ff1-ce00-000000000000/site_url@tenant_id"
}),
headers: {
"contentType": "application/x-www-form-urlencoded",
},
success: function() {},
error: function() {}
})

Note: This code returns an access token in postman. If I use that access token in SharePoint REST Api, I'm able to add data to list. So the challenge is generating access token from my javascript application instead of Postman. I can't use MSAL as I don't want to register my app in Azure AD. I don't want a temporary fix like using a chrome extension to fix cors error or modifying rewrite rules in IIS.

I'm new to SharePoint, so any insights on alternate approaches would also be helpful.

2

1 Answer 1

0

I'm not sure to understand why you need to connect to "https://accounts.accesscontrol.windows.net/tenant_id/tokens/OAuth/2"? Is your HTML page inside the SharePoint site where the list is stored?

Because you can get the request digest from "/_api/contextinfo" with something like:

$.ajax({
  url: "<site_url>/_api/contextinfo", 
  header: {
    "accept": "application/json; odata=verbose", 
    "content-type":"application/json;odata=verbose"
  }, 
  type: "POST", 
  contentType: "application/json;charset=utf-8"
})

It will return a JSON string with d.GetContextWebInformation.FormDigestValue that you can then use in the header X-RequestDigest.

Once you have the Request Digest, you can use Sharepoint REST API to add stuff into the list.

5
  • Hi @AymKdn, Thanks a lot for the suggestion. My html page is not inside SharePoint. It runs on localhost. So I need to authenticate my javascript application to connect with SharePoint Online. So I registered my application in SharePoint and generated client ID and credentials which I'm passing into the "accounts.accesscontrol.windows.net/tenant_id/tokens/OAuth/2". This will return access token which can be used in SharePoint REST api call. But the access token is not getting generated due to cors error.
    – user100006
    Commented Sep 6, 2021 at 15:13
  • In localhost?! The purpose is to keep it in the localhost?!
    – AymKdn
    Commented Sep 6, 2021 at 19:34
  • Yeah. Currently it's not hosted anywhere. I spent two days on this, nothing worked :( Now, I'm trying to add this html and javascript to a page in Modern SharePoint Online. It would be great if you can give me your suggestion on this or if there are any other approach that is much feasible?
    – user100006
    Commented Sep 7, 2021 at 5:21
  • Search for it on Stackoverflow: to do JS and HTML in Sharepoint Online. It really depends the kind of access you have and so on.
    – AymKdn
    Commented Sep 7, 2021 at 9:55
  • I searched and nothing worked so far. Anyways, I'll try some other approach. Thanks a lot for your inputs @AmyKdn.
    – user100006
    Commented Sep 7, 2021 at 11:54

Your Answer

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

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