Export (0) Print
Expand All

Create a Campaign

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

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.

This sample code can be found in the following files in the SDK download:

Server\Reference\CS\BulkOperation\RetrieveMembersBulkOperation.cs
Server\Reference\VB\BulkOperation\RetrieveMembersBulkOperation.vb

For more information about the helper methods in the Microsoft.Crm.Sdk.Utility.CrmServiceUtility namespace, see Utility Sample Code.

Example

[C#]

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;
        }
    }
}

[Visual Basic .NET]

Imports Microsoft.VisualBasic
Imports System
Imports CrmSdk
Imports Microsoft.Crm.Sdk.Utility

Namespace Microsoft.Crm.Sdk.Reference.BulkOperation
    Public Class RetrieveMembersBulkOperation
        Public Sub New()

        End Sub

        Public Shared Function Run(ByVal crmServerUrl As String, ByVal orgName As String) As Boolean
            Dim success As Boolean = False

            ' Set up the CRM Service.  
            Dim service As CrmService = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetCrmService(crmServerUrl, orgName)
            service.PreAuthenticate = True

            '            #Region "Setup Data Required for this Sample"

            ' Create a campaign.
            Dim sampleCampaign As New campaign()
            sampleCampaign.name = "SDK Sample Campaign"

            Dim createdCampaignId As Guid = service.Create(sampleCampaign)

            ' Create a campaign activity.
            Dim sampleActivity As New campaignactivity()
            sampleActivity.regardingobjectid = New Lookup()
            sampleActivity.regardingobjectid.type = EntityName.campaign.ToString()
            sampleActivity.regardingobjectid.Value = createdCampaignId
            sampleActivity.subject = "Sample Campaign Activity"
            sampleActivity.channeltypecode = New Picklist(1) ' 1 == phone

            Dim createdCampaignActivityId As Guid = service.Create(sampleActivity)

            ' Create a list to add to the activity.
            Dim marketingList As New list()
            marketingList.listname = "SDK Sample Marketing List"
            marketingList.createdfromcode = New Picklist()
            marketingList.createdfromcode.Value = 1 ' 1 == account

            Dim createdMarketingListId As Guid = service.Create(marketingList)

            ' Create an account to add to the marketing list.  
            ' This will be the name returned by the RetrieveMembersBulkOperation message.
            Dim sampleAccount As New account()
            sampleAccount.name = "Fourth Coffee"

            Dim createdAccountId As Guid = service.Create(sampleAccount)

            Dim addAccountRequest As New AddMemberListRequest()
            addAccountRequest.ListId = createdMarketingListId
            addAccountRequest.EntityId = createdAccountId

            service.Execute(addAccountRequest)

            ' First, associate the list with the campaign.
            Dim addListToCampaignRequest As New AddItemCampaignRequest()
            addListToCampaignRequest.CampaignId = createdCampaignId
            addListToCampaignRequest.EntityName = EntityName.list
            addListToCampaignRequest.EntityId = createdMarketingListId

            Dim createdCampaignItem As AddItemCampaignResponse = CType(service.Execute(addListToCampaignRequest), AddItemCampaignResponse)

            ' Then, associate the list with the campaign activity.
            Dim addListToCampaignActivityRequest As New AddItemCampaignActivityRequest()
            addListToCampaignActivityRequest.CampaignActivityId = createdCampaignActivityId
            addListToCampaignActivityRequest.EntityName = EntityName.list
            addListToCampaignActivityRequest.ItemId = createdMarketingListId

            Dim createdCampaignActivityItem As AddItemCampaignActivityResponse = CType(service.Execute(addListToCampaignActivityRequest), AddItemCampaignActivityResponse)

            '            #End Region

            ' Create a phone activity to be distributed (bulk operation).
            Dim samplePhoneCall As New phonecall()
            samplePhoneCall.subject = "Sample phone call to distribute to a marketing list."

            ' The owner property is a REQUIRED property.
            Dim systemUserRequest As New WhoAmIRequest()
            Dim systemUser As WhoAmIResponse = CType(service.Execute(systemUserRequest), WhoAmIResponse)

            ' Execute a bulk operation.
            Dim distributeCampaignRequest As 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()

            Dim distributeCampaignResponse As DistributeCampaignActivityResponse = CType(service.Execute(distributeCampaignRequest), DistributeCampaignActivityResponse)

            ' Execute the request.
            Dim getMembers As New RetrieveMembersBulkOperationRequest()
            getMembers.BulkOperationId = distributeCampaignResponse.BulkOperationId
            getMembers.BulkOperationSource = BulkOperationSource.CampaignActivity
            getMembers.EntitySource = EntitySource.Account
            getMembers.ReturnDynamicEntities = False

            Dim membersResponse As RetrieveMembersBulkOperationResponse = CType(service.Execute(getMembers), RetrieveMembersBulkOperationResponse)

            '            #Region "check success"

            Dim returnSet As New ColumnSet()
            returnSet.Attributes = New String() {"activityid", "statuscode"}

            ' Wait for the bulk operation to complete.
            Dim retrieveMembersOperation As CrmSdk.bulkoperation = TryCast(service.Retrieve(EntityName.bulkoperation.ToString(), distributeCampaignResponse.BulkOperationId, returnSet), CrmSdk.bulkoperation)
            Dim secondsTicker As Integer = 20
            Do While secondsTicker > 0 AndAlso retrieveMembersOperation.statuscode.Value <> BulkOperationStatus.Completed
                retrieveMembersOperation = TryCast(service.Retrieve(EntityName.bulkoperation.ToString(), distributeCampaignResponse.BulkOperationId, returnSet), CrmSdk.bulkoperation)
                System.Threading.Thread.Sleep(1000)
                secondsTicker -= 1
            Loop

            ' Verify the account created and added to the marketing list was returned.
            If retrieveMembersOperation.statuscode.Value = BulkOperationStatus.Completed AndAlso membersResponse.BusinessEntityCollection.BusinessEntities.Length = 1 Then
                Dim verifyAccount As account = TryCast(membersResponse.BusinessEntityCollection.BusinessEntities(0), account)
                If verifyAccount.accountid.Value = createdAccountId Then
                    success = True
                End If
            End If

            '            #End Region

            '            #Region "Remove Data Required for this Sample"

            ' Query for all phonecall activities created so that they can be removed.
            Dim resultSet As New ColumnSet()
            resultSet.Attributes = New String() {"activityid"}

            Dim phoneCallCondition As New ConditionExpression()
            phoneCallCondition.AttributeName = "regardingobjectid"
            phoneCallCondition.Operator = ConditionOperator.Equal
            phoneCallCondition.Values = New Object() {createdCampaignActivityId}

            Dim regardingObjectFilter As New FilterExpression()
            regardingObjectFilter.Conditions = New ConditionExpression() {phoneCallCondition}
            regardingObjectFilter.FilterOperator = LogicalOperator.And

            Dim phoneCallQuery As New QueryExpression()
            phoneCallQuery.ColumnSet = resultSet
            phoneCallQuery.Criteria = regardingObjectFilter
            phoneCallQuery.EntityName = EntityName.phonecall.ToString()

            Dim phoneCalls As BusinessEntityCollection = service.RetrieveMultiple(phoneCallQuery)

            For Each aPhoneCall As phonecall In phoneCalls.BusinessEntities
                service.Delete(EntityName.phonecall.ToString(), aPhoneCall.activityid.Value)
            Next aPhoneCall

            ' 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)

            '            #End Region

            Return success
        End Function
    End Class
End Namespace

See Also

Reference


© 2010 Microsoft Corporation. All rights reserved.


Show:
© 2014 Microsoft