Tech Support > Microsoft Windows > Windows CRM > Lookup field for Current User
Lookup field for Current User
Posted by Chris Brown on June 6th, 2008


Hello,

I am needing to write some javascript (for onload) that will lookup the
current logged on user, then pull a couple fields from that users entity to
populate part of a http string used for an IFrame. I already have the
javascript setup for the IFrame and it is working, I just need to make it
dynamic depending on who is logging in.

Thanks in advance for the help.

Respectfully,

Chris

Posted by Michael Höhne on June 9th, 2008


Where is your code running? As part of the CRM web (below the /isv folder)?
If yes and if it is an ASP.NET application, you can use the SDK assemblies
to impersonate and get to all information you need directly from your code.

If this is not an option, then you can always execute a WhoAmIRequest
directly from the JavaScript code, or do a Retrieve request where the only
condition is "systemuserid equals the current user id".

Some examples: http://www.stunnware.com/crm2/topic.aspx?id=JS8. Note that
the tools reference in this article refer to CRM 3.0. If you are using CRM
4.0, look at http://www.stunnware.com/crm2/topic.aspx?id=Framework2 and
http://www.stunnware.com/crm2/topic....=JSWebService2 instead.


--
Michael Höhne, Microsoft Dynamics CRM MVP

CRM Blog: http://www.stunnware.com/?area=blog

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

"Chris Brown" <ChrisBrown@discussions.microsoft.com> schrieb im Newsbeitrag
news:E5F5E448-31C8-401A-BDD6-DB06E3C54DBF@microsoft.com...


Posted by Chris Brown on June 9th, 2008


Hi Michael,

Thanks for your reply. Unfortunately I am pretty new to scripting so I
don't exactly understand your answer. Here is the javascript that I am
currently using in the Onload event, but it is not dynamic.

{
if (crmForm.all.dv_iirplantid.DataValue != null)
{document.all.IFRAME_PECweb.src="http://www.industrialinfo.com/loginprocess.jsp?loginuser=username&loginpassword= password&productNumber=MEMBER&colcDestination=/plant/showPlantReport.jsp?PLANT_ID="
+ crmForm.all.dv_iirplantid.value + "&style=pecweb";}
}

As you can see above, I am hardcoding the username and password into the
string, but I need it to pull from the current users entity which will
contain their specific username and password.

Thanks in advance for your help!

Respectfully,

Chris Brown



"Michael Höhne" wrote:

Posted by Chris Brown on June 13th, 2008


Thanks Michael,

I got it figured out.

Here is the entire code.

{
var xml = "" +
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
GenerateAuthenticationHeader() +
" <soap:Body>" +
" <RetrieveMultiple
xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\"
xsi:type=\"q1:QueryExpression\">" +
" <q1:EntityName>systemuser</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" +
" <q1:Attribute>systemuserid</q1:Attribute>" +
" <q1:Attribute>dv_iirusername</q1:Attribute>" +
" <q1:Attribute>dv_iirpassword</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1istinct>false</q1istinct>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>systemuserid</q1:AttributeName>" +
" <q1:Operator>EqualUserId</q1:Operator>" +
" </q1:Condition>" +
" </q1:Conditions>" +
" </q1:Criteria>" +
" </query>" +
" </RetrieveMultiple>" +
" </soap:Body>" +
"</soap:Envelope>" +
"";
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction","http ://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);

var resultXml = xmlHttpRequest.responseXML;

var entityNodes =
resultXml.selectNodes("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");

for (var i = 0; i < entityNodes.length; i++) {

// Access the current array element
var entityNode = entityNodes[i];

// Attributes are child nodes of the BusinessEntity node. Use
selectSingleNode to return
// the first occurrence of a named node. The selectNodes method we used
before returns all
// matching nodes, but as an attribute name only occurs once,
selectSingleNode is easier to use.
// Use the same namespace alias (q1) you have specified in the query.
var systemuseridNode = entityNode.selectSingleNode("q1:systemuserid");
var dv_iirusernameNode = entityNode.selectSingleNode("q1:dv_iirusername");
var dv_iirpasswordNode =
entityNode.selectSingleNode("q1:dv_iirpassword");

// Always check for null values. If an attribute is set to null, it is
not contained in the
// resulting XML. And accessing systemuseridNode.text when
systemuseridNode is null leads to
// a runtime error.
var systemuserid = (systemuseridNode == null) ? null :
systemuseridNode.text;
var dv_iirusername = (dv_iirusernameNode == null) ? null :
dv_iirusernameNode.text;
var dv_iirpassword = (dv_iirpasswordNode == null) ? null :
dv_iirpasswordNode.text;}

}

{
if (crmForm.all.dv_iirplantid.DataValue != null)
{document.all.IFRAME_PECweb.src="http://www.industrialinfo.com/loginprocess.jsp?loginuser="
+ dv_iirusername + "&loginpassword=" + dv_iirpassword +
"&productNumber=MEMBER&colcDestination=/plant/showPlantReport.jsp?PLANT_ID="
+ crmForm.all.dv_iirplantid.value + "&style=pecweb";}
}


Thanks,

Chris Brown

"Michael Höhne" wrote: