cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
HoracioCrespo
Frequent Visitor

shell is not defined in web api update

Hi, 

 

I'm trying to make an update from a web page in a table in dataverse from my custom portal when I select a panel, but I'm getting always the error: "shell is not defined" in the wrapper function.

 

 

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="icon" type="image/x-icon" href="../favicon.ico">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="~/style.css">
    <script src="~/jquery.min.js"></script>

    <script>
        (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)
    </script>

    <script>
        $(document).ready(function () {
           
            sidepanel.on('click', '.js-evt-save', function () {
                
                var dataObject = {
                    "new_field1": 1,
                    "new_field2": 2
                };

                webapi.safeAjax({
                    type: "PATCH",
                    url: "/_api/new_myentity(549b6ebf-00f6-eb11-94ee-000d3ab2493f)",
                    contentType: "application/json",
                    data: JSON.stringify(dataObject),
                    success: function (res) {
                        debugger;
                        console.log(res)
                    },
                    error: function (xhr) {
                        debugger;
                    }
                });
            })
			})
    </script>

 

 

HoracioCrespo_0-1629755981104.png

 

the portal version is: 9.3.7.14 and I already created the web role, entity permission and settings. 

I tried with the wrapper inside the $(document).ready but nothing works. 

I'm not logged in the portal, I'm as anonymous. Can be it a problem ?

 

Has anyone had the same problem ?

 

Thanks in advance

 

3 REPLIES 3
ragavanrajan
Super User
Super User

Hi @HoracioCrespo 

 

In your code, there is a method "

validateLoginSession

 

which indicates it requires login. 

 

However, all your queries clearly answered in @OOlashyn  blog 

 

https://www.dancingwithcrm.com/powerapps-portal-web-api-deep-dive/ 

 

I would strongly recommend you to read especially about shell function and how Web API works. 

Hope it gives you some clarity. 


------------

If you like this post, give it a Thumbs up. Where it solved your request, Mark it as a Solution to enable other users to find it.

Hi @HoracioCrespo ,

As @ragavanrajan  mentioned I wrote a deep dive article about Web API so check it out to find out more.

Web API wrapper doesn't care if you anonymous or authorized user. All it cares about is proper entity permissions when executing requests (so if you don't have them you will receive an error). This error looks more like a problem with portal as shell is provided by it. What I advise you to do is create a web template that will have only a script tag with web api wrapper. Then check if a page with that template throws the error as well. If it does restart the portal and check again. If the issue still persists please open a ticket with Microsoft support. If the problem goes away try again to add the web api to your main template.

----------------------------------------------------
If you find this post helpful consider marking it as a solution to help others find it.
vduggineni91
New Member

Hi, Could you able to figure out this issue? I am in the same boat says shell is not defined?

 

vduggineni91_0-1645655808158.png

 

Helpful resources

Announcements
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Top Solution Authors
Users online (4,329)