Hi,
Recently I was working on an assignment where I had to retrieve data of a lookup record using Jscript, I accomplished to retrieve the lookup record's data using RestFul services. But I found that for the optionset field on the lookup record, i got back only the value (integer) of the selected option, whereas i was expecting to get both, value as well as its text. only later i found that to retrieve its text, I had to do a soap request to the organization metadata service.
Here;s the code which will get the optionset's text. You need to supply the entity logical name, attribute logical name & its selected value, to get back the text of selection option.
function RetrieveAttributeSync(EntityLogicalName, LogicalName, MetadataId, RetrieveAsIfPublished, attributeValue) {
var ODataPath;
var serverUrl = Xrm.Page.context.getServerUrl();
ODataPath = serverUrl + "/XRMServices/2011/Organization.svc/web";
var request = "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
request += "<request i:type=\"a:RetrieveAttributeRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">";
request += "<a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
request += "<a:KeyValuePairOfstringanyType>";
request += "<b:key>EntityLogicalName</b:key>";
request += "<b:value i:type=\"c:string\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + EntityLogicalName + "</b:value>";
request += "</a:KeyValuePairOfstringanyType>";
if (MetadataId == null)
{ MetadataId = "00000000-0000-0000-0000-000000000000"; }
request += "<a:KeyValuePairOfstringanyType>";
request += "<b:key>MetadataId</b:key>";
request += "<b:value i:type=\"ser:guid\" xmlns:ser=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + MetadataId + "</b:value>";
request += "</a:KeyValuePairOfstringanyType>";
request += "<a:KeyValuePairOfstringanyType>";
request += "<b:key>RetrieveAsIfPublished</b:key>";
request += "<b:value i:type=\"c:boolean\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + RetrieveAsIfPublished + "</b:value>";
request += "</a:KeyValuePairOfstringanyType>";
request += "<a:KeyValuePairOfstringanyType>";
request += "<b:key>LogicalName</b:key>";
request += "<b:value i:type=\"c:string\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + LogicalName + "</b:value>";
request += "</a:KeyValuePairOfstringanyType>";
request += "</a:Parameters>";
request += "<a:RequestId i:nil=\"true\" /><a:RequestName>RetrieveAttribute</a:RequestName></request>";
request += "</Execute>";
request = _getSOAPWrapper(request);
var req = new XMLHttpRequest();
req.open("POST", ODataPath, false);
req.setRequestHeader("Accept", "application/xml, text/xml, */*");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
req.send(request);
if (req.responseXML != null) {
var attributeData = req.responseXML.selectSingleNode("//b:value");
if (attributeData != null) {
var attributeType = attributeData.selectSingleNode("c:AttributeType").text;
switch (attributeType) {
case "Picklist":
return getPickListTextValue(attributeData, attributeValue);
break;
default:
break;
}
function getPickListTextValue(attributeData, attributeValue) {
var options = attributeData.selectSingleNode("c:OptionSet//c:Options");
for (var i = 0; i < options.childNodes.length; i++) {
var value = options.childNodes[i].selectSingleNode("c:Value").text;
if (value == attributeValue['Value']) {
var text = options.childNodes[i].selectSingleNode("c:Label").selectSingleNode("a:UserLocalizedLabel").selectSingleNode("a:Label").text;
return text;
}
}
}
Thus by using the above code, you will be able to retrieve all the items (text & value) of the option set in JScript.
Hope this post will be helpful to someone, as I struggled with this functionality !!
Good Luck :)
Recently I was working on an assignment where I had to retrieve data of a lookup record using Jscript, I accomplished to retrieve the lookup record's data using RestFul services. But I found that for the optionset field on the lookup record, i got back only the value (integer) of the selected option, whereas i was expecting to get both, value as well as its text. only later i found that to retrieve its text, I had to do a soap request to the organization metadata service.
Here;s the code which will get the optionset's text. You need to supply the entity logical name, attribute logical name & its selected value, to get back the text of selection option.
function RetrieveAttributeSync(EntityLogicalName, LogicalName, MetadataId, RetrieveAsIfPublished, attributeValue) {
var ODataPath;
var serverUrl = Xrm.Page.context.getServerUrl();
ODataPath = serverUrl + "/XRMServices/2011/Organization.svc/web";
var request = "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
request += "<request i:type=\"a:RetrieveAttributeRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">";
request += "<a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
request += "<a:KeyValuePairOfstringanyType>";
request += "<b:key>EntityLogicalName</b:key>";
request += "<b:value i:type=\"c:string\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + EntityLogicalName + "</b:value>";
request += "</a:KeyValuePairOfstringanyType>";
if (MetadataId == null)
{ MetadataId = "00000000-0000-0000-0000-000000000000"; }
request += "<a:KeyValuePairOfstringanyType>";
request += "<b:key>MetadataId</b:key>";
request += "<b:value i:type=\"ser:guid\" xmlns:ser=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + MetadataId + "</b:value>";
request += "</a:KeyValuePairOfstringanyType>";
request += "<a:KeyValuePairOfstringanyType>";
request += "<b:key>RetrieveAsIfPublished</b:key>";
request += "<b:value i:type=\"c:boolean\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + RetrieveAsIfPublished + "</b:value>";
request += "</a:KeyValuePairOfstringanyType>";
request += "<a:KeyValuePairOfstringanyType>";
request += "<b:key>LogicalName</b:key>";
request += "<b:value i:type=\"c:string\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + LogicalName + "</b:value>";
request += "</a:KeyValuePairOfstringanyType>";
request += "</a:Parameters>";
request += "<a:RequestId i:nil=\"true\" /><a:RequestName>RetrieveAttribute</a:RequestName></request>";
request += "</Execute>";
request = _getSOAPWrapper(request);
var req = new XMLHttpRequest();
req.open("POST", ODataPath, false);
req.setRequestHeader("Accept", "application/xml, text/xml, */*");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
req.send(request);
if (req.responseXML != null) {
var attributeData = req.responseXML.selectSingleNode("//b:value");
if (attributeData != null) {
var attributeType = attributeData.selectSingleNode("c:AttributeType").text;
switch (attributeType) {
case "Picklist":
return getPickListTextValue(attributeData, attributeValue);
break;
default:
break;
}
function getPickListTextValue(attributeData, attributeValue) {
var options = attributeData.selectSingleNode("c:OptionSet//c:Options");
for (var i = 0; i < options.childNodes.length; i++) {
var value = options.childNodes[i].selectSingleNode("c:Value").text;
if (value == attributeValue['Value']) {
var text = options.childNodes[i].selectSingleNode("c:Label").selectSingleNode("a:UserLocalizedLabel").selectSingleNode("a:Label").text;
return text;
}
}
}
Thus by using the above code, you will be able to retrieve all the items (text & value) of the option set in JScript.
Hope this post will be helpful to someone, as I struggled with this functionality !!
Good Luck :)
Thanks mate....
ReplyDelete