web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
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
I have the same question (0)
  • Clofly Profile Picture
    260 on at

    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

  • Suggested answer
    Fubar Profile Picture
    2,761 on at

    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).

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
11manish Profile Picture

11manish 36

#2
Mallesh Deshapaga Profile Picture

Mallesh Deshapaga 32

#3
ManoVerse Profile Picture

ManoVerse 30 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans