Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jesper Ordrup 1019 posts 1528 karma points MVP
    Mar 29, 2021 @ 12:49
    Jesper Ordrup
    0

    contentResource.getById warns when accessing

    If you call this method with an id that you dont have access to - then you are prompted with a red alert warning in Umbraco backend.

    When it fails we are thrown into a catch block and deals with it.

    Can the UI red alert warning be avoided somehow? Any undocumented parameters?

    contentResource.getById(1234)
    .then(function(content) {
       var myDoc = content;       
    })
    .catch(function(error) ({
       //we deal with the error ourselves.
    });
    
  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Mar 31, 2021 @ 18:01
    Anders Bjerner
    100

    Hi Jesper,

    Umbraco listen for either a HTTP header or in the configuration object for the request to the API:

    https://github.com/umbraco/Umbraco-CMS/blob/34e80d86e8c0b754f6b7a02e307f53cb32806bbe/src/Umbraco.Web.UI.Client/src/common/interceptors/security.interceptor.js#L47

    You may be able to find a quirky way to set the HTTP response header, but you then probably be able to distinguish request coming from your controller and requests coming for elsewhere in Umbraco.

    The getById method doesn't let you specify your own configuration, so you won't be able do it this way either - at least not directly:

    But you could copy the implementation of the getById function:

    https://github.com/umbraco/Umbraco-CMS/blob/34e80d86e8c0b754f6b7a02e307f53cb32806bbe/src/Umbraco.Web.UI.Client/src/common/resources/content.resource.js#L372

    This means turning something like:

    function (id) {
                return umbRequestHelper.resourcePromise(
                    $http.get(
                        umbRequestHelper.getApiUrl(
                            "contentApiBaseUrl",
                            "GetById",
                            { id: id })),
                    'Failed to retrieve data for content id ' + id)
                    .then(function (result) {
                        return $q.when(umbDataFormatter.formatContentGetData(result));
                    });
            }
    

    into:

    function getById(id, config) {
                return umbRequestHelper.resourcePromise(
                    $http.get(
                        umbRequestHelper.getApiUrl(
                            "contentApiBaseUrl",
                            "GetById",
                            { id: id }), config),
                    'Failed to retrieve data for content id ' + id)
                    .then(function (result) {
                        return $q.when(umbDataFormatter.formatContentGetData(result));
                    });
            }
    

    Remember to inject the different parts via DI.

    When using your own getById function, you can then call it like getById(1234, { umbIgnoreErrors: true }).

    I haven't tested this code just now, but I'd reckon it should work 😉

  • Jesper Ordrup 1019 posts 1528 karma points MVP
    Jun 04, 2021 @ 13:38
    Jesper Ordrup
    0

    Hi Anders,

    Thanks for your help. I was just revisiting the problem and re-found your post which I was so sure that I had marked as solution.

    In the end my troubles were also related to

    umbRequestHelper.resourcePromise( $http.get(umbRequestHelper.getApiUrl('mediaApiBaseUrl', 'GetById', [{ id: id }])), 'Failed to retrieve data for media id ' + id);
    

    which luckily also accepts the same parameter { umbIgnoreErrors: true }

    umbRequestHelper.resourcePromise( $http.get(umbRequestHelper.getApiUrl('mediaApiBaseUrl', 'GetById', [{ id: id }]), { umbIgnoreErrors: true }), 'Failed to retrieve data for media id ' + id);
    

    It solved my problem.

    Thanks
    Jesper

Please Sign in or register to post replies

Write your reply to:

Draft