The following code example demonstrates how to create a campaign and retrieve the members from the marketing list that is used during a bulk operation, such as a distributing phone activity.
1: //# [ Create a Campaign ]
2:
3: using System;
4: using CrmSdk;
5: using Microsoft.Crm.Sdk.Utility;
6: using System.Web.Services.Protocols;
7:
8: namespace Microsoft.Crm.Sdk.Reference.BulkOperation
9: {
10: public class RetrieveMembersBulkOperation
11: {
12:
13: public RetrieveMembersBulkOperation()
14: {
15:
16: }
17:
18: public static bool Run(string crmServerUrl, string orgName)
19: {
20: bool success = false;
21:
22: // Set up the CRM Service.
23: CrmService service = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetCrmService(crmServerUrl, orgName);
24: service.PreAuthenticate = true;
25:
26: #region Setup Data Required for this Sample
27:
28: // Create a campaign.
29: campaign sampleCampaign = new campaign();
30: sampleCampaign.name = "SDK Sample Campaign";
31:
32: Guid createdCampaignId = service.Create(sampleCampaign);
33:
34: // Create a campaign activity.
35: campaignactivity sampleActivity = new campaignactivity();
36: sampleActivity.regardingobjectid = new Lookup();
37: sampleActivity.regardingobjectid.type = EntityName.campaign.ToString();
38: //sampleActivity.regardingobjectid.Value = createdCampaign.id;
39: sampleActivity.regardingobjectid.Value = createdCampaignId;
40: sampleActivity.subject = "Sample Campaign Activity";
41: sampleActivity.channeltypecode = new Picklist(1); // 1 == phone
42:
43: Guid createdCampaignActivityId = service.Create(sampleActivity);
44:
45: // Create a list to add to the activity.
46: list marketingList = new list();
47: marketingList.listname = "SDK Sample Marketing List";
48: marketingList.createdfromcode = new Picklist();
49: marketingList.createdfromcode.Value = 1; // 1 == account
50:
51: Guid createdMarketingListId = service.Create(marketingList);
52:
53: // Create an account to add to the marketing list.
54: // This will be the name returned by the RetrieveMembersBulkOperation message.
55: account sampleAccount = new account();
56: sampleAccount.name = "Fourth Coffee";
57:
58: Guid createdAccountId = service.Create(sampleAccount);
59:
60: AddMemberListRequest addAccountRequest = new AddMemberListRequest();
61: addAccountRequest.ListId = createdMarketingListId;
62: addAccountRequest.EntityId = createdAccountId;
63:
64: service.Execute(addAccountRequest);
65:
66: // First, associate the list with the campaign.
67: AddItemCampaignRequest addListToCampaignRequest = new AddItemCampaignRequest();
68: addListToCampaignRequest.CampaignId = createdCampaignId;
69: addListToCampaignRequest.EntityName = EntityName.list;
70: addListToCampaignRequest.EntityId = createdMarketingListId;
71:
72: AddItemCampaignResponse createdCampaignItem = (AddItemCampaignResponse)service.Execute(addListToCampaignRequest);
73:
74: // Then, associate the list with the campaign activity.
75: AddItemCampaignActivityRequest addListToCampaignActivityRequest = new AddItemCampaignActivityRequest();
76: addListToCampaignActivityRequest.CampaignActivityId = createdCampaignActivityId;
77: addListToCampaignActivityRequest.EntityName = EntityName.list;
78: addListToCampaignActivityRequest.ItemId = createdMarketingListId;
79:
80: AddItemCampaignActivityResponse createdCampaignActivityItem = (AddItemCampaignActivityResponse)service.Execute(addListToCampaignActivityRequest);
81:
82: #endregion
83:
84: // Create a phone activity to be distributed (bulk operation).
85: phonecall samplePhoneCall = new phonecall();
86: samplePhoneCall.subject = "Sample phone call to distribute to a marketing list.";
87:
88: // The owner property is REQUIRED.
89: WhoAmIRequest systemUserRequest = new WhoAmIRequest();
90: WhoAmIResponse systemUser = (WhoAmIResponse)service.Execute(systemUserRequest);
91:
92: // Execute a bulk operation.
93: DistributeCampaignActivityRequest distributeCampaignRequest = new DistributeCampaignActivityRequest();
94: distributeCampaignRequest.Activity = samplePhoneCall;
95: distributeCampaignRequest.CampaignActivityId = createdCampaignActivityId;
96: distributeCampaignRequest.Propagate = true;
97: distributeCampaignRequest.SendEmail = false;
98: distributeCampaignRequest.Owner = new Moniker();
99: distributeCampaignRequest.Owner.Id = systemUser.UserId;
100: distributeCampaignRequest.Owner.Name = EntityName.systemuser.ToString();
101:
102: DistributeCampaignActivityResponse distributeCampaignResponse = (DistributeCampaignActivityResponse)service.Execute(distributeCampaignRequest);
103:
104: // Execute the request.
105: RetrieveMembersBulkOperationRequest getMembers = new RetrieveMembersBulkOperationRequest();
106: getMembers.BulkOperationId = distributeCampaignResponse.BulkOperationId;
107: getMembers.BulkOperationSource = BulkOperationSource.CampaignActivity;
108: getMembers.EntitySource = EntitySource.Account;
109: getMembers.ReturnDynamicEntities = false;
110:
111: RetrieveMembersBulkOperationResponse membersResponse = (RetrieveMembersBulkOperationResponse)service.Execute(getMembers);
112:
113: #region check success
114:
115: ColumnSet returnSet = new ColumnSet();
116: returnSet.Attributes = new string[] { "activityid", "statuscode" };
117:
118: // Wait for the bulk operation to complete.
119: bulkoperation retrieveMembersOperation = service.Retrieve(EntityName.bulkoperation.ToString(), distributeCampaignResponse.BulkOperationId, returnSet) as bulkoperation;
120: int secondsTicker = 20;
121: while (secondsTicker > 0 && retrieveMembersOperation.statuscode.Value != BulkOperationStatus.Completed)
122: {
123: retrieveMembersOperation = service.Retrieve(EntityName.bulkoperation.ToString(), distributeCampaignResponse.BulkOperationId, returnSet) as bulkoperation;
124: System.Threading.Thread.Sleep(1000);
125: secondsTicker--;
126: }
127:
128: // Verify that the account created and added to the marketing list was returned.
129: if (retrieveMembersOperation.statuscode.Value == BulkOperationStatus.Completed && membersResponse.BusinessEntityCollection.BusinessEntities.Length == 1)
130: {
131: account verifyAccount = membersResponse.BusinessEntityCollection.BusinessEntities[0] as account;
132: if (verifyAccount.accountid.Value == createdAccountId)
133: {
134: success = true;
135: }
136: }
137:
138: #endregion
139:
140: #region Remove Data Required for this Sample
141:
142: // Query for all phonecall activities created so that they can be removed.
143: ColumnSet resultSet = new ColumnSet();
144: resultSet.Attributes = new string[] {"activityid"};
145:
146: ConditionExpression phoneCallCondition = new ConditionExpression();
147: phoneCallCondition.AttributeName = "regardingobjectid";
148: phoneCallCondition.Operator = ConditionOperator.Equal;
149: phoneCallCondition.Values = new object[] {createdCampaignActivityId};
150:
151: FilterExpression regardingObjectFilter = new FilterExpression();
152: regardingObjectFilter.Conditions = new ConditionExpression[] { phoneCallCondition };
153: regardingObjectFilter.FilterOperator = LogicalOperator.And;
154:
155: QueryExpression phoneCallQuery = new QueryExpression();
156: phoneCallQuery.ColumnSet = resultSet;
157: phoneCallQuery.Criteria = regardingObjectFilter;
158: phoneCallQuery.EntityName = EntityName.phonecall.ToString();
159:
160: BusinessEntityCollection phoneCalls = service.RetrieveMultiple(phoneCallQuery);
161:
162: foreach (phonecall aPhoneCall in phoneCalls.BusinessEntities)
163: {
164: service.Delete(EntityName.phonecall.ToString(), aPhoneCall.activityid.Value);
165: }
166:
167: // Remove the campaign activity.
168: service.Delete(EntityName.campaignactivity.ToString(), createdCampaignActivityId);
169:
170: // Remove the campaign.
171: service.Delete(EntityName.campaign.ToString(), createdCampaignId);
172:
173: // Remove the marketing list.
174: service.Delete(EntityName.list.ToString(), createdMarketingListId);
175:
176: // Remove the account.
177: service.Delete(EntityName.account.ToString(), createdAccountId);
178:
179: #endregion
180:
181: return success;
182: }
183: }
184: }
185:
186: [Visual Basic .NET]
187:
188: Imports Microsoft.VisualBasic
189: Imports System
190: Imports CrmSdk
191: Imports Microsoft.Crm.Sdk.Utility
192:
193: Namespace Microsoft.Crm.Sdk.Reference.BulkOperation
194: Public Class RetrieveMembersBulkOperation
195: Public Sub New()
196:
197: End Sub
198:
199: Public Shared Function Run(ByVal crmServerUrl As String, ByVal orgName As String) As Boolean
200: Dim success As Boolean = False
201:
202: ' Set up the CRM Service.
203: Dim service As CrmService = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetCrmService(crmServerUrl, orgName)
204: service.PreAuthenticate = True
205:
206: ' #Region "Setup Data Required for this Sample"
207:
208: ' Create a campaign.
209: Dim sampleCampaign As New campaign()
210: sampleCampaign.name = "SDK Sample Campaign"
211:
212: Dim createdCampaignId As Guid = service.Create(sampleCampaign)
213:
214: ' Create a campaign activity.
215: Dim sampleActivity As New campaignactivity()
216: sampleActivity.regardingobjectid = New Lookup()
217: sampleActivity.regardingobjectid.type = EntityName.campaign.ToString()
218: sampleActivity.regardingobjectid.Value = createdCampaignId
219: sampleActivity.subject = "Sample Campaign Activity"
220: sampleActivity.channeltypecode = New Picklist(1) ' 1 == phone
221:
222: Dim createdCampaignActivityId As Guid = service.Create(sampleActivity)
223:
224: ' Create a list to add to the activity.
225: Dim marketingList As New list()
226: marketingList.listname = "SDK Sample Marketing List"
227: marketingList.createdfromcode = New Picklist()
228: marketingList.createdfromcode.Value = 1 ' 1 == account
229:
230: Dim createdMarketingListId As Guid = service.Create(marketingList)
231:
232: ' Create an account to add to the marketing list.
233: ' This will be the name returned by the RetrieveMembersBulkOperation message.
234: Dim sampleAccount As New account()
235: sampleAccount.name = "Fourth Coffee"
236:
237: Dim createdAccountId As Guid = service.Create(sampleAccount)
238:
239: Dim addAccountRequest As New AddMemberListRequest()
240: addAccountRequest.ListId = createdMarketingListId
241: addAccountRequest.EntityId = createdAccountId
242:
243: service.Execute(addAccountRequest)
244:
245: ' First, associate the list with the campaign.
246: Dim addListToCampaignRequest As New AddItemCampaignRequest()
247: addListToCampaignRequest.CampaignId = createdCampaignId
248: addListToCampaignRequest.EntityName = EntityName.list
249: addListToCampaignRequest.EntityId = createdMarketingListId
250:
251: Dim createdCampaignItem As AddItemCampaignResponse = CType(service.Execute(addListToCampaignRequest), AddItemCampaignResponse)
252:
253: ' Then, associate the list with the campaign activity.
254: Dim addListToCampaignActivityRequest As New AddItemCampaignActivityRequest()
255: addListToCampaignActivityRequest.CampaignActivityId = createdCampaignActivityId
256: addListToCampaignActivityRequest.EntityName = EntityName.list
257: addListToCampaignActivityRequest.ItemId = createdMarketingListId
258:
259: Dim createdCampaignActivityItem As AddItemCampaignActivityResponse = CType(service.Execute(addListToCampaignActivityRequest), AddItemCampaignActivityResponse)
260:
261: ' #End Region
262:
263: ' Create a phone activity to be distributed (bulk operation).
264: Dim samplePhoneCall As New phonecall()
265: samplePhoneCall.subject = "Sample phone call to distribute to a marketing list."
266:
267: ' The owner property is a REQUIRED property.
268: Dim systemUserRequest As New WhoAmIRequest()
269: Dim systemUser As WhoAmIResponse = CType(service.Execute(systemUserRequest), WhoAmIResponse)
270:
271: ' Execute a bulk operation.
272: Dim distributeCampaignRequest As New DistributeCampaignActivityRequest()
273: distributeCampaignRequest.Activity = samplePhoneCall
274: distributeCampaignRequest.CampaignActivityId = createdCampaignActivityId
275: distributeCampaignRequest.Propagate = True
276: distributeCampaignRequest.SendEmail = False
277: distributeCampaignRequest.Owner = New Moniker()
278: distributeCampaignRequest.Owner.Id = systemUser.UserId
279: distributeCampaignRequest.Owner.Name = EntityName.systemuser.ToString()
280:
281: Dim distributeCampaignResponse As DistributeCampaignActivityResponse = CType(service.Execute(distributeCampaignRequest), DistributeCampaignActivityResponse)
282:
283: ' Execute the request.
284: Dim getMembers As New RetrieveMembersBulkOperationRequest()
285: getMembers.BulkOperationId = distributeCampaignResponse.BulkOperationId
286: getMembers.BulkOperationSource = BulkOperationSource.CampaignActivity
287: getMembers.EntitySource = EntitySource.Account
288: getMembers.ReturnDynamicEntities = False
289:
290: Dim membersResponse As RetrieveMembersBulkOperationResponse = CType(service.Execute(getMembers), RetrieveMembersBulkOperationResponse)
291:
292: ' #Region "check success"
293:
294: Dim returnSet As New ColumnSet()
295: returnSet.Attributes = New String() {"activityid", "statuscode"}
296:
297: ' Wait for the bulk operation to complete.
298: Dim retrieveMembersOperation As CrmSdk.bulkoperation = TryCast(service.Retrieve(EntityName.bulkoperation.ToString(), distributeCampaignResponse.BulkOperationId, returnSet), CrmSdk.bulkoperation)
299: Dim secondsTicker As Integer = 20
300: Do While secondsTicker > 0 AndAlso retrieveMembersOperation.statuscode.Value <> BulkOperationStatus.Completed
301: retrieveMembersOperation = TryCast(service.Retrieve(EntityName.bulkoperation.ToString(), distributeCampaignResponse.BulkOperationId, returnSet), CrmSdk.bulkoperation)
302: System.Threading.Thread.Sleep(1000)
303: secondsTicker -= 1
304: Loop
305:
306: ' Verify the account created and added to the marketing list was returned.
307: If retrieveMembersOperation.statuscode.Value = BulkOperationStatus.Completed AndAlso membersResponse.BusinessEntityCollection.BusinessEntities.Length = 1 Then
308: Dim verifyAccount As account = TryCast(membersResponse.BusinessEntityCollection.BusinessEntities(0), account)
309: If verifyAccount.accountid.Value = createdAccountId Then
310: success = True
311: End If
312: End If
313:
314: ' #End Region
315:
316: ' #Region "Remove Data Required for this Sample"
317:
318: ' Query for all phonecall activities created so that they can be removed.
319: Dim resultSet As New ColumnSet()
320: resultSet.Attributes = New String() {"activityid"}
321:
322: Dim phoneCallCondition As New ConditionExpression()
323: phoneCallCondition.AttributeName = "regardingobjectid"
324: phoneCallCondition.Operator = ConditionOperator.Equal
325: phoneCallCondition.Values = New Object() {createdCampaignActivityId}
326:
327: Dim regardingObjectFilter As New FilterExpression()
328: regardingObjectFilter.Conditions = New ConditionExpression() {phoneCallCondition}
329: regardingObjectFilter.FilterOperator = LogicalOperator.And
330:
331: Dim phoneCallQuery As New QueryExpression()
332: phoneCallQuery.ColumnSet = resultSet
333: phoneCallQuery.Criteria = regardingObjectFilter
334: phoneCallQuery.EntityName = EntityName.phonecall.ToString()
335:
336: Dim phoneCalls As BusinessEntityCollection = service.RetrieveMultiple(phoneCallQuery)
337:
338: For Each aPhoneCall As phonecall In phoneCalls.BusinessEntities
339: service.Delete(EntityName.phonecall.ToString(), aPhoneCall.activityid.Value)
340: Next aPhoneCall
341:
342: ' Remove the campaign activity.
343: service.Delete(EntityName.campaignactivity.ToString(), createdCampaignActivityId)
344:
345: ' Remove the campaign.
346: service.Delete(EntityName.campaign.ToString(), createdCampaignId)
347:
348: ' Remove the marketing list.
349: service.Delete(EntityName.list.ToString(), createdMarketingListId)
350:
351: ' Remove the account.
352: service.Delete(EntityName.account.ToString(), createdAccountId)
353:
354: ' #End Region
355:
356: Return success
357: End Function
358: End Class
359: End Namespace