views:

1130

answers:

3

I'm trying to pull information from a CRM installation and so far this is fine for using the default fields. However I'm having difficulty retrieving custom fields, for example Contacts have a custom field called web_username.

My code at present is

        QueryExpression query = new QueryExpression();
        query.EntityName = "contact";
        ColumnSet cols = new ColumnSet();
        cols.Attributes = new string[] { "firstname", "lastname" };
        query.ColumnSet = cols;

        BusinessEntityCollection beReturned = tomService.RetrieveMultiple(query);
        foreach (contact _contact in beReturned.BusinessEntities)
        {
            DataRow dr = dt.NewRow();
            dr["firstname"] = _contact.firstname;
            dr["lastname"] = _contact.lastname;
            dt.Rows.Add(dr);
        }

How do I include custom fields in my query? I've tried searching around but with no luck yet but I could be searching incorrectly as I'm not used to CRM terms.

Cheers in advance!

+2  A: 

I've since been able to solve this. In case it is of use to anyone else this is what I did. The query is set up as before except I've added my custom field to the ColumnSet.

cols.Attributes = new string[] { "firstname", "lastname", "new_web_username" };

And then used RetrieveMultipleResponse and Request with ReturnDynamicEntities set to true

        RetrieveMultipleResponse retrived = new RetrieveMultipleResponse();

        RetrieveMultipleRequest retrive = new RetrieveMultipleRequest();
        retrive.Query = query;
        retrive.ReturnDynamicEntities = true;

        retrived = (RetrieveMultipleResponse)tomService.Execute(retrive);

Please still comment if there is a better way for me to do this.

EDIT Using the example in my original question if you cast to a contact

contact myContact = (contact)myService.Retrieve(EntityName.contact.ToString(), userID, cols);

You can then access properties of the object

                phone = myContact.telephone1;
            password = myContact.new_password;

If you update your CRM webreference custom fields you've added in CRM are available.

Does that help?

Cheers,

Fishcake.

Fishcake
A: 

Just curious how you are retrieving the value of the new_web_username from the results? Are you accessing them with contact.new_web_username or is there a way to specify the field name as part of a collection as a string?

Yes, contact.new_web_username exactly.
Fishcake
I don't understand how this could work since it is not a standard field... how can it be part of the definition of the contact object if it is a custom entity or is there some kind of late binding going on?
Actually I see what is going on now... the WSDL for the CRMService webservice actually changes to include my custom attribute.
A: 

Please explain in detail how to access custom field from 'retrived'

AD User
Edited my original question. Does that help?
Fishcake