将成员添加到CRM 2011插件中的营销列表中

时间:2012-05-15 14:40:01

标签: c# dynamics-crm dynamics-crm-2011 crm

我希望有人可以帮助我。我找到了一篇文章,它为您提供了一小段代码,用于添加添加多个实体(成员)到营销列表。到现在为止还挺好。我遇到了这个问题。我有一个自定义查找字段,它在营销成员列表中获得另一个营销列表(具有联系人,帐户或潜在客户)。现在我需要将这些成员迁移(添加)到我的新营销列表中。我的代码:

  1. AddListMembersListRequest request = new AddListMembersListRequest();
  2. request.ListId = Origmarketing_List_Id.Id;  
  3. request.MemberIds = new Guid[1];
  4. request.MemberIds[0] = guid;
  5. AddListMembersListResponse resp = (AddListMembersListResponse)service.Execute(request);   

第2行是我从EntityReference获取的ID(查找字段获取另一个营销列表),现在我设置的第三和第四行是我真的很困惑但我仍然确定我是就在这里,因为我将它设置为listmemberid。在这个例子中,我只有一个原因,我想尝试它是如何工作的。第4行bdw中的guid获得正确的值,它在我的代码的顶部被声明(并且我在另一个字段上输出它只是为了检查它获取正确的值)。也有人可以在你想要添加多个实体时展示你将如何做到这一点?谢谢。我在预操作(Create)上注册了我的插件。并且插件本身不会触发任何错误,但它似乎并没有在我的新列表中添加任何成员。如果somone可以帮助我,我真的很感激。非常感谢你提前。

1 个答案:

答案 0 :(得分:5)

首先,将事件更改为后期操作,因为您还没有创建实体的GUID,事实上您也没有实体本身,这就是为什么它被称为预操作。 要添加多个实体,请尝试传递GUIDs数组,如下面的代码:

    // Setup the CrmConnection and OrganizationService instances
    CrmConnectionInstance = new CrmConnection(ConfigurationConstants.CrmConnectionName);
OrgServiceInstance = new OrganizationService(CrmConnectionInstance);
    // Create the marketing list 
    Guid NewMarketingListId = Guid.Empty; 
    Microsoft.Xrm.Sdk.Entity CurrentList = new Microsoft.Xrm.Sdk.Entity(MarketingListConstants.MarketingListEntityName); 
    CurrentList[MarketingListConstants.MarketingListTypeAttribute] = false; 
    CurrentList[MarketingListConstants.ListNameAttribute] = "NameOfList"; 
    // For contacts, a value of 2 should be used. 
    CurrentList[MarketingListConstants.CreatedFromCodeAttribute] = new OptionSetValue(2); 
    // Actually create the list 
    NewMarketingListId = OrgServiceInstance.Create(CurrentList); 
    // Use the AddListMembersListRequest to add the members to the list 
    List<Guid> MemberListIds = new List<Guid>(); 
    // Now you'll need to add the Guids for each member to the list  
    // I'm leaving that part out as adding values to a list is very basic. 
    AddListMembersListRequest AddMemberRequest = new AddListMembersListRequest(); 
    AddMemberRequest.ListId = NewMarketingListId; 
    AddMemberRequest.MemberIds = memberIds.ToArray(); 
    // Use AddListMembersListReponse to get information about the request execution 
    AddListMembersListResponse AddMemberResponse = OrgServiceInstance.Execute(AddMemberRequest) as AddListMembersListResponse;
相关问题