locked
CRM 4.0 : ListMembers Query RRS feed

  • Question

  • Hi guys,

    So, I have a custom entity (planning) which has a relationship with “Marketing List”, where I add “contacts” to the “ListMembers”.

    On a certain status of this planning entity, I create a campaign record. Now, I would like to “ADD” the “listmember” from the planning record to the newly created campaign record.

    I have the below which I retrieve contacts from the “listmembers”:

    //initialize QueryExpression for adding link entities

    QueryExpression qe = new QueryExpression();

    qe.EntityName = EntityName.contact.ToString();

    //Initialize columnset

    ColumnSet col = new ColumnSet();

     

    //add columns to columnset for the acc to retrieve each acc from the contact list

    col.AddColumns(new string[] { "donotbulkemail",

                                  "donotphone",

                                  "donotfax",

                                  "donotbulkpostalmail",

                                  "donotemail",

                                  "donotpostalmail",

                                  "bis_calldownscripts",

                                  "fullname",

    "new_ teststring" });

    qe.ColumnSet = col;

     

    // link from contact to listmember

    LinkEntity le = new LinkEntity();

    le.LinkFromEntityName = EntityName.contact.ToString();

    le.LinkFromAttributeName = "contactid";

    le.LinkToEntityName = EntityName.listmember.ToString();

    le.LinkToAttributeName = "entityid";

     

    //link from listmember to list

    LinkEntity le2 = new LinkEntity();

    le2.LinkFromEntityName = EntityName.listmember.ToString();

    le2.LinkFromAttributeName = "listid";

    le2.LinkToEntityName = EntityName.list.ToString();

    le2.LinkToAttributeName = "listid";

     

    // add condition for listid

    ConditionExpression ce = new ConditionExpression();

    ce.AttributeName = "listid";

    ce.Operator = ConditionOperator.Equal;

    ce.Values = new object[] { guidListID };

     

    //add condition to linkentity

    le2.LinkCriteria = new FilterExpression();

    le2.LinkCriteria.Conditions.Add(ce);

     

    //add linkentity2 to linkentity

    le.LinkEntities.Add(le2);

     

    //add linkentities to Query Expression which is passed for getting collection of contacts

    qe.LinkEntities.Add(le);

     

    //above query expression is passed to retrieve multiple for getting BusinessEntityCollection of contacts.

    BusinessEntityCollection bec = crmService.RetrieveMultiple(qe);

    foreach (contact deContact in bec.BusinessEntities)

    {

    string Con = deContact.fullname;

    Console.WriteLine(deContact.contactid.Value);

     

    bool DoNotBulkEmail = deContact.donotbulkemail.Value;

    bool DoNotPhone = deContact.donotphone.Value;

     

    Guid ID = deContact.contactid.Value;

    string FullName = deContact.fullname;

    string newString = deContact.new_teststring; //Custom field

    }

     

    The below line does not work

    string newString = deContact.new_teststring; //Custom field

    is there anything I am doing wrong here?

    On a certain status of this planning entity, I create a campaign record. Now, I would like to “ADD” the “listmember” from the planning record to the newly created campaign record.

    Any ideas on how to achieve this guys?

    Regards

    _Hopeful

    Sunday, October 16, 2011 9:06 PM

Answers

  • Thanks guys, this worked for me:

    RetrieveMultipleRequest requestEntity = new RetrieveMultipleRequest();
    requestEntity.Query = qe;
    requestEntity.ReturnDynamicEntities = true;
    RetrieveMultipleResponse responseEntity = (RetrieveMultipleResponse)crmService.Execute(requestEntity);

    So, now i am creating Marketing List's from the contacts with the below :

    foreach(DynamicEntity deContact in responseEntity.BusinessEntityCollection.BusinessEntities)

    {

    #region Create List

    list autoList = new list();

    autoList.listname = ContactMethod;

    autoList.membertype = new CrmNumber();

    autoList.membertype.Value = 2; //Contact/Customer

    autoList.createdfromcode = new Picklist();

    autoList.createdfromcode.Value = 2; //Contact/Customer

    Guid listId = crmService.Create(autoList);

    #endregion 

    #region Add Member to List

    AddMemberListRequest addRequest = new AddMemberListRequest();

    addRequest.EntityId = guidContact;

    addRequest.ListId = listId;

    AddMemberListResponse response = (AddMemberListResponse)crmService.Execute(addRequest);

    #endregion

    }

    "ContactMethod" would be either "Fax, SMS. email e.t.c" I use that as the ListName from the contact.

    e.g: If i have two or more contacts with the same ContactMethod, i.e: "E-mail", i would like to create an E-mail Marketing List and then be able to add those contacts as Members to that Marketing List. How would I go about doing that and I would also like to be able to associate the marketing list with a campaign record.

    Any assistance is welcome guys.

     

    • Edited by _Hopeful Monday, October 17, 2011 7:34 AM
    • Marked as answer by Donna EdwardsMVP Monday, October 17, 2011 11:33 AM
    Monday, October 17, 2011 7:20 AM
  • Hello,

    Following sample code should perform association of list and campaign

                AddItemCampaignRequest addListToCampaignRequest = new AddItemCampaignRequest();
                addListToCampaignRequest.CampaignId = createdCampaignId;
                addListToCampaignRequest.EntityName = EntityName.list;
                addListToCampaignRequest.EntityId = createdMarketingListId;
                
                AddItemCampaignResponse createdCampaignItem = (AddItemCampaignResponse)service.Execute(addListToCampaignRequest);
    



    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter
    Monday, October 17, 2011 7:30 AM
    Moderator

All replies

  • Hi,

     

    In the list of columns

    col.AddColumns(new string[] { "donotbulkemail",

     

                                  "donotphone",

                                  "donotfax",

                                  "donotbulkpostalmail",

                                  "donotemail",

                                  "donotpostalmail",

                                  "bis_calldownscripts",

                                  "fullname",

    "new_ teststring"

    The new_teststring has been provided to include a space after new_

    Again should that really be the error, the retrieve call itself should have thrown an error missing field...

    What error do you receive from

    string newString = deContact.new_teststring; //Custom field

    That might help understand the issue.

    Sam

     

     


    Web: http://www.inogic.com
    Blog: http://inogic.blogspot.com
    Email: news@inogic.com
    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Monday, October 17, 2011 3:29 AM
  • The new_teststring field does not show in intellisense. All other fields show.
    Monday, October 17, 2011 4:49 AM
  • I don't know how are you using this code.

    If you are using web services, you need to update the web service reference to include this custom field.

    But if you are using dlls, you can't see this field. To use this field, you need to use Dynamic Entity instead of contact entity.

    I hope this helps.


    Amreek Singh Senior CRM Consultant CDC Praxa Sydney, Australia http://mscrmshop.blogspot.com http://crm2011usersettings.codeplex.com
    • Proposed as answer by Amreek Singh Monday, October 17, 2011 5:13 AM
    • Unproposed as answer by _Hopeful Monday, October 17, 2011 7:34 AM
    • Edited by Amreek Singh Monday, October 17, 2011 11:05 AM
    Monday, October 17, 2011 5:13 AM
  • I am using the SDK dll's,

    The problem i have is, I am unable to cast the "bec.BusinessEntities" into a DynamicEntity.

    Monday, October 17, 2011 5:30 AM
  • Hello,

    Your custom field would be shown using intellisense only in the case you will add webreference.

    You should use DynamicEntity. To use it you should use RetrieveMultipleRequest and in foreach clause - DynamicEntity. To get/set property you should use following syntax:

     

    string newString = (string)deContact["new_teststring"];
    deContact["new_teststring"] = newString;
    

     


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter
    Monday, October 17, 2011 5:30 AM
    Moderator
  • You need to set

    qe.ReturnDynamicEntities = true;

     

    I hope this helps.


    Amreek Singh Senior CRM Consultant CDC Praxa Sydney, Australia http://mscrmshop.blogspot.com http://crm2011usersettings.codeplex.com
    • Proposed as answer by Amreek Singh Monday, October 17, 2011 5:35 AM
    Monday, October 17, 2011 5:35 AM
  • You need to set

    qe.ReturnDynamicEntities = true;

     

    I hope this helps.


    Amreek Singh Senior CRM Consultant CDC Praxa Sydney, Australia http://mscrmshop.blogspot.com http://crm2011usersettings.codeplex.com

    As far as I know QueryExpression class doesn't contain ReturnDynamicEntities property - http://technet.microsoft.com/en-us/library/bb890680.aspx

    RetrieveMultipleRequest contains.


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter
    Monday, October 17, 2011 5:50 AM
    Moderator
  • Thanks guys, this worked for me:

    RetrieveMultipleRequest requestEntity = new RetrieveMultipleRequest();
    requestEntity.Query = qe;
    requestEntity.ReturnDynamicEntities = true;
    RetrieveMultipleResponse responseEntity = (RetrieveMultipleResponse)crmService.Execute(requestEntity);

    So, now i am creating Marketing List's from the contacts with the below :

    foreach(DynamicEntity deContact in responseEntity.BusinessEntityCollection.BusinessEntities)

    {

    #region Create List

    list autoList = new list();

    autoList.listname = ContactMethod;

    autoList.membertype = new CrmNumber();

    autoList.membertype.Value = 2; //Contact/Customer

    autoList.createdfromcode = new Picklist();

    autoList.createdfromcode.Value = 2; //Contact/Customer

    Guid listId = crmService.Create(autoList);

    #endregion 

    #region Add Member to List

    AddMemberListRequest addRequest = new AddMemberListRequest();

    addRequest.EntityId = guidContact;

    addRequest.ListId = listId;

    AddMemberListResponse response = (AddMemberListResponse)crmService.Execute(addRequest);

    #endregion

    }

    "ContactMethod" would be either "Fax, SMS. email e.t.c" I use that as the ListName from the contact.

    e.g: If i have two or more contacts with the same ContactMethod, i.e: "E-mail", i would like to create an E-mail Marketing List and then be able to add those contacts as Members to that Marketing List. How would I go about doing that and I would also like to be able to associate the marketing list with a campaign record.

    Any assistance is welcome guys.

     

    • Edited by _Hopeful Monday, October 17, 2011 7:34 AM
    • Marked as answer by Donna EdwardsMVP Monday, October 17, 2011 11:33 AM
    Monday, October 17, 2011 7:20 AM
  • Hello,

    Following sample code should perform association of list and campaign

                AddItemCampaignRequest addListToCampaignRequest = new AddItemCampaignRequest();
                addListToCampaignRequest.CampaignId = createdCampaignId;
                addListToCampaignRequest.EntityName = EntityName.list;
                addListToCampaignRequest.EntityId = createdMarketingListId;
                
                AddItemCampaignResponse createdCampaignItem = (AddItemCampaignResponse)service.Execute(addListToCampaignRequest);
    



    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter
    Monday, October 17, 2011 7:30 AM
    Moderator
  • sorry buddy, My bad. I meant

    requestEntity.ReturnDynamicEntities = true;

    Any he worked it out.


    Amreek Singh Senior CRM Consultant CDC Praxa Sydney, Australia http://mscrmshop.blogspot.com http://crm2011usersettings.codeplex.com
    Monday, October 17, 2011 11:07 AM