function FormOnLoad() {
try {
if (Xrm.Page.data.entity.getId() == null) { return true; }
var contactId = Xrm.Page.data.entity.getId();
var queryUrl = "ContactSet(guid'" + contactId + "')"
var result = RequestODATA(queryUrl);
alert(result[0].FullName);
}
catch (ex) {
alert(ex.message);
}
}
function RequestODATA(query) {
try {
var xmlhttp = new XMLHttpRequest();
}
catch (e) {
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
xmlhttp.open("GET", prependOrgName("/xrmservices/2011/organizationdata.svc/") + query, false);
xmlhttp.send();
return GetReturnObject(xmlhttp.responseText, null, "");
}
function GetReturnObject (xmlText, linksArr, fatherNode) {
var returnObjectNSResolver = { m: "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata", d: "http://schemas.microsoft.com/ado/2007/08/dataservices" };
var Entities = new Array();
Entities.flError = {};
try {
var xml = XUI.Xml.LoadXml(xmlText.replace('xmlns="http://www.w3.org/2005/Atom"', ""));
var entrys = XUI.Xml.SelectNodes(xml, fatherNode + '/entry', null);
var entityName = GetEntityName(xml);
Entities.EntityName = entityName;
for (var i = 0; !IsNull(entrys), i < entrys.length; i++) {
var linksXML = entrys[i].getElementsByTagName('link');
var obj = new Object();
if (!IsNull(linksArr)) {
obj.LinksExpanded = GetLinks2Expand(entrys[i], linksArr);
}
var props = XUI.Xml.SelectSingleNode(entrys[i], "content/m:properties", returnObjectNSResolver);
for (var j = 0; j < props.childNodes.length; j++) {
if (props.childNodes[j].nodeType != 1)
continue;
var type = props.childNodes[j].getAttribute("m:type");
var propName = props.childNodes[j].tagName.replace("d:", "");
switch (IsNull(type) ? "" : type) {
case FieldTypes.int:
case FieldTypes.double:
case FieldTypes.long:
case FieldTypes.string:
case FieldTypes.datetime:
case FieldTypes.guid:
case FieldTypes.decimal:
var propValue = IsNull(XUI.Xml.GetText(props.childNodes[j])) ? "" : XUI.Xml.GetText(props.childNodes[j]);
eval("obj." + propName + " = null; var tempEval = null;");
tempEval = propValue;
var evalExpression = ("obj." + propName + " = tempEval");
break;
case FieldTypes.bool:
var propValue = IsNull(XUI.Xml.GetText(props.childNodes[j])) ? "false" : XUI.Xml.GetText(props.childNodes[j]);
if (propValue == "")
propValue = "false";
var evalExpression = "obj." + propName + " = " + FixedString(propValue) + ";";
break;
case FieldTypes.money:
case FieldTypes.optionSetValue:
var value = XUI.Xml.SelectSingleNode(props.childNodes[j], 'd:Value', returnObjectNSResolver);
var propValue = IsNull(value) ? "" : XUI.Xml.GetText(value);
break;
case FieldTypes.entityReference:
var id = IsNull(XUI.Xml.SelectSingleNode(props.childNodes[j], 'd:Id', returnObjectNSResolver)) ? "" : "'" + XUI.Xml.GetText(XUI.Xml.SelectSingleNode(props.childNodes[j], 'd:Id', returnObjectNSResolver)) + "'";
var logic = IsNull(XUI.Xml.SelectSingleNode(props.childNodes[j], 'd:LogicalName', returnObjectNSResolver)) ? "" : "'" + XUI.Xml.GetText(XUI.Xml.SelectSingleNode(props.childNodes[j], 'd:LogicalName', returnObjectNSResolver)) + "'";
var display = IsNull(XUI.Xml.SelectSingleNode(props.childNodes[j], 'd:Name', returnObjectNSResolver)) ? "" : "'" + FixedString(XUI.Xml.GetText(XUI.Xml.SelectSingleNode(props.childNodes[j], 'd:Name', returnObjectNSResolver))) + "'";
break;
}
eval(evalExpression);
}
Entities.push(obj);
}
if (Entities.length == 0) {
var error = xml.getElementsByTagName('error');
if (!IsNull(error[0])) {
Entities.flError.Message = XUI.Xml.GetText(XUI.Xml.SelectSingleNode(error[0], 'message', null));
Entities.flError.Code = 1;
}
else {
Entities.flError.Message = "Query with returned no results";
Entities.flError.Code = 2;
}
}
else {
Entities.flError.Message = "";
Entities.flError.Code = 0;
}
} catch (e) {
Entities.flError.Message = e.message;
Entities.flError.Code = 3;
}
return Entities;
}
function GetEntityName (xml) {
var retVal = "";
var node = XUI.Xml.SelectSingleNode(xml, "//link[@rel='edit']", null);
if (!IsNull(node)) {
retVal = node.getAttribute("title");
}
return retVal;
}
function GetLinks2Expand (xmlTemp, linksArr) {
var xml = new ActiveXObject("Microsoft.XMLDOM");
xml.loadXML(xmlTemp.xml);
var xpathOne = "m:inline";
var xpathMany = "m:inline/feed";
var xmlDeclaration = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>";
var retArray = new Array();
for (var i = 0; i < linksArr.length; i++) {
var node = XUI.Xml.SelectSingleNode(xml, "//link[@title = '" + linksArr[i] + "']", null);
if (!IsNull(node)) {
var title = node.getAttribute('title');
var tmpArr;
if (!IsNull(XUI.Xml.SelectSingleNode(node, xpathMany, null))) {
tmpArr = this.GetReturnObject(xmlDeclaration + XUI.Xml.SelectSingleNode(node, xpathMany, null).xml, null, "feed");
}
else if (!IsNull(XUI.Xml.SelectSingleNode(node, xpathOne, null))) {
tmpArr = this.GetReturnObject(xmlDeclaration + XUI.Xml.SelectSingleNode(node, xpathOne, null).xml, null, xpathOne);
}
if (!IsNull(title) && !IsNull(linksArr)) {
var LinkObject = new Link(title, tmpArr);
retArray.push(LinkObject);
}
}
}
return retArray;
}
FieldTypes = {
int: "Edm.Int32",
long: "Edm.Int64",
bool: "Edm.Boolean",
guid: "Edm.Guid",
datetime: "Edm.DateTime",
decimal: "Edm.Decimal",
optionSetValue: "Microsoft.Crm.Sdk.Data.Services.OptionSetValue",
money: "Microsoft.Crm.Sdk.Data.Services.Money",
entityReference: "Microsoft.Crm.Sdk.Data.Services.EntityReference",
double: "Edm.Double",
string: ""
}
function FixedString(str) {
str = str.replace("\\", "\\\\");
str = str.replace(/\'/g, "\\'");
var newStr = str;
return newStr;
}
Enjoy,
Rami Heleg