C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Javascript to get Logged in USER info for Microsoft CRM 3.0 and 4.0


Posted Date: 19 Aug 2008    Resource Type: Articles    Category: Microsoft Dynamics

Posted By: Ashwinikumar       Member Level: Bronze
Rating:     Points: 10



With Microsoft CRM 3.0 and 4.0 making there way in the market, we need ways to get the relevant information faster with less coding efforts.

One of the common requirement is to get LOGGED in CRM USER info via JavaScript.

Now MS CRM 3.0 and 4.0 have little different way of accessing this info, below are code snippets for both the versions.

Just copy paste and you should be good to go.


-------
CRM 3.0
-------



//Get Logged in CRM user ID
function GetUserId()
{
try
{
var Command = new RemoteCommand("SystemUser", "WhoAmI", "/MSCRMServices/");
var Result = Command.Execute();

if (Result.Success)
{
return Result.ReturnValue.UserId;
}
}
catch(e)
{
alert("Error while fetching User details.");
}
return null;
}




-------
CRM 4.0
-------




function GetLoggedUserName()
{
//Create the XML that will fetch the required info.
//You can inspect this web service call using a tool called FIDDLER.
var XMLRequest = "" +
"<?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>fullname</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1:Distinct>false</q1:Distinct>" +
" <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>" +
"";



try
{

//Create Http request object
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", XMLRequest.length);
xmlHttpRequest.send(XMLRequest);

//Store the response which would be XML
var Result = xmlHttpRequest.responseXML;

/*
The return is of type "BusinessEntity" if you were using similar code one server side.
Hence we need to select node of type "BusinessEntity"

In our case It should be not more one than one node
*/
var BusinessEntityNodes = Result.selectNodes("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");

// Check If data was retrived
if (BusinessEntityNodes.length != 0)
{


var BusinessEntityNode = BusinessEntityNodes[0];
var SystemUserId = BusinessEntityNode.selectSingleNode("q1:systemuserid");
var FullName = BusinessEntityNode.selectSingleNode("q1:fullname");


var SystemUserId = (SystemUserId == null) ? null : SystemUserId.text;
var FullName = (FullName == null) ? null : FullName.text;



}
return FullName;
}
catch (e)
{
alert(e.message);
}



}
alert(GetLoggedUserName());





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Ms crm 4  .  Ms crm 3  .  Javascript  .  Get Logged in USER  .  Crm 4  .  Crm 3  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Previous Resource: Disable CRM 3.0 TABS Completely
Return to Discussion Resource Index
Post New Resource
Category: Microsoft Dynamics


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

fax server

Contact Us    Privacy Policy    Terms Of Use