private void CopyToStaticList(MarketingListItem item) { WorkAsync( new WorkAsyncInfo { Message = "Start copy of list", Work = (w, ae) => { Guid _staticListId = Guid.Empty; // Copy the dynamic list to a static list. CopyDynamicListToStaticRequest copyRequest = new CopyDynamicListToStaticRequest() { ListId = item.Id }; CopyDynamicListToStaticResponse copyResponse = (CopyDynamicListToStaticResponse)Service.Execute(copyRequest); _staticListId = copyResponse.StaticListId; CopyUrlToClipboard(new MarketingListItem { Id = _staticListId }); w.ReportProgress(99, string.Format("Copied dynamic list to a static list with id {0}", _staticListId)); }, PostWorkCallBack = ae => { ExecuteMethod(ProcessLoadLists); }, ProgressChanged = ae => { SetWorkingMessage(ae.UserState.ToString()); } } ); }
/// <summary> /// Create a <c>Static List</c> from the specified dynamic list and add the members that satisfy the dynamic list query criteria to the static list. /// Please note that newly created static list is <c>Locked</c> and you can not add new members to created <c>Static List</c>. /// If you need to unlock the list you can call <see cref="Unlock(Guid)"/> method. /// <para> /// For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.copydynamiclisttostaticrequest(v=crm.7).aspx /// </para> /// </summary> /// <param name="id"><c>Dynamic</c> Marketing List Id</param> /// <param name="createdListName"> /// If you want set a new and known name please set this, otherwise MS CRM gives a unique name to newly created static list. /// </param> /// <returns> /// Returns created <c>Static</c> Marketing List Id in <see cref="CopyDynamicListToStaticResponse.StaticListId"/> property. /// </returns> public CopyDynamicListToStaticResponse CopyDynamicListToStaticList(Guid id, string createdListName) { ExceptionThrow.IfGuidEmpty(id, "id"); CopyDynamicListToStaticRequest request = new CopyDynamicListToStaticRequest() { ListId = id }; var result = (CopyDynamicListToStaticResponse)this.OrganizationService.Execute(request); if (!string.IsNullOrEmpty(createdListName)) { UpdateName(result.StaticListId, createdListName); } return(result); }
[STAThread] // Required to support the interactive login experience static void Main(string[] args) { CrmServiceClient service = null; try { service = SampleHelpers.Connect("Connect"); if (service.IsReady) { // Create any entity records that the demonstration code requires SetUpSample(service); #region Demonstrate // Create FetchXml for marketing list's query which locates accounts // in Seattle. 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='seattle' /> </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 }; _dynamicListId = service.Create(dynamicList); dynamicList.Id = _dynamicListId; Console.WriteLine("Created dynamic list."); #endregion #region Associate dynamic list to campaign // Create a campaign. var campaign = new Campaign() { Name = "Sample Campaign" }; _campaignId = service.Create(campaign); campaign.Id = _campaignId; // Add the dynamic list to the campaign. var addListToCampaignRequest = new AddItemCampaignRequest() { CampaignId = _campaignId, EntityId = _dynamicListId, EntityName = List.EntityLogicalName, }; service.Execute(addListToCampaignRequest); Console.WriteLine("Added dynamic list to the campaign."); // 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() }; _campaignActivityId = service.Create(campaignActivity); // Add dynamic list to campaign activity. var addListToCampaignActivityRequest = new AddItemCampaignActivityRequest() { CampaignActivityId = _campaignActivityId, ItemId = _dynamicListId, EntityName = List.EntityLogicalName }; service.Execute(addListToCampaignActivityRequest); Console.WriteLine("Added dynamic list to the campaign activity."); #endregion #region Associate static list to campaign // Copy the dynamic list to a static list. var copyRequest = new CopyDynamicListToStaticRequest() { ListId = _dynamicListId }; var copyResponse = (CopyDynamicListToStaticResponse)service.Execute(copyRequest); _staticListId = copyResponse.StaticListId; Console.WriteLine("Copied dynamic list to a static list."); // Add the static list to the campaign. var addStaticListToCampaignRequest = new AddItemCampaignRequest() { CampaignId = _campaignId, EntityId = _staticListId, EntityName = List.EntityLogicalName }; service.Execute(addStaticListToCampaignRequest); Console.WriteLine("Added static list to the campaign."); // Add the static list to the campaign activity. var addStaticListToCampaignActivityRequest = new AddItemCampaignActivityRequest() { CampaignActivityId = _campaignActivityId, ItemId = _staticListId, EntityName = List.EntityLogicalName }; service.Execute(addStaticListToCampaignActivityRequest); Console.WriteLine("Added static list to the campaign's activity."); #endregion #region Create fax for campaign's activity // Create a fax. var fax = new Fax() { Subject = "Example Fax" }; Console.WriteLine("Created fax for campaign's activity."); #endregion Create fax for campaign's activity #region Distribute fax to the marketing list // Distribute the campaign activity to the marketing lists. var distributeRequest = new DistributeCampaignActivityRequest() { CampaignActivityId = _campaignActivityId, Activity = fax, Owner = new EntityReference("systemuser", _salesManagerId), Propagate = true, SendEmail = false, PostWorkflowEvent = true }; service.Execute(distributeRequest); Console.WriteLine("Distributed fax to the marketing lists."); #endregion Distribute fax to the marketing list #region Retrieve collection of entities from marketing list // Retrieve a collection of entities that correspond // to all of the members in a marketing list // This approach of retrieving list members allows you to dynamically // retrieve the members of a list programmatically without requiring // knowledge of the member entity type. OrganizationServiceContext orgContext = new OrganizationServiceContext(service); var member = (from mb in orgContext.CreateQuery <List>() where mb.Id == _dynamicListId select mb).FirstOrDefault(); string fetchQuery = member.Query; var memberRequest = new RetrieveMultipleRequest(); FetchExpression fetch = new FetchExpression(fetchQuery); memberRequest.Query = fetch; var memberResponse = (RetrieveMultipleResponse)service.Execute(memberRequest); Console.WriteLine("Retrieved collection of entities from a marketing list."); #endregion Demonstrate #region Clean up CleanUpSample(service); #endregion Clean up } else { const string UNABLE_TO_LOGIN_ERROR = "Unable to Login to Common Data Service"; if (service.LastCrmError.Equals(UNABLE_TO_LOGIN_ERROR)) { Console.WriteLine("Check the connection string values in cds/App.config."); throw new Exception(service.LastCrmError); } else { throw service.LastCrmException; } } } catch (Exception ex) { SampleHelpers.HandleException(ex); } finally { if (service != null) { service.Dispose(); } Console.WriteLine("Press <Enter> to exit."); Console.ReadLine(); } }
/// <summary> /// This method first connects to the Organization service. Afterwards a dynamic /// list is created and associated to the campaign and the campaign's activity. /// Then the dynamic list is copied to a static list and associated with the same /// campaign and campaign activity. Finally the sample distributes the campaign /// to both the dynamic and static lists. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptforDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete) { try { // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); CreateRequiredRecords(); #region Create Dynamic List // Create FetchXml for marketing list's query which locates accounts // in Seattle. 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='seattle' /> </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 }; _dynamicListId = _serviceProxy.Create(dynamicList); dynamicList.Id = _dynamicListId; Console.WriteLine("Created dynamic list."); #endregion #region Associate dynamic list to campaign // Create a campaign. Campaign campaign = new Campaign() { Name = "Sample Campaign" }; _campaignId = _serviceProxy.Create(campaign); campaign.Id = _campaignId; // Add the dynamic list to the campaign. AddItemCampaignRequest addListToCampaignRequest = new AddItemCampaignRequest() { CampaignId = _campaignId, EntityId = _dynamicListId, EntityName = List.EntityLogicalName, }; _serviceProxy.Execute(addListToCampaignRequest); Console.WriteLine("Added dynamic list to the campaign."); // 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() }; _campaignActivityId = _serviceProxy.Create(campaignActivity); // Add dynamic list to campaign activity. AddItemCampaignActivityRequest addListToCampaignActivityRequest = new AddItemCampaignActivityRequest() { CampaignActivityId = _campaignActivityId, ItemId = _dynamicListId, EntityName = List.EntityLogicalName }; _serviceProxy.Execute(addListToCampaignActivityRequest); Console.WriteLine("Added dynamic list to the campaign activity."); #endregion #region 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); _staticListId = copyResponse.StaticListId; Console.WriteLine("Copied dynamic list to a static list."); // Add the static list to the campaign. AddItemCampaignRequest addStaticListToCampaignRequest = new AddItemCampaignRequest() { CampaignId = _campaignId, EntityId = _staticListId, EntityName = List.EntityLogicalName }; _serviceProxy.Execute(addStaticListToCampaignRequest); Console.WriteLine("Added static list to the campaign."); // Add the static list to the campaign activity. AddItemCampaignActivityRequest addStaticListToCampaignActivityRequest = new AddItemCampaignActivityRequest() { CampaignActivityId = _campaignActivityId, ItemId = _staticListId, EntityName = List.EntityLogicalName }; _serviceProxy.Execute(addStaticListToCampaignActivityRequest); Console.WriteLine("Added static list to the campaign's activity."); #endregion #region Create fax for campaign's activity // Create a fax. Fax fax = new Fax() { Subject = "Example Fax" }; Console.WriteLine("Created fax for campaign's activity."); #endregion Create fax for campaign's activity #region Distribute fax to the marketing list // 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); Console.WriteLine("Distributed fax to the marketing lists."); #endregion Distribute fax to the marketing list #region Retrieve collection of entities from marketing list // Retrieve a collection of entities that correspond // to all of the members in a marketing list // This approach of retrieving list members allows you to dynamically // retrieve the members of a list programmatically without requiring // knowledge of the member entity type. OrganizationServiceContext orgContext = new OrganizationServiceContext(_serviceProxy); var member = (from mb in orgContext.CreateQuery <List>() where mb.Id == _dynamicListId select mb).FirstOrDefault(); string fetchQuery = member.Query; RetrieveMultipleRequest memberRequest = new RetrieveMultipleRequest(); FetchExpression fetch = new FetchExpression(fetchQuery); memberRequest.Query = fetch; RetrieveMultipleResponse memberResponse = (RetrieveMultipleResponse)_serviceProxy.Execute(memberRequest); Console.WriteLine("Retrieved collection of entities from a marketing list."); #endregion Retrieve collection of entities from marketing list DeleteRequiredRecords(promptforDelete); } } // Catch any service fault exceptions that Microsoft Dynamics CRM throws. catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { // You can handle an exception here or pass it back to the calling method. throw; } }
/// <summary> /// This method first connects to the Organization service. Afterwards a dynamic /// list is created and associated to the campaign and the campaign's activity. /// Then the dynamic list is copied to a static list and associated with the same /// campaign and campaign activity. Finally the sample distributes the campaign /// to both the dynamic and static lists. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptforDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete) { try { //<snippetMarketingAutomation1> // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,serverConfig.Credentials, serverConfig.DeviceCredentials)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); CreateRequiredRecords(); #region Create Dynamic List // Create FetchXml for marketing list's query which locates accounts // in Seattle. 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='seattle' /> </filter> </entity> </fetch>"; //<snippetAddItemCampaign> // 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 }; _dynamicListId = _serviceProxy.Create(dynamicList); dynamicList.Id = _dynamicListId; Console.WriteLine("Created dynamic list."); #endregion #region Associate dynamic list to campaign // Create a campaign. Campaign campaign = new Campaign() { Name = "Sample Campaign" }; _campaignId = _serviceProxy.Create(campaign); campaign.Id = _campaignId; // Add the dynamic list to the campaign. AddItemCampaignRequest addListToCampaignRequest = new AddItemCampaignRequest() { CampaignId = _campaignId, EntityId = _dynamicListId, EntityName = List.EntityLogicalName, }; _serviceProxy.Execute(addListToCampaignRequest); Console.WriteLine("Added dynamic list to the campaign."); //</snippetAddItemCampaign> //<snippetAddItemCampaignActivity> // 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() }; _campaignActivityId = _serviceProxy.Create(campaignActivity); // Add dynamic list to campaign activity. AddItemCampaignActivityRequest addListToCampaignActivityRequest = new AddItemCampaignActivityRequest() { CampaignActivityId = _campaignActivityId, ItemId = _dynamicListId, EntityName = List.EntityLogicalName }; _serviceProxy.Execute(addListToCampaignActivityRequest); Console.WriteLine("Added dynamic list to the campaign activity."); //</snippetAddItemCampaignActivity> #endregion #region Associate static list to campaign //<snippetCopyDynamicListToStatic> // Copy the dynamic list to a static list. CopyDynamicListToStaticRequest copyRequest = new CopyDynamicListToStaticRequest() { ListId = _dynamicListId }; CopyDynamicListToStaticResponse copyResponse = (CopyDynamicListToStaticResponse)_serviceProxy.Execute(copyRequest); _staticListId = copyResponse.StaticListId; Console.WriteLine("Copied dynamic list to a static list."); //</snippetCopyDynamicListToStatic> // Add the static list to the campaign. AddItemCampaignRequest addStaticListToCampaignRequest = new AddItemCampaignRequest() { CampaignId = _campaignId, EntityId = _staticListId, EntityName = List.EntityLogicalName }; _serviceProxy.Execute(addStaticListToCampaignRequest); Console.WriteLine("Added static list to the campaign."); // Add the static list to the campaign activity. AddItemCampaignActivityRequest addStaticListToCampaignActivityRequest = new AddItemCampaignActivityRequest() { CampaignActivityId = _campaignActivityId, ItemId = _staticListId, EntityName = List.EntityLogicalName }; _serviceProxy.Execute(addStaticListToCampaignActivityRequest); Console.WriteLine("Added static list to the campaign's activity."); #endregion #region Create fax for campaign's activity // Create a fax. Fax fax = new Fax() { Subject = "Example Fax" }; Console.WriteLine("Created fax for campaign's activity."); #endregion Create fax for campaign's activity #region Distribute fax to the marketing list //<snippetDistributeCampaignActivity> // 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); Console.WriteLine("Distributed fax to the marketing lists."); //</snippetDistributeCampaignActivity> #endregion Distribute fax to the marketing list #region Retrieve collection of entities from marketing list // Retrieve a collection of entities that correspond // to all of the members in a marketing list // This approach of retrieving list members allows you to dynamically // retrieve the members of a list programmatically without requiring // knowledge of the member entity type. OrganizationServiceContext orgContext = new OrganizationServiceContext(_serviceProxy); var member = (from mb in orgContext.CreateQuery<List>() where mb.Id == _dynamicListId select mb).FirstOrDefault(); string fetchQuery = member.Query; RetrieveMultipleRequest memberRequest = new RetrieveMultipleRequest(); FetchExpression fetch = new FetchExpression(fetchQuery); memberRequest.Query = fetch; RetrieveMultipleResponse memberResponse = (RetrieveMultipleResponse)_serviceProxy.Execute(memberRequest); Console.WriteLine("Retrieved collection of entities from a marketing list."); #endregion Retrieve collection of entities from marketing list DeleteRequiredRecords(promptforDelete); } //</snippetMarketingAutomation1> } // Catch any service fault exceptions that Microsoft Dynamics CRM throws. catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>) { // You can handle an exception here or pass it back to the calling method. throw; } }