Dela via


DropDownListbox with Lookup data from CRM.

hey,

Have you ever wondered how to get values that you see in a lookup entity in MS CRM?

Today I found the way of doing this.

You simply pass an xml request to the Fetch property of the CrmService, in this example called mycrm.
In this example I will make a dropdownlist of available languages a customer can order brochures in.

The entity we want to retrieve data from is in this case called new_language.

//Crm Authentication mycrm = new CrmSdk.CrmService
string fetchXml = "<fetch mapping='logical'><entity name='new_language'><all-attributes/></entity></fetch>";
string result = mycrm.Fetch(fetchXml);
XmlDocument xdoc = new XmlDocument();
xdoc.Load(result);
XmlNodeList langList = xdoc.SelectNodes("resultset/result");
foreach (XmlNode itm in langList)
{
  ListItem item = new ListItem();
item.Text = itm["new_name"].InnerText;
item.Value = itm["new_languageid"].InnerText;
ddlLanguage.Items.Add(item);
}

The XML I receive is like this

<

resultset>
<result>
<createdby dsc="0" name="Test User 2">{06893EF7-3DB6-DD11-A401-0017A4AACD8C}</createdby>
<statecode name="Active">0</statecode>
<new_languageid>{AF2EA9D4-EDB6-DD11-AA93-0017A4AACD8C}</new_languageid>
<owningbusinessunit>{EB773EF7-3DB6-DD11-A401-0017A4AACD8C}</owningbusinessunit>
<new_name>German</new_name>
<new_languagecode>DE</new_languagecode>
<modifiedon date="11/20/2008" time="10:27 AM">2008-11-20T10:27:29-00:00</modifiedon>
    <createdon date="11/20/2008" time="10:27 AM">2008-11-20T10:27:29-00:00</createdon>
<statuscode name="Active">1</statuscode>
<ownerid name="Test User 2" dsc="0" type="8">{06893EF7-3DB6-DD11-A401-0017A4AACD8C}</ownerid>
<modifiedby dsc="0" name="Test User 2">{06893EF7-3DB6-DD11-A401-0017A4AACD8C}</modifiedby>
</result>
<result>
<createdby dsc="0" name="Test User 3">{321EC4E1-0EB7-DD11-AA93-0017A4AACD8C}</createdby>
<statecode name="Active">0</statecode>
<new_languageid>{6DF8E3E9-2BBA-DD11-AA93-0017A4AACD8C}</new_languageid>
<owningbusinessunit>{EB773EF7-3DB6-DD11-A401-0017A4AACD8C}</owningbusinessunit>
<new_name>English</new_name>
<new_languagecode>English</new_languagecode>
<modifiedon date="11/24/2008" time="1:29 PM">2008-11-24T13:29:27-00:00</modifiedon>
<createdon date="11/24/2008" time="1:29 PM">2008-11-24T13:29:27-00:00</createdon>
<statuscode name="Active">1</statuscode>
<ownerid name="Test User 3" dsc="0" type="8">{321EC4E1-0EB7-DD11-AA93-0017A4AACD8C}</ownerid>
<modifiedby dsc="0" name="Test User 3">{321EC4E1-0EB7-DD11-AA93-0017A4AACD8C}</modifiedby>
</result>
</resultset>

You might need to do some formatting of the attributes of the <resultset> since you might get some illegal characters.

Hope this helps,

Peace out,

Christian