Thursday, January 31, 2008

Hide Tab , Button base on Security Role in Javascript in CRM 4.0

Dear,

Here is some code for Javascript in CRM 4.0 I am using , it will help you and cover your queries as many of you looking CRM 4.0 Javascript help.

// Check Current User Security Roles and hide tab / field / Button base on that

Here is some code for Javascript in CRM 4.0 I am using , it will help you to hide/ disable Tab, Field, Section using Javascript in CRM 4.0

// pass string of role like Sales Person,/ Administrator and check if that is role exist
function CheckCurrentRole()
{

//

var userXml = GetUserRoles();
if(userXml != null)
{
//select the node text
var allRoles = userXml.selectNodes("//BusinessEntity/q1:name");
if(allRoles != null)
{
for(int i = 0; i < text ="="" visibility = "hidden" position = "absolute" text ="="" lisdocument =" document.getElementsByTagName('LI');
}
j= j + 1;
}

}
}}}
}

function GetUserRoles(){

// define XML for retrieve query, for more ask me if you have to buid any query regarding
var xmlQuery = "" + "" + "" + GenerateAuthenticationHeader() + " " + " " + " " + " role" + " " + " " + " name" + " " + " " + " false" + " " + " " + " roleid" + " role" + " systemuserroles" + " roleid" + " Inner" + " " + " " + " systemuserid" + " systemuserroles" + " systemuser" + " systemuserid" + " Inner" + " " + " And" + " " + " " + " systemuserid" + " EqualUserId" + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + "" + "";
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);
// send xml query and get xml data from CRM in Javascript.
xmlHttpRequest.send(xmlquery);
var resultXml = xmlHttpRequest.responseXML;
// return xml for user role
return(resultXml);}





If you have to get current user info ( user id, organization id, etc)

// Get Current User Information in Javascript

function GetWhoAmI(){
var crmserver = http://crmserver/;

var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
xmlhttp.open("POST",crmserver + "/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 = ""+""+""+"";
var soapXml = "";s
oapXml += GenerateAuthenticationHeader();soapXml += soapBody;soapXml += "";
xmlhttp.send(soapXml);
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;

// Load XML and parse the nodes.
xmlDoc.loadXML(xmlhttp.responseXML.xml);
// Get Current User ID
var uid = xmlDoc.getElementsByTagName("UserId")[0].childNodes[0].nodeValue;
// Get Current Business Unit ID
var bid = xmlDoc.getElementsByTagName("BusinessUnitId")[0].childNodes[0].nodeValue;

// Get Current Organization ID Value
var oid = xmlDoc.getElementsByTagName("OrganizationId")[0].childNodes[0].nodeValue;

alert("Current User Id: " + uid + "\r\nBusiness Unit Id: " + bid + "\r\nOrganization Id: " + oid);
}



Contact me directly if you have any query ( mscrmexpert@gmail.com)


Regards,
Imran