Access MS CRM 2011 with IP address ,can get Odata Java Script Query Results

Access MS CRM 2011 with IP address ,can get Odata Java Script Query Results:


Some times we may access the CRM with IP address as below.AccessCrmWithIp


At this point of time CRM Javascript  Xrm.Page.context.getServerUrl() cant get the Server Url. To over come this use the following Java Script code to get the Server Url.

var serverUrl = document.location.protocol + “//” + + “/” + Xrm.Page.context.getOrgUniqueName(); 

This will work fine for the both IP and CRM Serverl Urls.

Please have a glance the below example.

Below i am commenting var serverUrl = Xrm.Page.context.getServerUrl();

function OdataRetrieveMultipleUsingJquery(ODataFilterQuery) {
//var serverUrl = Xrm.Page.context.getServerUrl();

var serverUrl = document.location.protocol + “//” + + “/” + Xrm.Page.context.getOrgUniqueName();
var Odata_OrgName = “/xrmservices/2011/OrganizationData.svc”;
var oDataUri = serverUrl + Odata_OrgName + ODataFilterQuery;
var jSonArray = new Array();

type: “GET”,
contentType: “application/json; charset=utf-8″,
datatype: “json”,
url: oDataUri,
async: false,
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader(“Accept”, “application/json”);
success: function (data, textStatus, XmlHttpRequest) {
if (data && data.d != null) {
error: function (XmlHttpRequest, textStatus, errorThrown) {
alert(“Invalid Record Selection or No Record found with the Search Item”);

return jSonArray;


Hope this helps you.



Route a QueueItem from one Queue to Another Queue using SOAP Code in MS CRM 2011

Route a QueueItem from one Queue to Another Queue using SOAP Code in MS CRM 2011

Some times we may need to route manually queue item to our personal queue on ribbon button click.

Call the below function on custom ribbon button to assign a queue item to your personal Queue or any other queue by passing the Source Queue guid , Destination queue guid and Queueitem guid.

function AssignEmialToQueueUsingEmailGuid(sourceQueueid ,SystemUserPersonalQueueGuid, QueueItemGuid) {
var header = Xrm.Page.context.getAuthenticationHeader();

var xml = “” +
“<?xml version=\”1.0\” encoding=\”utf-8\”?>” +
“<soap:Envelope xmlns:soap=\”\” xmlns:xsi=\”\” xmlns:xsd=\”\”>” +
header +
” <soap:Body>” +
” <Execute xmlns=\”\”>” +
” <Request xsi:type=\”RouteRequest\”>” +
” <Target xsi:type=\”TargetQueuedEmail\”>” +
” <EntityId>” + QueueItemGuid + “</EntityId>” +
” </Target>” +
” <SourceQueueId>” + sourceQueueid + “</SourceQueueId>” +
” <RouteType>Queue</RouteType>” +
” <EndpointId>” + SystemUserPersonalQueueGuid + “</EndpointId>” +
” </Request>” +
” </Execute>” +
” </soap:Body>” +
“</soap:Envelope>” +

var xmlHttpRequest = new ActiveXObject(“Msxml2.XMLHTTP”);

xmlHttpRequest.Open(“POST”, “/mscrmservices/2007/CrmService.asmx”, false);
xmlHttpRequest.setRequestHeader(“SOAPAction”, “;);
xmlHttpRequest.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8″);
xmlHttpRequest.setRequestHeader(“Content-Length”, xml.length);

var resultXml = xmlHttpRequest.responseXML;
alert(“A Queue Item is Successfully Routed to Your Queue”);


Activate/Deactivate CRM Entity Record using JavaScript SOAP Code

Activate/Deactivate CRM Entity Record using JavaScript

function ChangeRecordStatus(RECORD_ID, stateCode, statusCode) {
// create the SetState request
var request = “<s:Envelope xmlns:s=\”\”>”;
request += “<s:Body>”;
request += “<Execute xmlns=\”\” xmlns:i=\”\”>”;
request += “<request i:type=\”b:SetStateRequest\” xmlns:a=\”\” xmlns:b=\”\”>”;
request += “<a:Parameters xmlns:c=\”\”>”;
request += “<a:KeyValuePairOfstringanyType>”;
request += “<c:key>EntityMoniker</c:key>”;
request += “<c:value i:type=\”a:EntityReference\”>”;
request += “<a:Id>” + RECORD_ID + “</a:Id>”;
request += “<a:LogicalName>phonecall</a:LogicalName>”;
request += “<a:Name i:nil=\”true\” />”;
request += “</c:value>”;
request += “</a:KeyValuePairOfstringanyType>”;
request += “<a:KeyValuePairOfstringanyType>”;
request += “<c:key>State</c:key>”;
request += “<c:value i:type=\”a:OptionSetValue\”>”;
request += “<a:Value>” + stateCode + “</a:Value>”;
request += “</c:value>”;
request += “</a:KeyValuePairOfstringanyType>”;
request += “<a:KeyValuePairOfstringanyType>”;
request += “<c:key>Status</c:key>”;
request += “<c:value i:type=\”a:OptionSetValue\”>”;
request += “<a:Value>” + statusCode + “</a:Value>”;
request += “</c:value>”;
request += “</a:KeyValuePairOfstringanyType>”;
request += “</a:Parameters>”;
request += “<a:RequestId i:nil=\”true\” />”;
request += “<a:RequestName>SetState</a:RequestName>”;
request += “</request>”;
request += “</Execute>”;
request += “</s:Body>”;
request += “</s:Envelope>”;

//send set state request

type: “POST”, contentType: “text/xml;charset=utf-8″, datatype: “xml”, url: Xrm.Page.context.getServerUrl() + “/XRMServices/2011/Organization.svc/web”, data: request,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader(“Accept”, “application/xml, text/xml, */*”);
XMLHttpRequest.setRequestHeader(“SOAPAction”, “;);
}, success: function (data, textStatus, XmlHttpRequest) { Xrm.Page.ui.close(); }, error: function (XMLHttpRequest, textStatus, errorThrown) {


Call the Above function and pass the Record Guid , Statecode and status code of the Record as parameters as shown below.




Open Existing CRM Record Using Java Script

Open Existing CRM Record Using Script

To Open the exisiting CRM Record , pass the Entity Name and Record guid to the below function , it will open the specified record.

//Open an existing Custom record
function OpenExistingEntityRecord(EntityName, RecordGuid) {
Xrm.Utility.openEntityForm(EntityName, RecordGuid);

Call the above funciion is as folows.


You may call this function any event in the CRM


Hide the Navigation Pane items in MS CRM 2011

Hide Activities and Closed Activities in the Navigation Pane in MS CRM 2011 Form On load

function hideActivitiesAndClosedActivitiesInNavItems()
Xrm.Page.ui.navigation.items.forEach(function (item, index)
var itemLabel = item.getLabel();

if (itemLabel == “Activities” || itemLabel == “Closed Activities”)


You may use the above code to hide all navigation items like marketing , Audit History etc.

Make sure your function must call on Form onLoad event.



Preventing/Cancelling CRM Form onSave event

Preventing/Cancelling CRM Form onSave event

write the below code end of the result.

event.returnValue = false;

return false;

or this may help you the same

In the ValidateData method make sure you have a parameter defined to receive the Execution Object.

function ValidateData(ExecutionObj){
//Place all your data validation here
//In case data is not valid call the below method
// The getEventArgs() method returns an object with methods to manage the Save event.
// The preventDefault() method cancels the save operation



Disable MS D CRM Form Ribbon Buttons using Java Script on Form Load

Disable MS D CRM Form Ribbon Buttons using Java Script on Form Load.

For Eg. I am trying to disable SAVE , SAVE & NEW , SAVE & CLOSE buttons on Form Load Event


function DisableSaveSaveAndCloseSaveAndNewButtons() {

var TelephoneNumber = Xrm.Page.getAttribute(“telephone”).getValue();

if (TelephoneNumber != “” || TelephoneNumber != null) {
//Disable delete button
var Save = top.document.getElementById(“phonecall|NoRelationship|Form|Mscrm.Form.phonecall.Save-Large”);
var SaveAndClose = top.document.getElementById(“phonecall|NoRelationship|Form|Mscrm.Form.phonecall.SaveAndClose-Large”);
var SaveAndNew = top.document.getElementById(“phonecall|NoRelationship|Form|Mscrm.Form.phonecall.SaveAndNew-Medium”);

Save.disabled = true;
SaveAndClose.disabled = true;
SaveAndNew.disabled = true;



Set Default View in Party Lookup (Regarding)Type in MS CRM 2011

Set Default View in Party Lookup (Regarding)Type in MS CRM 2011

Use the following code to set the Regarding Lookup to Incident instead of Account and set the default view.

document.getElementById(“regardingobjectid”).setAttribute(“defaulttype”, “112”);
Xrm.Page.getControl(“regardingobjectid”).setDefaultView(“Guid of the View to be displayed”);



Default Regarding Lookup to Incident in CRM 2011 in Email and Phone Call

Default Regarding Lookup to Incident in CRM 2011 in Email and Phone Call

Use the following code to set the Regarding Lookup to Incident instead of all party list types.

document.getElementById(“regardingobjectid”).setAttribute(“defaulttype”, “112”);


crmForm.all.regardingobjectid.setAttribute(“defaulttype”, “112”);

you may use either type of codes . . Working fine.

if you disable all the types and show the only one entity type use the following code on page load

document.getElementById(“regardingobjectid”).setAttribute(“defaulttype”, “112”);
document.getElementById(“regardingobjectid”).setAttribute(“lookuptypes”, “112”);
document.getElementById(“regardingobjectid”).setAttribute(“lookuptypeIcons”, “/_imgs/ico_18_112.gif”);


using CRM Form Code is as Follows"defaulttype", "112");"lookuptypes", "112");"lookuptypeIcons", "/_imgs/ico_18_112.gif");

You may achieve same functionality using JQuery is as follows and refer to page JQuery 1.4.1 and json2 files in your Form Properties

$("#regardingobjectid").attr("defaulttype", "112");
    $("#regardingobjectid").attr("lookuptypes", "112");
    $("#regardingobjectid").attr("lookuptypeIcons", "/_imgs/ico_18_112.gif");


Trigger WorkFlow Using JavaScript – MS CRM 2011

Trigger WorkFlow Using JavaScript

var workflowGuid = “<Work Flow GUid>”;
var entityId = “<Record Guid>”;

function TriggerWorkflow(entityId,workflowGuid) {

/*Generate Soap Body.*/

var soapBody = “<soap:Body>” +
” <Execute xmlns=’’>&#8221; +
” <Request xsi:type=\’ExecuteWorkflowRequest\’>” +
” <EntityId>” + entityId + “</EntityId>” +
” <WorkflowId>” + workflowGuid + “</WorkflowId>” +
” </Request>” +
” </Execute>” +

/*Wrap the Soap Body in a soap:Envelope.*/
var soapXml = “<soap:Envelope ” +
” xmlns:soap=’; ” +
” xmlns:xsi=’; ” +
” xmlns:xsd=’’>&#8221; +
GenerateAuthenticationHeader() +
soapBody +

/* Create the XMLHTTP object for the execute method.*/
var xmlhttp = new ActiveXObject(“Msxml2.XMLHTTP”);“POST”, “/MSCRMservices/2007/crmservice.asmx”, false);
xmlhttp.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8″);
xmlhttp.setRequestHeader(“SOAPAction”, “;);

/* Send the XMLHTTP object. */
alert(“The workflow has been triggered successfully”);