Search

Create a Campaign

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.



using System;
using CrmSdk;
using Microsoft.Crm.Sdk.Utility;
using System.Web.Services.Protocols;

namespace Microsoft.Crm.Sdk.Reference.BulkOperation
{
public class RetrieveMembersBulkOperation
{

public RetrieveMembersBulkOperation()
{

}

public static bool Run(string crmServerUrl, string orgName)
{
bool success = false;

// Set up the CRM Service.
CrmService service = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetCrmService(crmServerUrl, orgName);
service.PreAuthenticate = true;

#region Setup Data Required for this Sample

// Create a campaign.
campaign sampleCampaign = new campaign();
sampleCampaign.name = "SDK Sample Campaign";

Guid createdCampaignId = service.Create(sampleCampaign);

// Create a campaign activity.
campaignactivity sampleActivity = new campaignactivity();
sampleActivity.regardingobjectid = new Lookup();
sampleActivity.regardingobjectid.type = EntityName.campaign.ToString();
//sampleActivity.regardingobjectid.Value = createdCampaign.id;
sampleActivity.regardingobjectid.Value = createdCampaignId;
sampleActivity.subject = "Sample Campaign Activity";
sampleActivity.channeltypecode = new Picklist(1); // 1 == phone

Guid createdCampaignActivityId = service.Create(sampleActivity);

// Create a list to add to the activity.
list marketingList = new list();
marketingList.listname = "SDK Sample Marketing List";
marketingList.createdfromcode = new Picklist();
marketingList.createdfromcode.Value = 1; // 1 == account

Guid createdMarketingListId = service.Create(marketingList);

// Create an account to add to the marketing list.
// This will be the name returned by the RetrieveMembersBulkOperation message.
account sampleAccount = new account();
sampleAccount.name = "Fourth Coffee";

Guid createdAccountId = service.Create(sampleAccount);

AddMemberListRequest addAccountRequest = new AddMemberListRequest();
addAccountRequest.ListId = createdMarketingListId;
addAccountRequest.EntityId = createdAccountId;

service.Execute(addAccountRequest);

// First, associate the list with the campaign.
AddItemCampaignRequest addListToCampaignRequest = new AddItemCampaignRequest();
addListToCampaignRequest.CampaignId = createdCampaignId;
addListToCampaignRequest.EntityName = EntityName.list;
addListToCampaignRequest.EntityId = createdMarketingListId;

AddItemCampaignResponse createdCampaignItem = (AddItemCampaignResponse)service.Execute(addListToCampaignRequest);

// Then, associate the list with the campaign activity.
AddItemCampaignActivityRequest addListToCampaignActivityRequest = new AddItemCampaignActivityRequest();
addListToCampaignActivityRequest.CampaignActivityId = createdCampaignActivityId;
addListToCampaignActivityRequest.EntityName = EntityName.list;
addListToCampaignActivityRequest.ItemId = createdMarketingListId;

AddItemCampaignActivityResponse createdCampaignActivityItem = (AddItemCampaignActivityResponse)service.Execute(addListToCampaignActivityRequest);

#endregion

// Create a phone activity to be distributed (bulk operation).
phonecall samplePhoneCall = new phonecall();
samplePhoneCall.subject = "Sample phone call to distribute to a marketing list.";

// The owner property is REQUIRED.
WhoAmIRequest systemUserRequest = new WhoAmIRequest();
WhoAmIResponse systemUser = (WhoAmIResponse)service.Execute(systemUserRequest);

// Execute a bulk operation.
DistributeCampaignActivityRequest distributeCampaignRequest = new DistributeCampaignActivityRequest();
distributeCampaignRequest.Activity = samplePhoneCall;
distributeCampaignRequest.CampaignActivityId = createdCampaignActivityId;
distributeCampaignRequest.Propagate = true;
distributeCampaignRequest.SendEmail = false;
distributeCampaignRequest.Owner = new Moniker();
distributeCampaignRequest.Owner.Id = systemUser.UserId;
distributeCampaignRequest.Owner.Name = EntityName.systemuser.ToString();

DistributeCampaignActivityResponse distributeCampaignResponse = (DistributeCampaignActivityResponse)service.Execute(distributeCampaignRequest);

// Execute the request.
RetrieveMembersBulkOperationRequest getMembers = new RetrieveMembersBulkOperationRequest();
getMembers.BulkOperationId = distributeCampaignResponse.BulkOperationId;
getMembers.BulkOperationSource = BulkOperationSource.CampaignActivity;
getMembers.EntitySource = EntitySource.Account;
getMembers.ReturnDynamicEntities = false;

RetrieveMembersBulkOperationResponse membersResponse = (RetrieveMembersBulkOperationResponse)service.Execute(getMembers);

#region check success

ColumnSet returnSet = new ColumnSet();
returnSet.Attributes = new string[] { "activityid", "statuscode" };

// Wait for the bulk operation to complete.
bulkoperation retrieveMembersOperation = service.Retrieve(EntityName.bulkoperation.ToString(), distributeCampaignResponse.BulkOperationId, returnSet) as bulkoperation;
int secondsTicker = 20;
while (secondsTicker > 0 && retrieveMembersOperation.statuscode.Value != BulkOperationStatus.Completed)
{
retrieveMembersOperation = service.Retrieve(EntityName.bulkoperation.ToString(), distributeCampaignResponse.BulkOperationId, returnSet) as bulkoperation;
System.Threading.Thread.Sleep(1000);
secondsTicker--;
}

// Verify that the account created and added to the marketing list was returned.
if (retrieveMembersOperation.statuscode.Value == BulkOperationStatus.Completed && membersResponse.BusinessEntityCollection.BusinessEntities.Length == 1)
{
account verifyAccount = membersResponse.BusinessEntityCollection.BusinessEntities[0] as account;
if (verifyAccount.accountid.Value == createdAccountId)
{
success = true;
}
}

#endregion

#region Remove Data Required for this Sample

// Query for all phonecall activities created so that they can be removed.
ColumnSet resultSet = new ColumnSet();
resultSet.Attributes = new string[] {"activityid"};

ConditionExpression phoneCallCondition = new ConditionExpression();
phoneCallCondition.AttributeName = "regardingobjectid";
phoneCallCondition.Operator = ConditionOperator.Equal;
phoneCallCondition.Values = new object[] {createdCampaignActivityId};

FilterExpression regardingObjectFilter = new FilterExpression();
regardingObjectFilter.Conditions = new ConditionExpression[] { phoneCallCondition };
regardingObjectFilter.FilterOperator = LogicalOperator.And;

QueryExpression phoneCallQuery = new QueryExpression();
phoneCallQuery.ColumnSet = resultSet;
phoneCallQuery.Criteria = regardingObjectFilter;
phoneCallQuery.EntityName = EntityName.phonecall.ToString();

BusinessEntityCollection phoneCalls = service.RetrieveMultiple(phoneCallQuery);

foreach (phonecall aPhoneCall in phoneCalls.BusinessEntities)
{
service.Delete(EntityName.phonecall.ToString(), aPhoneCall.activityid.Value);
}

// Remove the campaign activity.
service.Delete(EntityName.campaignactivity.ToString(), createdCampaignActivityId);

// Remove the campaign.
service.Delete(EntityName.campaign.ToString(), createdCampaignId);

// Remove the marketing list.
service.Delete(EntityName.list.ToString(), createdMarketingListId);

// Remove the account.
service.Delete(EntityName.account.ToString(), createdAccountId);

#endregion

return success;
}
}
}