In this Article, I am going to explain how to create Campaign and Associate Campaign with Static and Dynamic List
For Better Understanding I divided this article in four part
(i) Code to create campaign
(ii) Create and Associate Dynamics List to campaign
(iii) Create and Associate Static List to campaign
(iv) Distribute a campaign
Namespace need to Include
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
Code to create campaign
// Create a campaign.
Campaign campaign = new Campaign()
{
Name = "Sample Campaign"
};
Guid _campaignId = _serviceProxy.Create(campaign);
campaign.Id = _campaignId;
Code to create and Associate Dynamics List to campaign
//Create Dynamic List
String fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='account'>
<attribute name='name' />
<attribute name='address1_city' />
<attribute name='primarycontactid' />
<attribute name='telephone1' />
<attribute name='accountid' />
<order attribute='name' descending='false' />
<filter type='and'>
<condition attribute='address1_city' operator='eq' value='chandigarh' />
</filter>
</entity>
</fetch>";
// Create dynamic list. Set the type to true to declare a dynamic
// list.
List dynamicList = new List()
{
Type = true,
ListName = "Dynamic List",
CreatedFromCode = new OptionSetValue((int)ListCreatedFromCode.Account),
Query = fetchXml
};
Guid _dynamicListId = _serviceProxy.Create(dynamicList);
dynamicList.Id = _dynamicListId;
//Associate dynamic list to campaign
// Add the dynamic list to the campaign.
AddItemCampaignRequest addListToCampaignRequest =
new AddItemCampaignRequest()
{
CampaignId = _campaignId,
EntityId = _dynamicListId,
EntityName = List.EntityLogicalName,
};
_serviceProxy.Execute(addListToCampaignRequest);
// Create a campaign activity to distribute fax to the list members.
CampaignActivity campaignActivity = new CampaignActivity()
{
Subject = "Sample Campaign Activity",
ChannelTypeCode = new OptionSetValue((int)CampaignActivityChannelTypeCode.Fax),
RegardingObjectId = campaign.ToEntityReference()
};
Guid _campaignActivityId = _serviceProxy.Create(campaignActivity);
// Add dynamic list to campaign activity.
AddItemCampaignActivityRequest addListToCampaignActivityRequest =
new AddItemCampaignActivityRequest()
{
CampaignActivityId = _campaignActivityId,
ItemId = _dynamicListId,
EntityName = List.EntityLogicalName
};
_serviceProxy.Execute(addListToCampaignActivityRequest);
Code to create and Associate Static List to campaign
// Add the static list to the campaign.
AddItemCampaignRequest addStaticListToCampaignRequest =
new AddItemCampaignRequest()
{
CampaignId = _campaignId,
EntityId = _staticListId,
EntityName = List.EntityLogicalName
};
_serviceProxy.Execute(addStaticListToCampaignRequest);
// Add the static list to the campaign activity.
AddItemCampaignActivityRequest addStaticListToCampaignActivityRequest =
new AddItemCampaignActivityRequest()
{
CampaignActivityId = _campaignActivityId,
ItemId = _staticListId,
EntityName = List.EntityLogicalName
};
_serviceProxy.Execute(addStaticListToCampaignActivityRequest);
Code to Distribute a campaign
// Create a fax.
Fax fax = new Fax()
{
Subject = "Example Fax"
};
// Distribute the campaign activity to the marketing lists.
DistributeCampaignActivityRequest distributeRequest =
new DistributeCampaignActivityRequest()
{
CampaignActivityId = _campaignActivityId,
Activity = fax,
Owner = new EntityReference("systemuser", _salesManagerId),
Propagate = true,
SendEmail = false,
PostWorkflowEvent = true
};
_serviceProxy.Execute(distributeRequest);
Complete Code
using (OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri,
serverConfig.HomeRealmUri,
serverConfig.Credentials,
serverConfig.DeviceCredentials))
{
_serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
// Retrieve a sales manager.
Guid _salesManagerId = new Guid("ffbb6e3f-ccfe-4d84-90d9-421418b03a8e");
Guid _accountId = new Guid("9fbb2e3f-ggfe-4d84-90d9-423245b03a8e");
// Create a campaign.
Campaign campaign = new Campaign()
{
Name = "Sample Campaign"
};
Guid _campaignId = _serviceProxy.Create(campaign);
campaign.Id = _campaignId;
//Create Dynamic List
String fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='account'>
<attribute name='name' />
<attribute name='address1_city' />
<attribute name='primarycontactid' />
<attribute name='telephone1' />
<attribute name='accountid' />
<order attribute='name' descending='false' />
<filter type='and'>
<condition attribute='address1_city' operator='eq' value='chandigarh' />
</filter>
</entity>
</fetch>";
// Create dynamic list. Set the type to true to declare a dynamic
// list.
List dynamicList = new List()
{
Type = true,
ListName = "Dynamic List",
CreatedFromCode = new OptionSetValue((int)ListCreatedFromCode.Account),
Query = fetchXml
};
Guid _dynamicListId = _serviceProxy.Create(dynamicList);
dynamicList.Id = _dynamicListId;
//Associate dynamic list to campaign
// Add the dynamic list to the campaign.
AddItemCampaignRequest addListToCampaignRequest =
new AddItemCampaignRequest()
{
CampaignId = _campaignId,
EntityId = _dynamicListId,
EntityName = List.EntityLogicalName,
};
_serviceProxy.Execute(addListToCampaignRequest);
// Create a campaign activity to distribute fax to the list members.
CampaignActivity campaignActivity = new CampaignActivity()
{
Subject = "Sample Campaign Activity",
ChannelTypeCode = new OptionSetValue((int)CampaignActivityChannelTypeCode.Fax),
RegardingObjectId = campaign.ToEntityReference()
};
Guid _campaignActivityId = _serviceProxy.Create(campaignActivity);
// Add dynamic list to campaign activity.
AddItemCampaignActivityRequest addListToCampaignActivityRequest =
new AddItemCampaignActivityRequest()
{
CampaignActivityId = _campaignActivityId,
ItemId = _dynamicListId,
EntityName = List.EntityLogicalName
};
_serviceProxy.Execute(addListToCampaignActivityRequest);
// Associate static list to campaign
// Copy the dynamic list to a static list.
CopyDynamicListToStaticRequest copyRequest =
new CopyDynamicListToStaticRequest()
{
ListId = _dynamicListId
};
CopyDynamicListToStaticResponse copyResponse =
(CopyDynamicListToStaticResponse)_serviceProxy.Execute(copyRequest);
Guid _staticListId = copyResponse.StaticListId;
// Add the static list to the campaign.
AddItemCampaignRequest addStaticListToCampaignRequest =
new AddItemCampaignRequest()
{
CampaignId = _campaignId,
EntityId = _staticListId,
EntityName = List.EntityLogicalName
};
_serviceProxy.Execute(addStaticListToCampaignRequest);
// Add the static list to the campaign activity.
AddItemCampaignActivityRequest addStaticListToCampaignActivityRequest =
new AddItemCampaignActivityRequest()
{
CampaignActivityId = _campaignActivityId,
ItemId = _staticListId,
EntityName = List.EntityLogicalName
};
_serviceProxy.Execute(addStaticListToCampaignActivityRequest);
// Create a fax.
Fax fax = new Fax()
{
Subject = "Example Fax"
};
// Distribute the campaign activity to the marketing lists.
DistributeCampaignActivityRequest distributeRequest =
new DistributeCampaignActivityRequest()
{
CampaignActivityId = _campaignActivityId,
Activity = fax,
Owner = new EntityReference("systemuser", _salesManagerId),
Propagate = true,
SendEmail = false,
PostWorkflowEvent = true
};
_serviceProxy.Execute(distributeRequest);
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.