Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

need to change document.getElementById to a supported method

Posted on by 15

Hi,

I am using the following code in a webresource which can be launched from the Sitemap and it is used to show a canvas App. and i am geting the Canvas AppID dynamically then pass it into the src parameter of the iframe tag:





    
    
    
    



    
 

but the problem arises when I perform the PowerApps Checker using AppSource rules, and it fails to pass due to the 

document.getElementById method which is not supported in Dynamics.
can any one help me to change it to a supported method?
thanks,
Mohammad
  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: need to change document.getElementById to a supported method

    Hi Mohammad,

    Like I said before, use of getElementByID to access any DOM element within your web resource content is supported.  If you have an html element lets say an input tag inside your web resource, you cannot use Xrm client side libraries to access that. You either have to use vanilla JavaScript function like getElementbyId or jquery or other libraries to access that element. Please see the following from the docs page

    pastedimage1600108968488v1.png

    https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/use-javascript

    The error you are getting is pointing to the same paragraph and the rule that tirggers this error is : 

     {
        "description": "Do not directly access the HTML Document Object Model (DOM) of CDS for Apps application pages or entity forms",
        "include": true,
        "code": "web-avoid-dom-form",
        "summary": "Do not directly access the HTML Document Object Model (DOM) of CDS for Apps application pages or entity forms",
        "componentType": 0,
        "primaryCategory": 8,
        "severity": 4
      },

    Clearly your script is not trying to access DOM element of CRM application pages or the entity form. You script is trying to access an element within your web resource content. This is in my view is a false negative. I assume you are not planing to submit your work to AppSource, and if thats the case, I would use the Solution Checker rule-set. I tried your webresource with solution checker rule-set and it does not complaint. 

    If your work has to be submitted to AppSource, you can still submit the package and the team that reviews  code will ignore any false positives / negatives.

    Just wanted to show you an example web resource code that is part of the CRM SDK does use document.getElementById to display content within the web resource

    pastedimage1600121275551v3.png

    pastedimage1600121464284v4.png

    Hope this helps

  • Suggested answer
    RE: need to change document.getElementById to a supported method

    Hi Mohammad,

    Indeed, using document.getElementById is not supported.

    If you want to load a canvas app dynamically into a web resource, you can use the setSrc method provided by Dynamics and pass the correct URL: docs.microsoft.com/.../setsrc

    Maybe this could work in your implementation.

    Thank you!

  • mokhawaja Profile Picture
    mokhawaja 15 on at
    RE: need to change document.getElementById to a supported method

    Kokulan, thanks for your prompt response. you are totally right, testing using Power Apps checker in the Power Apps maker portal, shows zero errors, but i am using DevOps to export and deploy my solutions, and I am using the Power Apps Checker task from he Power Platform tasks,

    SharedScreenshot.jpg

    which as you can see in the screenshot, you can choose the stricter rule set of AppSource Certification, then it produces a sarif file, and this checker sets the alarm on for 

    document.getElementById
    here is the screenshot:
    4762.SharedScreenshot2.jpg
    so I am not sure if I should leave it as is and ignore it if there is a supported way!
    thanks,
    Mohammad
  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: need to change document.getElementById to a supported method

    Hi

    An html webresource can use getElementByID to access the elements within the webresource content.  Your example above is not trying to access any elements on the form so should not get the unsupported error.

    I ran the solution checker with your HTML and the tool does not complaint about the getElementByID, please see below

    pastedimage1599919817257v1.png

    Could you please double check the solution checker result, it could be complaining about other web resources?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans