问题
We have Dynamics CRM and a webform which is loaded from the ribbon, essentially inside an iframe.
How do we get the logged on user? On the top right, is my name and image as logged in via Active Directory. However, if I do something like:
var UserID = window.parent.Xrm.Page.context.getUserId();
or in C#:
UserPrincipal user = UserPrincipal.Current;
lblUser.Text = user.SamAccountName;
then we get the generic user that CRM is configured to use.
If I do a right click on the entire form and go "View Source", I can see this:
var USER_NAME = 'Rodney Ellis';
In Chrome's developer tools I can run this from the Console, and my name appears:
alert(USER_NAME);
But when I try to access it from javascript in the code it says it can't be found: Uncaught ReferenceError: USER_NAME is not defined
How can I get the Username from inside the aspx webform, either by c# or js? Cross-side scripting being blocked has stopped a lot of the easy ways, hence why we're looking for a work-around.
回答1:
The below code should give you what you want. Read more
Xrm.Page.context.getUserName();
But based on popup or inline embedded iframe, you have append in front.
window.parent.Xrm.Page.context.getUserName();
window.opener.Xrm.Page.context.getUserName();
回答2:
In one of the all time great hacks ... we got around the problem by embedding another iFrame into a web resource!
So the web resource can call Xrm.Page.context.getUserName() and then concatenate that to the querystring which we then pass into an iFrame. because CRM thinks the iframe is just one control, it allows all the webform commands taking place inside of it and doesn't give any 500 errors.
来源:https://stackoverflow.com/questions/59572835/accessing-the-logged-in-crm-user-from-custom-page