Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Suggested answer

error 401, unauthorized

(0) ShareShare
ReportReport
Posted on by 5

I want to create a contact record whenever an account record is created in power apps portals. when I run the JavaScript I get the error 401 unauthorized. 

I have set site settings for both accounts and contacts. I have given entity permissions for both the entities, i have also set web roles for the entity permissions and have also assigned the web roles to contact trying to create the record. 

below is my code, i am using web api to create the record.

debugger;
var entity = {};
entity.firstname = "sample";
entity.lastname = "data";

var req = new XMLHttpRequest();
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
//req.setRequestHeader('Access-Control-Allow-Origin', "">testing1.powerappsportals.com");
//req.setRequestHeader('Access-Control-Allow-Credentials', 'true');
//req.setRequestHeader('Access-Control-Allow-Methods',"POST");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 201) {
var uri = this.getResponseHeader("OData-EntityId");
var regExp = /\(([^)]+)\)/;
var matches = regExp.exec(uri);
var newEntityId = matches[1];
} else {
console.log(this.statusText);
}
}
};
req.send(JSON.stringify(entity));
Authenticaion is enabled by local sign in and azure active directory.
The role of my user is system administrator.
what could be the issue?
WhatsApp-Image-2021_2D00_03_2D00_02-at-12.43.18-AM.jpeg
  • Suggested answer
    Fubar Profile Picture
    Fubar 2,752 on at
    RE: error 401, unauthorized

    Your portal user (contact record) needs a Web Role associated to it that has Entity Permissions to perform the actions that you want to perform (create, read etc) -  System Administrator security role does not give access to entities via the PowerApps Portal (nor the Web Role = Administrators by default).

    Edit: your error message does not match the code that you provided.  The error is showing that you are posting to api/accounts but your other code is for api/contacts, and your 'entity' object just has contact fields

    On the link that clofy posted, I would recommend tyring the sample code provided (and also the nice Ajax Web API wrapper code that Microsoft provide on that same page).

  • Clofly Profile Picture
    Clofly 260 on at
    RE: error 401, unauthorized

    Hi tahagain,

    Did you run the code above in console directly?

    If so, please create a web page and bind the function to the click event of a button to test, the API should be called from portal page.

    https://docs.microsoft.com/en-us/powerapps/maker/portals/web-api-perform-operations#step-3---create-a-webpage

    Regards,

    Clofly

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,307 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans