Get EntityId for MarketingList Member CRM 2011 RRS feed

  • Question

  • I'm sure someone can help me out here, I'm really much ripping my hair out
    why my code below doesn't work. Basically I have a custom entity wich has a list
    (and all its list members inside the list) with my custom entity(registerd on
    post-create) I create another list which imports all those list members into
    another list. Now I need to retrieve the entityID for each list member (in order
    for me to add them) My code below (just for one list member for testing

    QueryByAttribute query = new QueryByAttribute("listmember");
    query.AddAttributeValue("listid", m_list_ID.Id);
    ColumnSet cs = new ColumnSet();
    query.ColumnSet = cs;
    EntityCollection entityCollection = service.RetrieveMultiple(query);
    Guid g = Guid.Empty;
    g = (Guid)entityCollection.Entities[0].Attributes["entityid"];

    To check the guid I output the GUID on another field as a string just to
    check if it has th right one, but when I try to save the form I get the error
    "System.InvalidCastException: Specified cast is not valid.". On another note, which is quite strange to me is

    when I try the same with "listmemberid" which is also in listmember table I get the GUID, its just when I try

    to grab the entityId I get this error.

    Tuesday, May 22, 2012 2:30 PM

All replies

  • Hi A Roben,

    The entityid attribute is actually returning EntityReference object (not Guid).

    So to get the actual Guid of the EntityId, you would have to convert the Attribute to EntityReference and access the Id property of it.

    g = ((EntityReference)entityCollection.Entities[0].Attributes["entityid"]).Id;

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.

    Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits

    Tuesday, May 22, 2012 11:13 PM