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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Pages / Can I use WenAPI metho...
Power Pages
Unanswered

Can I use WenAPI methods for custom created entity

(0) ShareShare
ReportReport
Posted on by 175

Hello

https://docs.microsoft.com/en-us/powerapps/maker/portals/web-api-perform-operations

Can I use WbAPI methods (Create , Update , Delete) from above link with my custom created entity?

I tried this methods already with my custom created entity but getting "Resource not found for the segment employee" error every-time.

employee is my custom entity name

Categories:
I have the same question (0)
  • H V Profile Picture
    1,510 on at

    Hi @Dhananjay_Patil

    Yes, you can use WebAPI for create, update and delete records.

    But, Your portal version must be 9.2.6.41 or later for this feature to work.

     

    --------------------------
    If you like this post, give a Thumbs up. Where it solved your query, Mark as a Solution so it can help other people!

  • Dhananjay_Patil Profile Picture
    175 on at

    Hello @hardikv 

    My portal version is  9.2.6.58 but still facing problem, I can able to create , update , delete records for contact entity  from the code mentioned in this link

    https://docs.microsoft.com/en-us/powerapps/maker/portals/web-api-perform-operations

    But if I tried it for custom entity it always fails

    What I Change in code is

    1. Change in url end point , replaced custom entity local name at end of url
    2. Added site settings for custom entity , used logical name everywhere
    3. While adding site setting fields added logical name of entity fields
    4. used logical name of entity fields in create api as a key

     

  • H V Profile Picture
    1,510 on at

    Hi @Dhananjay_Patil

    Did you create the Entity Permission for your custom entity?

     

    Can you pleass share code? If possible. 

  • Dhananjay_Patil Profile Picture
    175 on at

    Hello @hardikv 

    Yes I already added entity permission and assigned it to one web role and that web role assigned to one contact

    Please find Site Settings in attached image

    I had just changed entity name and field names in addSampleRecord() method

    Getting this error : Resource not found for the segment cr6a3_employee.

    code as below:

     

     

    <style>
     #processingMsg {
     width: 150px;
     text-align: center;
     padding: 6px 10px;
     z-index: 9999;
     top: 0;
     left: 40%;
     position: fixed;
     -webkit-border-radius: 0 0 2px 2px;
     border-radius: 0 0 2px 2px;
     -webkit-box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
     box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
     display: none;
     }
    
     table td[data-attribute] .glyphicon-pencil {
     margin-left: 5px;
     opacity: 0;
     }
    
     table td[data-attribute]:hover .glyphicon-pencil {
     opacity: 0.7;
     }
    </style>{% fetchxml contactList %}
    <fetch version="1.0" mapping="logical">
     <entity name="contact">
     <attribute name="fullname"></attribute>
     <attribute name="firstname"></attribute>
     <attribute name="lastname"></attribute>
     <attribute name="contactid"></attribute>
     <attribute name="emailaddress1"></attribute>
     <attribute name="telephone1"></attribute>
     <order attribute="contactid" descending="false"></order>
     </entity>
    </fetch>
    {% endfetchxml %}
    <script>
     //Add the contact data in json object
     var contactList = [
     {% for entity in contactList.results.entities %}
     {
     id: "{{entity.contactid}}",
     fullname: "{{entity.fullname}}",
     firstname: "{{ entity.firstname }}",
     lastname: "{{ entity.lastname }}",
     emailaddress1: "{{ entity.emailaddress1 }}",
     telephone1: "{{ entity.telephone1 }}"
     } {% unless forloop.last %}, {% endunless %}
     {% endfor %} 
    ];
     $(function () {
     //Web API ajax wrapper
     (function (webapi, $) {
     function safeAjax(ajaxOptions) {
     var deferredAjax = $.Deferred();
     shell.getTokenDeferred().done(function (token) {
     // Add headers for ajax
     if (!ajaxOptions.headers) {
     $.extend(ajaxOptions, {
     headers: {
     "__RequestVerificationToken": token
     }
     });
     } else {
     ajaxOptions.headers["__RequestVerificationToken"] = token;
     }
     $.ajax(ajaxOptions)
     .done(function (data, textStatus, jqXHR) {
     validateLoginSession(data, textStatus, jqXHR, deferredAjax.resolve);
     }).fail(deferredAjax.reject); //ajax
     }).fail(function () {
     deferredAjax.rejectWith(this, arguments); // On token failure pass the token ajax and args
     });
     return deferredAjax.promise();
     }
     webapi.safeAjax = safeAjax;
     })(window.webapi = window.webapi || {}, jQuery)
     // Notification component
     var notificationMsg = (function () {
     var $processingMsgEl = $('#processingMsg'),
     _msg = 'Processing...',
     _stack = 0,
     _endTimeout;
     return {
     show: function (msg) {
     $processingMsgEl.text(msg || _msg);
     if (_stack === 0) {
     clearTimeout(_endTimeout);
     $processingMsgEl.show();
     }
     _stack++;
     },
     hide: function () {
     _stack--;
     if (_stack <= 0) {
     _stack = 0;
     clearTimeout(_endTimeout);
     _endTimeout = setTimeout(function () {
     $processingMsgEl.hide();
     }, 500);
     }
     }
     }
     })();
     // Inline editable table component
     var webAPIExampleTable = (function () {
     var trTpl = '<% _.forEach(data, function(data){ %>' +
     '<tr data-id="<%=data.id%>" data-name="<%=data.fullname%>">' +
     '<% _.forEach(columns, function(col){ %>' +
     '<td data-attribute="<%=col.name%>" data-label="<%=col.label%>" data-value="<%=data[col.name]%>">' +
     '<%-data[col.name]%><i class="glyphicon glyphicon-pencil"></i>' +
     '</td>' +
     '<% }) %>' +
     '<td>' +
     '<button class="btn btn-default delete" type="submit"><i class="glyphicon glyphicon-trash" aria-hidden="true"></i></button>' +
     '</td>' +
     '</tr>' +
     '<% }) %>';
     var tableTpl = '<table class="table table-hover">' +
     '<thead>' +
     '<tr>' +
     '<% _.forEach(columns, function(col){ %>' +
     '<th><%=col.label%></th>' +
     '<% }) %>' +
     '<th>' +
     '<button class="btn btn-default add" type="submit">' +
     '<i class="glyphicon glyphicon-plus" aria-hidden="true"></i> Add Sample Record' +
     '</button>' +
     '</th>' +
     '</tr>' +
     '</thead>' +
     '<tbody>' + trTpl + '</tbody>' +
     '</table>';
     function getDataObject(rowEl) {
     var $rowEl = $(rowEl),
     attrObj = {
     id: $rowEl.attr('data-id'),
     name: $rowEl.attr('data-name')
     };
     $rowEl.find('td').each(function (i, el) {
     var $el = $(el),
     key = $el.attr('data-attribute');
     if (key) {
     attrObj[key] = $el.attr('data-value');
     }
     })
     return attrObj;
     }
     function bindRowEvents(tr, config) {
     var $row = $(tr),
     $deleteButton = $row.find('button.delete'),
     dataObj = getDataObject($row);
     $.each(config.columns, function (i, col) {
     var $el = $row.find('td[data-attribute="' + col.name + '"]');
     $el.on('click', $.proxy(col.handler, $el, col, dataObj));
     });
     //User can delete record using this button
     $deleteButton.on('click', $.proxy(config.deleteHandler, $row, dataObj));
     }
     function bindTableEvents($table, config) {
     $table.find('tbody tr').each(function (i, tr) {
     bindRowEvents(tr, config);
     });
     $table.find('thead button.add').on('click', $.proxy(config.addHandler, $table));
     }
     return function (config) {
     var me = this,
     columns = config.columns,
     data = config.data,
     addHandler = config.addHandler,
     deleteHandler = config.deleteHandler,
     $table;
     me.render = function (el) {
     $table = $(el).html(_.template(tableTpl)({ columns: columns, data: data })).find('table');
     bindTableEvents($table, { columns: columns, addHandler: addHandler, deleteHandler: deleteHandler });
     }
     me.addRecord = function (record) {
     $table.find('tbody tr:first').before(_.template(trTpl)({ columns: columns, data: [record] }));
     bindRowEvents($table.find('tbody tr:first'), config);
     }
     me.updateRecord = function (attributeName, newValue, record) {
     $table.find('tr[data-id="' + record.id + '"] td[data-attribute="' + attributeName + '"]').text(newValue);
     }
     me.removeRecord = function (record) {
     $table.find('tr[data-id="' + record.id + '"]').fadeTo("slow", 0.7, function () {
     $(this).remove();
     });
     }
     };
     })();
     //Applicaton ajax wrapper 
     function appAjax(processingMsg, ajaxOptions) {
     notificationMsg.show(processingMsg);
     return webapi.safeAjax(ajaxOptions)
     .fail(function (response) {
     if (response.responseJSON) {
     alert("Error: " + response.responseJSON.error.message)
     } else {
     alert("Error: Web API is not available... ")
     }
     }).always(notificationMsg.hide);
     }
     function addSampleRecord() {
     //Sample data to create a record - change as appropriate
     var recordObj = {
     cr6a3_firstname: "DJ",
     cr6a3_lastname: "Patil"
     
     };
     appAjax('Adding...', {
     type: "POST",
     url: "/_api/cr6a3_employee",
     contentType: "application/json",
     data: JSON.stringify(recordObj),
     success: function (res, status, xhr) {
     recordObj.id = xhr.getResponseHeader("entityid");
     //recordObj.fullname = recordObj.firstname + " " + recordObj.lastname;
     //table.addRecord(recordObj);
     }
     });
     return false;
     }
     function deleteRecord(recordObj) {
     var response = confirm("Are you sure, you want to delete \"" + recordObj.name + "\" ?");
     if (response == true) {
     appAjax('Deleting...', {
     type: "DELETE",
     url: "/_api/cr6a3_user(" + recordObj.id + ")",
     contentType: "application/json",
     success: function (res) {
     table.removeRecord(recordObj);
     }
     });
     }
     return false;
     }
     function updateRecordAttribute(col, recordObj) {
     var attributeName = col.name,
     value = recordObj[attributeName],
     newValue = prompt("Please enter \"" + col.label + "\"", value);
     if (newValue != null && newValue !== value) {
     appAjax('Updating...', {
     type: "PUT",
     url: "/_api/contacts(" + recordObj.id + ")/" + attributeName,
     contentType: "application/json",
     data: JSON.stringify({
     "value": newValue
     }),
     success: function (res) {
     table.updateRecord(attributeName, newValue, recordObj);
     }
     });
     }
     return false;
     }
     var table = new webAPIExampleTable({
     columns: [{
     name: 'firstname',
     label: 'First Name',
     handler: updateRecordAttribute
     }, {
     name: 'lastname',
     label: 'Last Name',
     handler: updateRecordAttribute
     }, {
     name: 'emailaddress1',
     label: 'Email',
     handler: updateRecordAttribute
     }, {
     name: 'telephone1',
     label: 'Telephone',
     handler: updateRecordAttribute
     }],
     data: contactList,
     addHandler: addSampleRecord,
     deleteHandler: deleteRecord
     });
     table.render($('#dataTable'));
     });
    </script>
    <div id="processingMsg" class="alert alert-warning" role="alert"></div>
    <div id="dataTable"></div>

     

     



     

    Screen Shot 2563-08-12 at 22.57.44.png
    Screen Shot 2563-08-12 at 22.57.59.png
    Screen Shot 2563-08-12 at 22.58.42.png
  • H V Profile Picture
    1,510 on at

    Hi @Dhananjay_Patil

    You should use plural name of entity in web api url, like "/api/cr6a3_employees".

     

    After above change, clear the cache and check. 

     

    --------------------------
    If you like this post, give a Thumbs up. Where it solved your query, Mark as a Solution so it can help other people!

  • Dhananjay_Patil Profile Picture
    175 on at

    Hello @hardikv 

    And which name of entity should be used in site setting ?

    using logical name of entity fields in site setting and Webapi calling is ok?

  • Dhananjay_Patil Profile Picture
    175 on at

    Hello @hardikv 

    Display Name: Employee
    Logical Name: cr6a3_employee
    Plural Name:Employees

    Site Settings added:

    Webapi/cr6a3_employee/enabled
    Webapi/cr6a3_employee/fields

    Used Plural name in url as 'employees' (tried with 'Employees' also but getting error)

    But still getting error 'Resource not found for the segment employees'

    function addSampleRecord() {
     //Sample data to create a record - change as appropriate
     var recordObj = {
     cr6a3_firstname: "DJ",
     cr6a3_lastname: "Patil"
     
     };
     appAjax('Adding...', {
     type: "POST",
     url: "/_api/employees",
     contentType: "application/json",
     data: JSON.stringify(recordObj),
     success: function (res, status, xhr) {
     recordObj.id = xhr.getResponseHeader("entityid");
     //recordObj.fullname = recordObj.firstname + " " + recordObj.lastname;
     //table.addRecord(recordObj);
     }
     });
     return false;
     }

     

    Screen Shot 2563-08-13 at 10.47.13.png
    Screen Shot 2563-08-13 at 10.40.35.png
  • H V Profile Picture
    1,510 on at

    Hi @Dhananjay_Patil

    Please change url in above code with "/api/cr6a3_employees" this url. 

  • Dhananjay_Patil Profile Picture
    175 on at

    Hello @hardikv 

    After doing same getting following error

    Web API is not available...

    New Url : /api/cr6a3_employees

  • Hongzheng_Qin Profile Picture
    2 on at

    You also should assign permission to api in Site Settings

    like:

    Hongzheng_Qin_0-1627876209714.png

     

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Pages

#1
11manish Profile Picture

11manish 50

#2
Valantis Profile Picture

Valantis 26

#3
omkarsupreme Profile Picture

omkarsupreme 24

Last 30 days Overall leaderboard