Tuesday, 1 May 2012

Creating and Distributing Quick Campaign in CRM 2011


In this article , I am going to explain , how to create and distribute Quick Campaign .

There are two method of Creating Campaign
(i)  Using List Class
(ii) Using Query Expression


First Method : Code to Create Quick Campaign with marketing list as input

   List newList = new List()
   {
     ListName = "TestList",
     CreatedFromCode = new OptionSetValue((int)ListCreatedFromCode.Account)
   };
   Guid _newListId = _serviceProxy.Create(newList);

   for (int j = 0; j < 5; j++)
   {
      AddMemberListRequest addMemberListRequest = new AddMemberListRequest();
      addMemberListRequest.EntityId = _accountIdArray[j];
      addMemberListRequest.ListId = _newListId;
      AddMemberListResponse addMemberListResponse =
      _serviceProxy.Execute(addMemberListRequest) as AddMemberListResponse;
   }

   //Create the request object from input parameters
   CreateActivitiesListRequest request = new CreateActivitiesListRequest()
   {
      Activity = new Letter()
      {
          Subject = "qcCreatedLetterActivity"
      },
      ListId = _newListId,
      OwnershipOptions = PropagationOwnershipOptions.ListMemberOwner,
      Propagate = true,
      TemplateId = Guid.Empty,
      FriendlyName = "Quick Campaign for My List",
      Owner = new EntityReference("systemuser", _currentUser),
      PostWorkflowEvent = true ;

   //Execute the request
   CreateActivitiesListResponse response = (CreateActivitiesListResponse)_serviceProxy.Execute(request);
   Guid BOId = response.BulkOperationId;



Second Method : Code to Create Quick Campaign with Query Expression as input

   QueryExpression query = new QueryExpression("account");
   query.ColumnSet = new ColumnSet("accountid");
   query.Criteria = new FilterExpression();
   FilterExpression filter = query.Criteria.AddFilter(LogicalOperator.Or);
   for (int j = 0; j < 5; j++)
   {
     filter.AddCondition("accountid", ConditionOperator.Equal, _accountIdArray[j]);
   }
                 
   // create the bulkoperation
   PropagateByExpressionRequest qrequest = new PropagateByExpressionRequest()
   {
       Activity = new Email()
            {
               Subject = "qcCreatedEmailActivity"
            },
       ExecuteImmediately = false, // Default value.
       FriendlyName = "Query Based Quick Campaign",
       OwnershipOptions = PropagationOwnershipOptions.ListMemberOwner,
       QueryExpression = query,
       Owner = new EntityReference("systemuser", _currentUser),
       PostWorkflowEvent = true,
       SendEmail = false,
       TemplateId = Guid.Empty
   };

   PropagateByExpressionResponse qresponse =
                        (PropagateByExpressionResponse)_serviceProxy.Execute(qrequest);

   Guid bulkOpId = response.BulkOperationId;



Complete Sample Code

using (OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri,
              serverConfig.HomeRealmUri,
              serverConfig.Credentials,
              serverConfig.DeviceCredentials))
{
   _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

   WhoAmIRequest whoRequest = new WhoAmIRequest();
   Guid _currentUser = ((WhoAmIResponse)_serviceProxy.Execute(whoRequest)).UserId;

   // Create accounts on which we want to run Quick Campaign
   Guid[] _accountIdArray = new Guid[5];
   for (int i = 0; i < 5; i++)
   {
      Account acct = new Account()
      {
           Name = "Account For Quick Campaign " + i.ToString()
      };
      _accountIdArray[i] = _serviceProxy.Create(acct);
   }


   // Run a Quick Campaign with marketing list as input/
   List newList = new List()
   {
     ListName = "TestList",
     CreatedFromCode = new OptionSetValue((int)ListCreatedFromCode.Account)
   };
   Guid _newListId = _serviceProxy.Create(newList);

   for (int j = 0; j < 5; j++)
   {
      AddMemberListRequest addMemberListRequest = new AddMemberListRequest();
      addMemberListRequest.EntityId = _accountIdArray[j];
      addMemberListRequest.ListId = _newListId;
      AddMemberListResponse addMemberListResponse =
      _serviceProxy.Execute(addMemberListRequest) as AddMemberListResponse;
   }

   //Create the request object from input parameters
   CreateActivitiesListRequest request = new CreateActivitiesListRequest()
   {
      Activity = new Letter()
      {
          Subject = "qcCreatedLetterActivity"
      },
      ListId = _newListId,
      OwnershipOptions = PropagationOwnershipOptions.ListMemberOwner,
      Propagate = true,
      TemplateId = Guid.Empty,
      FriendlyName = "Quick Campaign for My List",
      Owner = new EntityReference("systemuser", _currentUser),
      PostWorkflowEvent = true
   ;

   //Execute the request
   CreateActivitiesListResponse response = (CreateActivitiesListResponse)_serviceProxy.Execute(request);
   Guid BOId = response.BulkOperationId;

                 

   // Run a Quick Campaign with a list of accounts as input

   // Construct a Query Expression(QE) which specifies which records QC should include              
   QueryExpression query = new QueryExpression("account");
   query.ColumnSet = new ColumnSet("accountid");
   query.Criteria = new FilterExpression();
   FilterExpression filter = query.Criteria.AddFilter(LogicalOperator.Or);
   for (int j = 0; j < 5; j++)
   {
     filter.AddCondition("accountid", ConditionOperator.Equal, _accountIdArray[j]);
   }
                 
   // create the bulkoperation
   PropagateByExpressionRequest qrequest = new PropagateByExpressionRequest()
   {
       Activity = new Email()
            {
               Subject = "qcCreatedEmailActivity"
            },
       ExecuteImmediately = false, // Default value.
       FriendlyName = "Query Based Quick Campaign",
       OwnershipOptions = PropagationOwnershipOptions.ListMemberOwner,
       QueryExpression = query,
       Owner = new EntityReference("systemuser", _currentUser),
       PostWorkflowEvent = true,
       SendEmail = false,
       TemplateId = Guid.Empty
   };

   PropagateByExpressionResponse qresponse =
                        (PropagateByExpressionResponse)_serviceProxy.Execute(qrequest);

   Guid bulkOpId = response.BulkOperationId;
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.