Archive
Reading option set using JScript in CRM 2011
Hi,
Below is the Jscript functions to read “Option set” and get the option text by option value.
In this approach I have 3 methods and details are below
- getOptionSet() – Returns optionset object
- Parameters
- Entity Name (Entity logical name)
- Attribute Name (i.e., Option set field name)
- retrieveAsIfPublished (True/False)
- Parameters
- getTextByValue() – Returns option set text by passing value
- Parameters
- optionSet (i.e., Option set object got from above method)
- attributeValue (Option set field value)
- Parameters
- getSOAPWrapper – Helper method
function getOptionSet (entityLogicalName, attributeLogicalName, retrieveAsIfPublished) {
var serverUrl = Xrm.Page.context.getServerUrl();
var ODataPath = serverUrl + “/XRMServices/2011/Organization.svc/web”;
var MetadataId = “00000000-0000-0000-0000-000000000000”;
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>”;
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\“>” + attributeLogicalName + “</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 attributeData;
break;
default:
break;
}
}
}
}
function _getSOAPWrapper(request) {
var SOAP = “<soapenv:Envelope xmlns:soapenv=\”http://schemas.xmlsoap.org/soap/envelope/\“><soapenv:Body>”;
SOAP += request;
SOAP += “</soapenv:Body></soapenv:Envelope>”;
return SOAP;
}
function getTextByValue(optionSet, attributeValue) {
var options = optionSet.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) {
var text = options.childNodes[i].selectSingleNode(“c:Label”).selectSingleNode(“a:UserLocalizedLabel”).selectSingleNode(“a:Label”).text;
return text;
}
}
}
How do I call this method –
- Copy the above functions and create a Jscript webresource in CRM
- In below example I am reading “Address Type” option set of entity “Contact”
// Get option set object by passing params
// Entity name, Optionset field name, true/false
var optionSet = getOptionSet(“contact”, “address1_addresstypecode”, true);
if (optionSet) {
// Read option text of value ‘2’
var optionValue = 2;
var optionText = getTextByValue(optionSet, optionValue);
alert(“Text of option value – ” + optionValue + ” is -” + optionText);
// Read option text of value ‘10001’
optionValue = 10001;
optionText = getTextByValue(optionSet, optionValue);
alert(“Text of option value – ” + optionValue + ” is -” + optionText);
}
else {
alert(“Invalid Option set”);
}
Note :- *** If you copy the code please replace double quotes(“) symbols with your keyboard double quotes ***
Hope it helps 🙂