Another way to get entity name and id in PCF in Model-Driven app
When I started with PCF one of the first question that I have was how to retrieve the current record id. However, to my surprise, there was no out of the box supported solution. Some proposed to use old Xrm.Page. Some suggested using context.page and get properties from there (I used this in a couple of my components). Some proposed to use context.mode.contextInfo and get properties from there.
Unfortunately what unites all of the methods presented above is that they are not supported and undocumented, which means they might be deprecated in the upcoming releases.
But there might be a solution that sort of supported and will work (only for Model-Driven app),
Let’s step back and think what else do we have for our disposal that contains necessary info and are available through browser methods only? If you guessed page URL you are right.
Typical URL for the record for Model-Driven app looks like this:
If you check your record URL it might look a bit different (might include additional parameter like formid), but in general all elements described above will be present. This means that we can extract everything we need from the URL and not depend on unsupported methods that might break.
Below code will extract all necessary data and return you the object that contains all available parameters from the URL.
The method described in this article will help you to avoid problems with unsupported methods until the PCF team will introduce supported one. However, this method will only work for Model-Driven app. Hope you find this article useful.