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
    2,756 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
    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

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

#1
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 51 Most Valuable Professional

#2
Ramesh Kumar Profile Picture

Ramesh Kumar 42

#3
David Shaw_UK Profile Picture

David Shaw_UK 27

Featured topics

Product updates

Dynamics 365 release plans