I've created a custom entity and I would like to fill a custom attribute
with the name of the logged on user. This is probably the same information
as the Created by, or the Owner.

I'm planning to do this in the On_Load Event of the form.

I just don't know where to get this information. Where can i find such
information ?

Kay

RE: CRM 4.0, Getting the logged on user by Bertil

Bertil
Fri Feb 08 07:17:03 CST 2008

Hi,

Try this script (it worked for me):

function GetCurrentUserInfo()
{
var SERVER_URL = "http://CRM";
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
xmlhttp.open("POST", "/mscrmservices/2007/crmservice.asmx", false);
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

xmlhttp.setRequestHeader("SOAPAction",
"http://schemas.microsoft.com/crm/2007/WebServices/Execute");
var soapBody = "<soap:Body>"+
"<Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
"<Request xsi:type='WhoAmIRequest' />"+
"</Execute></soap:Body>";
var soapXml = "<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'>";
soapXml += GenerateAuthenticationHeader();
soapXml += soapBody;
soapXml += "</soap:Envelope>";

xmlhttp.send(soapXml);
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(xmlhttp.responseXML.xml);
var userid = xmlDoc.getElementsByTagName("UserId")[0].childNodes[0].nodeValue;
return userid;
}

function GetAttributeValueFromID(sEntityName, sGUID, sAttributeName)
{
var sXml = "";
var oXmlHttp = new ActiveXObject("Msxml2.XMLHTTP.6.0");

//set up the SOAP message
sXml += "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
sXml += "<soap:Envelope
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"";
sXml += " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
sXml += " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
sXml += "<soap:Body>";
sXml += "<entityName
xmlns=\"http://schemas.microsoft.com/crm/2006/WebServices\">" + sEntityName +
"</entityName>";
sXml += "<id
xmlns=\"http://schemas.microsoft.com/crm/2006/WebServices\">" + sGUID +
"</id>";
sXml += "<columnSet
xmlns=\"http://schemas.microsoft.com/crm/2006/WebServices\"";
sXml += " xmlns:q=\"http://schemas.microsoft.com/crm/2006/Query\"";
sXml += " xsi:type=\"q:ColumnSet\"><q:Attributes><q:Attribute>" +
sAttributeName + "</q:Attribute></q:Attributes></columnSet>";
sXml += "</soap:Body>";
sXml += "</soap:Envelope>";

// send the message to the CRM Web service
oXmlHttp.open("POST", SERVER_URL +
"/MsCrmServices/2006/CrmService.asmx",false);
oXmlHttp.setRequestHeader("SOAPAction",
"http://schemas.microsoft.com/crm/2006/WebServices/Retrieve");
oXmlHttp.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
oXmlHttp.setRequestHeader("Content-Length", sXml.length);
oXmlHttp.send(sXml);

// retrieve response and find attribute value
// retrieve the given attribute name in any XML namespace
var result =
oXmlHttp.responseXML.selectSingleNode("//*[local-name()=\"" + sAttributeName
+"\"]");

if (result == null)
{
alert("***Error***");
return "";
}
else
{
alert(result.text);
return result.text;
}
}

var userid = GetCurrentUserInfo();
GetAttributeValueFromID('systemuser', userid, 'fullname');


Good luck,

Bertil

"Kay-Christian Wessel" wrote:

> I've created a custom entity and I would like to fill a custom attribute
> with the name of the logged on user. This is probably the same information
> as the Created by, or the Owner.
>
> I'm planning to do this in the On_Load Event of the form.
>
> I just don't know where to get this information. Where can i find such
> information ?
>
> Kay
>
>

Re: CRM 4.0, Getting the logged on user by Kay-Christian

Kay-Christian
Fri Feb 08 18:20:32 CST 2008

Thanks Bertil,

I found another solution which was better for me :


var lookupItem = new Array;
var strName = "";

lookupItem = crmForm.all.ownerid.DataValue;

if (lookupItem[0] != null)
{
strName = lookupItem[0].name;
crmForm.all.new_name.DataValue = strName;
}

Kay