/// <summary> /// Global OptionSet 삭제 /// </summary> /// <param name="schemaName"></param> public void DeleteGlobalOptionSet(string schemaName) { try { DeleteOptionSetRequest deleteOptionSetRequest = new DeleteOptionSetRequest { Name = schemaName }; _orgService.Execute(deleteOptionSetRequest); } catch (Exception) { throw; } }
/// <summary> /// Deletes any entity records that were created for this sample. /// <param name="prompt">Indicates whether to prompt the user to delete the records created in this sample.</param> /// </summary> public void DeleteRequiredRecords(bool prompt) { bool deleteRecords = true; if (prompt) { Console.WriteLine("\nDo you want these entity records deleted? (y/n)"); String answer = Console.ReadLine(); deleteRecords = (answer.StartsWith("y") || answer.StartsWith("Y")); } if (deleteRecords) { DeleteOptionSetRequest deleteRequest = new DeleteOptionSetRequest { Name = _optionSetName }; _serviceProxy.Execute(deleteRequest); Console.WriteLine("Entity records have been deleted."); } }
/// <summary> /// Deletes any entity records that were created for this sample. /// <param name="prompt">Indicates whether to prompt the user to delete the records created in this sample.</param> /// </summary> public void DeleteRequiredRecords(bool prompt) { bool deleteRecords = true; if (prompt) { Console.WriteLine("\nDo you want these entity records deleted? (y/n)"); String answer = Console.ReadLine(); deleteRecords = (answer.StartsWith("y") || answer.StartsWith("Y")); } if (deleteRecords) { DeleteOptionSetRequest deleteRequest = new DeleteOptionSetRequest { Name = _optionSetName }; _serviceProxy.Execute(deleteRequest); Console.WriteLine("Entity records have been deleted."); } }
/// <summary> /// This method creates any entity records that this sample requires. /// Create a publisher /// Create a new solution, "Primary" /// Create a Global Option Set in solution "Primary" /// Export the "Primary" solution, setting it to Protected /// Delete the option set and solution /// Import the "Primary" solution, creating a managed solution in CRM. /// Create a new solution, "Secondary" /// Create an attribute in "Secondary" that references the Global Option Set /// </summary> public void CreateRequiredRecords() { //Create the publisher that will "own" the two solutions //<snippetGetSolutionDependencies6> Publisher publisher = new Publisher { UniqueName = "examplepublisher", FriendlyName = "An Example Publisher", Description = "This is an example publisher", CustomizationPrefix = _prefix }; _publisherId = _serviceProxy.Create(publisher); //</snippetGetSolutionDependencies6> //Create the primary solution - note that we are not creating it //as a managed solution as that can only be done when exporting the solution. //<snippetGetSolutionDependencies2> Solution primarySolution = new Solution { Version = "1.0", FriendlyName = "Primary Solution", PublisherId = new EntityReference(Publisher.EntityLogicalName, _publisherId), UniqueName = _primarySolutionName }; _primarySolutionId = _serviceProxy.Create(primarySolution); //</snippetGetSolutionDependencies2> //Now, create the Global Option Set and associate it to the solution. //<snippetGetSolutionDependencies3> OptionSetMetadata optionSetMetadata = new OptionSetMetadata() { Name = _globalOptionSetName, DisplayName = new Label("Example Option Set", _languageCode), IsGlobal = true, OptionSetType = OptionSetType.Picklist, Options = { new OptionMetadata(new Label("Option 1", _languageCode), 1), new OptionMetadata(new Label("Option 2", _languageCode), 2) } }; CreateOptionSetRequest createOptionSetRequest = new CreateOptionSetRequest { OptionSet = optionSetMetadata }; createOptionSetRequest.SolutionUniqueName = _primarySolutionName; _serviceProxy.Execute(createOptionSetRequest); //</snippetGetSolutionDependencies3> //Export the solution as managed so that we can later import it. //<snippetGetSolutionDependencies4> ExportSolutionRequest exportRequest = new ExportSolutionRequest { Managed = true, SolutionName = _primarySolutionName }; ExportSolutionResponse exportResponse = (ExportSolutionResponse)_serviceProxy.Execute(exportRequest); //</snippetGetSolutionDependencies4> // Delete the option set previous created, so it can be imported under the // managed solution. //<snippetGetSolutionDependencies5> DeleteOptionSetRequest deleteOptionSetRequest = new DeleteOptionSetRequest { Name = _globalOptionSetName }; _serviceProxy.Execute(deleteOptionSetRequest); //</snippetGetSolutionDependencies5> // Delete the previous primary solution, so it can be imported as managed. _serviceProxy.Delete(Solution.EntityLogicalName, _primarySolutionId); _primarySolutionId = Guid.Empty; // Re-import the solution as managed. ImportSolutionRequest importRequest = new ImportSolutionRequest { CustomizationFile = exportResponse.ExportSolutionFile }; _serviceProxy.Execute(importRequest); // Retrieve the solution from CRM in order to get the new id. QueryByAttribute primarySolutionQuery = new QueryByAttribute { EntityName = Solution.EntityLogicalName, ColumnSet = new ColumnSet("solutionid"), Attributes = { "uniquename" }, Values = { _primarySolutionName } }; _primarySolutionId = _serviceProxy.RetrieveMultiple(primarySolutionQuery).Entities .Cast<Solution>().FirstOrDefault().SolutionId.GetValueOrDefault(); // Create a secondary solution. Solution secondarySolution = new Solution { Version = "1.0", FriendlyName = "Secondary Solution", PublisherId = new EntityReference(Publisher.EntityLogicalName, _publisherId), UniqueName = "SecondarySolution" }; _secondarySolutionId = _serviceProxy.Create(secondarySolution); // Create a Picklist attribute in the secondary solution linked to the option set in the // primary - see WorkWithOptionSets.cs for more on option sets. PicklistAttributeMetadata picklistMetadata = new PicklistAttributeMetadata { SchemaName = _picklistName, LogicalName = _picklistName, DisplayName = new Label("Example Picklist", _languageCode), RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None), OptionSet = new OptionSetMetadata { IsGlobal = true, Name = _globalOptionSetName } }; CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest { EntityName = Contact.EntityLogicalName, Attribute = picklistMetadata }; createAttributeRequest["SolutionUniqueName"] = secondarySolution.UniqueName; _serviceProxy.Execute(createAttributeRequest); }
public void DeleteSharedOptionSet(string schemaName) { var request = new DeleteOptionSetRequest { Name = schemaName }; Execute(request); if (SharedOptionSets.Any(o => o.Name == schemaName)) SharedOptionSets.Remove(SharedOptionSets.Single(o => o.Name == schemaName)); }
/// <summary> /// This method creates any entity records that this sample requires. /// </summary> public void CreateRequiredRecords() { // Create a managed solution for the Install or upgrade a solution sample Guid _tempPublisherId = new Guid(); System.String _tempCustomizationPrefix = "ds"; Guid _tempSolutionsSampleSolutionId = new Guid(); Random rn = new Random(); System.String _TempGlobalOptionSetName = "_TempSampleGlobalOptionSetName" + rn.Next(); Boolean _publisherCreated = false; Boolean _solutionCreated = false; //Define a new publisher Publisher _crmSdkPublisher = new Publisher { UniqueName = Constants.PublisherUniqueName, FriendlyName = Constants.PublisherFriendlyName, SupportingWebsiteUrl = Constants.PublisherSupportingWebsiteUrl, CustomizationPrefix = Constants.PublisherCustomizationPrefix, EMailAddress = Constants.PublisherEmailAddress, Description = Constants.PublisherDescription }; //Does publisher already exist? QueryExpression querySDKSamplePublisher = new QueryExpression { EntityName = Publisher.EntityLogicalName, ColumnSet = new ColumnSet("publisherid", "customizationprefix"), Criteria = new FilterExpression() }; querySDKSamplePublisher.Criteria.AddCondition("uniquename", ConditionOperator.Equal, _crmSdkPublisher.UniqueName); EntityCollection querySDKSamplePublisherResults = _serviceProxy.RetrieveMultiple(querySDKSamplePublisher); Publisher SDKSamplePublisherResults = null; //If it already exists, use it if (querySDKSamplePublisherResults.Entities.Count > 0) { SDKSamplePublisherResults = (Publisher)querySDKSamplePublisherResults.Entities[0]; _tempPublisherId = (Guid)SDKSamplePublisherResults.PublisherId; _tempCustomizationPrefix = SDKSamplePublisherResults.CustomizationPrefix; } //If it doesn't exist, create it if (SDKSamplePublisherResults == null) { _tempPublisherId = _serviceProxy.Create(_crmSdkPublisher); _tempCustomizationPrefix = _crmSdkPublisher.CustomizationPrefix; _publisherCreated = true; } //Upload only configuration page UploadConfigurationPageForSolution(); //SetWebResourceConfigurationForSolution(); //Define a solution Solution solution = new Solution { UniqueName = Constants.SolutionUniqueName, FriendlyName = Constants.SolutionFriendlyName, PublisherId = new EntityReference(Publisher.EntityLogicalName, _tempPublisherId), Description = Constants.SolutionDescription, Version = Constants.SolutionVersion, ConfigurationPageId = new EntityReference(WebResource.EntityLogicalName, _webResourceIdForSolution[0]) }; //Check whether it already exists QueryExpression querySampleSolution = new QueryExpression { EntityName = Solution.EntityLogicalName, ColumnSet = new ColumnSet(), Criteria = new FilterExpression() }; querySampleSolution.Criteria.AddCondition("uniquename", ConditionOperator.Equal, solution.UniqueName); EntityCollection querySampleSolutionResults = _serviceProxy.RetrieveMultiple(querySampleSolution); Solution SampleSolutionResults = null; if (querySampleSolutionResults.Entities.Count > 0) { SampleSolutionResults = (Solution)querySampleSolutionResults.Entities[0]; _tempSolutionsSampleSolutionId = (Guid)SampleSolutionResults.SolutionId; } if (SampleSolutionResults == null) { _tempSolutionsSampleSolutionId = _serviceProxy.Create(solution); _solutionCreated = true; } // Add a solution Component OptionSetMetadata optionSetMetadata = new OptionSetMetadata() { Name = _tempCustomizationPrefix + _TempGlobalOptionSetName, DisplayName = new Label("Example Option Set", _languageCode), IsGlobal = true, OptionSetType = OptionSetType.Picklist, Options = { new OptionMetadata(new Label("Option A", _languageCode), null), new OptionMetadata(new Label("Option B", _languageCode), null) } }; CreateOptionSetRequest createOptionSetRequest = new CreateOptionSetRequest { OptionSet = optionSetMetadata, SolutionUniqueName = solution.UniqueName }; _serviceProxy.Execute(createOptionSetRequest); //delete configuration entity if (IsEntityExist(_customEntityName) > 0) { DeleteEntityRequest customEntityNameFormField = new DeleteEntityRequest() { LogicalName = _customEntityName, }; _serviceProxy.Execute(customEntityNameFormField); } // Create the dots_autonumber entity. AutoSMS.DotsAutoNumberEntity(); // CreateTab(); //delete dots_configuration entity if (IsEntityExist(_customConfigurationEntityName) > 0) { DeleteEntityRequest customEntityNameFormField = new DeleteEntityRequest() { LogicalName = _customConfigurationEntityName, }; _serviceProxy.Execute(customEntityNameFormField); } //for create dots_configuration entity AutoSMS.DotsAutoNumberConfigurationEntity(); // assign dots_autonumber form entity to solution RetrieveEntityRequest retrievepowertEntityRequest = new RetrieveEntityRequest { EntityFilters = EntityFilters.Entity, LogicalName = _customEntityName }; RetrieveEntityResponse retrievepowerEntityResponse = (RetrieveEntityResponse)_serviceProxy.Execute(retrievepowertEntityRequest); AddSolutionComponentRequest addReq = new AddSolutionComponentRequest() { ComponentType = 1, ComponentId = (Guid)retrievepowerEntityResponse.EntityMetadata.MetadataId, SolutionUniqueName = solution.UniqueName, AddRequiredComponents = true }; _serviceProxy.Execute(addReq); //assign dots_configuration entity to solution RetrieveEntityRequest retrieveconfigurationtEntityRequest = new RetrieveEntityRequest { EntityFilters = EntityFilters.Entity, LogicalName = _customConfigurationEntityName }; RetrieveEntityResponse retrieveconfigEntityResponse = (RetrieveEntityResponse)_serviceProxy.Execute(retrieveconfigurationtEntityRequest); AddSolutionComponentRequest addConfigReq = new AddSolutionComponentRequest() { ComponentType = 1, ComponentId = (Guid)retrieveconfigEntityResponse.EntityMetadata.MetadataId, SolutionUniqueName = solution.UniqueName, AddRequiredComponents = true }; _serviceProxy.Execute(addConfigReq); //assign web resource to slution CreateWebResource(solution.UniqueName); //assign configuration page above created to solution AssiginConfigurationPageToSolution(_webResourceIdForSolution[0], solution.UniqueName); ExportSolutionRequest exportSolutionRequest = new ExportSolutionRequest(); exportSolutionRequest.Managed = false; exportSolutionRequest.SolutionName = solution.UniqueName; ExportSolutionResponse exportSolutionResponse = (ExportSolutionResponse)_serviceProxy.Execute(exportSolutionRequest); byte[] exportXml = exportSolutionResponse.ExportSolutionFile; System.IO.Directory.CreateDirectory(_outputDir); File.WriteAllBytes(_managedSolutionLocation, exportXml); // Delete the solution and the components so it can be installed. DeleteOptionSetRequest delOptSetReq = new DeleteOptionSetRequest { Name = (_tempCustomizationPrefix + _TempGlobalOptionSetName).ToLower() }; _serviceProxy.Execute(delOptSetReq); DeleteEntityRequest delEntReq = new DeleteEntityRequest { LogicalName = (_customEntityName) }; _serviceProxy.Execute(delEntReq); DeleteEntityRequest delEntReqConfig = new DeleteEntityRequest { LogicalName = (_customConfigurationEntityName) }; _serviceProxy.Execute(delEntReqConfig); if (_solutionCreated) { _serviceProxy.Delete(Solution.EntityLogicalName, _tempSolutionsSampleSolutionId); //delete webresorce foreach (var _id in _webResourceIds) { _serviceProxy.Delete(WebResource.EntityLogicalName, _id); } //for configuration page above created delete _serviceProxy.Delete(WebResource.EntityLogicalName, _webResourceIdForSolution[0]); } if (_publisherCreated) { _serviceProxy.Delete(Publisher.EntityLogicalName, _tempPublisherId); } Console.WriteLine("Managed Solution created and copied to {0}", _managedSolutionLocation); }
/// <summary> /// Deletes any entity records that were created for this sample. /// <param name="prompt">Indicates whether to prompt the user to /// delete the records created in this sample.</param> /// </summary> public void DeleteRequiredRecords(bool prompt) { bool deleteRecords = true; if (prompt) { Console.WriteLine("\nDo you want these entity records deleted? (y/n)"); String answer = Console.ReadLine(); deleteRecords = (answer.StartsWith("y") || answer.StartsWith("Y")); } if (deleteRecords) { #region How to delete a option from a option set // Use the DeleteOptionValueRequest message // to remove the newly inserted label. DeleteOptionValueRequest deleteOptionValueRequest = new DeleteOptionValueRequest { OptionSetName = _globalOptionSetName, Value = _insertedOptionValue }; // Execute the request. _serviceProxy.Execute(deleteOptionValueRequest); Console.WriteLine("Option Set option removed."); #endregion How to delete a option from a option set #region How to delete attribute // Create the request to see which components have a dependency on the // global option set. RetrieveDependentComponentsRequest dependencyRequest = new RetrieveDependentComponentsRequest { ObjectId = _optionSetId, ComponentType = (int)componenttype.OptionSet }; RetrieveDependentComponentsResponse dependencyResponse = (RetrieveDependentComponentsResponse)_serviceProxy.Execute( dependencyRequest); // Here you would check the dependencyResponse.EntityCollection property // and act as appropriate. However, we know there is exactly one // dependency so this example deals with it directly and deletes // the previously created attribute. DeleteAttributeRequest deleteAttributeRequest = new DeleteAttributeRequest { EntityLogicalName = Contact.EntityLogicalName, LogicalName = "sample_examplepicklist" }; _serviceProxy.Execute(deleteAttributeRequest); Console.WriteLine("Referring attribute deleted."); #endregion How to delete attribute #region How to delete global option set // Finally, delete the global option set. Attempting this before deleting // the picklist above will result in an exception being thrown. DeleteOptionSetRequest deleteRequest = new DeleteOptionSetRequest { Name = _globalOptionSetName }; _serviceProxy.Execute(deleteRequest); Console.WriteLine("Global Option Set deleted"); #endregion How to delete global option set } }
/// <summary> /// This method creates any entity records that this sample requires. /// </summary> public static void CreateRequiredRecords(CrmServiceClient service) { // Create a managed solution for the Install or upgrade a solution sample Guid _tempPublisherId = new Guid(); System.String _tempCustomizationPrefix = "new"; Guid _tempSolutionsSampleSolutionId = new Guid(); System.String _TempGlobalOptionSetName = "_TempSampleGlobalOptionSetName"; Boolean _publisherCreated = false; Boolean _solutionCreated = false; //Define a new publisher Publisher _powerappsSdkPublisher = new Publisher { UniqueName = "sdksamples", FriendlyName = "PowerApps SDK Samples", SupportingWebsiteUrl = "http://msdn.microsoft.com/en-us/dynamics/crm/default.aspx", CustomizationPrefix = "sample", EMailAddress = "*****@*****.**", Description = "This publisher was created with samples from the Microsoft Dynamics CRM SDK" }; //Does publisher already exist? QueryExpression querySDKSamplePublisher = new QueryExpression { EntityName = Publisher.EntityLogicalName, ColumnSet = new ColumnSet("publisherid", "customizationprefix"), Criteria = new FilterExpression() }; querySDKSamplePublisher.Criteria.AddCondition("uniquename", ConditionOperator.Equal, _powerappsSdkPublisher.UniqueName); EntityCollection querySDKSamplePublisherResults = service.RetrieveMultiple(querySDKSamplePublisher); Publisher SDKSamplePublisherResults = null; //If it already exists, use it if (querySDKSamplePublisherResults.Entities.Count > 0) { SDKSamplePublisherResults = (Publisher)querySDKSamplePublisherResults.Entities[0]; _tempPublisherId = (Guid)SDKSamplePublisherResults.PublisherId; _tempCustomizationPrefix = SDKSamplePublisherResults.CustomizationPrefix; } //If it doesn't exist, create it if (SDKSamplePublisherResults == null) { _tempPublisherId = service.Create(_powerappsSdkPublisher); _tempCustomizationPrefix = _powerappsSdkPublisher.CustomizationPrefix; _publisherCreated = true; } //Create a Solution //Define a solution Solution solution = new Solution { UniqueName = "samplesolutionforImport", FriendlyName = "Sample Solution for Import", PublisherId = new EntityReference(Publisher.EntityLogicalName, _tempPublisherId), Description = "This solution was created by the WorkWithSolutions sample code in the PowerApps SDK samples.", Version = "1.0" }; //Check whether it already exists QueryExpression querySampleSolution = new QueryExpression { EntityName = Solution.EntityLogicalName, ColumnSet = new ColumnSet(), Criteria = new FilterExpression() }; querySampleSolution.Criteria.AddCondition("uniquename", ConditionOperator.Equal, solution.UniqueName); EntityCollection querySampleSolutionResults = service.RetrieveMultiple(querySampleSolution); Solution SampleSolutionResults = null; if (querySampleSolutionResults.Entities.Count > 0) { SampleSolutionResults = (Solution)querySampleSolutionResults.Entities[0]; _tempSolutionsSampleSolutionId = (Guid)SampleSolutionResults.SolutionId; } if (SampleSolutionResults == null) { _tempSolutionsSampleSolutionId = service.Create(solution); _solutionCreated = true; } // Add a solution Component OptionSetMetadata optionSetMetadata = new OptionSetMetadata() { Name = _tempCustomizationPrefix + _TempGlobalOptionSetName, DisplayName = new Label("Example Option Set", _languageCode), IsGlobal = true, OptionSetType = OptionSetType.Picklist, Options = { new OptionMetadata(new Label("Option A", _languageCode), null), new OptionMetadata(new Label("Option B", _languageCode), null) } }; CreateOptionSetRequest createOptionSetRequest = new CreateOptionSetRequest { OptionSet = optionSetMetadata, SolutionUniqueName = solution.UniqueName }; service.Execute(createOptionSetRequest); //Export an a solution ExportSolutionRequest exportSolutionRequest = new ExportSolutionRequest(); exportSolutionRequest.Managed = true; exportSolutionRequest.SolutionName = solution.UniqueName; ExportSolutionResponse exportSolutionResponse = (ExportSolutionResponse)service.Execute(exportSolutionRequest); byte[] exportXml = exportSolutionResponse.ExportSolutionFile; System.IO.Directory.CreateDirectory(outputDir); File.WriteAllBytes(ManagedSolutionLocation, exportXml); // Delete the solution and the components so it can be installed. DeleteOptionSetRequest delOptSetReq = new DeleteOptionSetRequest { Name = (_tempCustomizationPrefix + _TempGlobalOptionSetName).ToLower() }; service.Execute(delOptSetReq); if (_solutionCreated) { service.Delete(Solution.EntityLogicalName, _tempSolutionsSampleSolutionId); } if (_publisherCreated) { service.Delete(Publisher.EntityLogicalName, _tempPublisherId); } Console.WriteLine("Managed Solution created and copied to {0}", ManagedSolutionLocation); }
/// <summary> /// This method creates any entity records that this sample requires. /// Create a publisher /// Create a new solution, "Primary" /// Create a Global Option Set in solution "Primary" /// Export the "Primary" solution, setting it to Protected /// Delete the option set and solution /// Import the "Primary" solution, creating a managed solution in CRM. /// Create a new solution, "Secondary" /// Create an attribute in "Secondary" that references the Global Option Set /// </summary> public void CreateRequiredRecords() { //Create the publisher that will "own" the two solutions //<snippetGetSolutionDependencies6> Publisher publisher = new Publisher { UniqueName = "examplepublisher", FriendlyName = "An Example Publisher", Description = "This is an example publisher", CustomizationPrefix = _prefix }; _publisherId = _serviceProxy.Create(publisher); //</snippetGetSolutionDependencies6> //Create the primary solution - note that we are not creating it //as a managed solution as that can only be done when exporting the solution. //<snippetGetSolutionDependencies2> Solution primarySolution = new Solution { Version = "1.0", FriendlyName = "Primary Solution", PublisherId = new EntityReference(Publisher.EntityLogicalName, _publisherId), UniqueName = _primarySolutionName }; _primarySolutionId = _serviceProxy.Create(primarySolution); //</snippetGetSolutionDependencies2> //Now, create the Global Option Set and associate it to the solution. //<snippetGetSolutionDependencies3> OptionSetMetadata optionSetMetadata = new OptionSetMetadata() { Name = _globalOptionSetName, DisplayName = new Label("Example Option Set", _languageCode), IsGlobal = true, OptionSetType = OptionSetType.Picklist, Options = { new OptionMetadata(new Label("Option 1", _languageCode), 1), new OptionMetadata(new Label("Option 2", _languageCode), 2) } }; CreateOptionSetRequest createOptionSetRequest = new CreateOptionSetRequest { OptionSet = optionSetMetadata }; createOptionSetRequest.SolutionUniqueName = _primarySolutionName; _serviceProxy.Execute(createOptionSetRequest); //</snippetGetSolutionDependencies3> //Export the solution as managed so that we can later import it. //<snippetGetSolutionDependencies4> ExportSolutionRequest exportRequest = new ExportSolutionRequest { Managed = true, SolutionName = _primarySolutionName }; ExportSolutionResponse exportResponse = (ExportSolutionResponse)_serviceProxy.Execute(exportRequest); //</snippetGetSolutionDependencies4> // Delete the option set previous created, so it can be imported under the // managed solution. //<snippetGetSolutionDependencies5> DeleteOptionSetRequest deleteOptionSetRequest = new DeleteOptionSetRequest { Name = _globalOptionSetName }; _serviceProxy.Execute(deleteOptionSetRequest); //</snippetGetSolutionDependencies5> // Delete the previous primary solution, so it can be imported as managed. _serviceProxy.Delete(Solution.EntityLogicalName, _primarySolutionId); _primarySolutionId = Guid.Empty; // Re-import the solution as managed. ImportSolutionRequest importRequest = new ImportSolutionRequest { CustomizationFile = exportResponse.ExportSolutionFile }; _serviceProxy.Execute(importRequest); // Retrieve the solution from CRM in order to get the new id. QueryByAttribute primarySolutionQuery = new QueryByAttribute { EntityName = Solution.EntityLogicalName, ColumnSet = new ColumnSet("solutionid"), Attributes = { "uniquename" }, Values = { _primarySolutionName } }; _primarySolutionId = _serviceProxy.RetrieveMultiple(primarySolutionQuery).Entities .Cast<Solution>().FirstOrDefault().SolutionId.GetValueOrDefault(); // Create a secondary solution. Solution secondarySolution = new Solution { Version = "1.0", FriendlyName = "Secondary Solution", PublisherId = new EntityReference(Publisher.EntityLogicalName, _publisherId), UniqueName = "SecondarySolution" }; _secondarySolutionId = _serviceProxy.Create(secondarySolution); // Create a Picklist attribute in the secondary solution linked to the option set in the // primary - see WorkWithOptionSets.cs for more on option sets. PicklistAttributeMetadata picklistMetadata = new PicklistAttributeMetadata { SchemaName = _picklistName, LogicalName = _picklistName, DisplayName = new Label("Example Picklist", _languageCode), RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None), OptionSet = new OptionSetMetadata { IsGlobal = true, Name = _globalOptionSetName } }; CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest { EntityName = Contact.EntityLogicalName, Attribute = picklistMetadata }; createAttributeRequest["SolutionUniqueName"] = secondarySolution.UniqueName; _serviceProxy.Execute(createAttributeRequest); }
/// <summary> /// Deletes any entity records that were created for this sample. /// <param name="prompt">Indicates whether to prompt the user to /// delete the records created in this sample.</param> /// </summary> public void DeleteRequiredRecords(bool prompt) { bool deleteRecords = true; if (prompt) { Console.WriteLine("\nDo you want these entity records deleted? (y/n)"); String answer = Console.ReadLine(); deleteRecords = (answer.StartsWith("y") || answer.StartsWith("Y")); } if (deleteRecords) { #region How to delete a option from a option set //<snippetWorkwithGlobalOptionSets11> // Use the DeleteOptionValueRequest message // to remove the newly inserted label. DeleteOptionValueRequest deleteOptionValueRequest = new DeleteOptionValueRequest { OptionSetName = _globalOptionSetName, Value = _insertedOptionValue }; // Execute the request. _serviceProxy.Execute(deleteOptionValueRequest); //</snippetWorkwithGlobalOptionSets11> Console.WriteLine("Option Set option removed."); #endregion How to delete a option from a option set #region How to delete attribute //<snippetWorkwithGlobalOptionSets12> // Create the request to see which components have a dependency on the // global option set. RetrieveDependentComponentsRequest dependencyRequest = new RetrieveDependentComponentsRequest { ObjectId = _optionSetId, ComponentType = (int)componenttype.OptionSet }; RetrieveDependentComponentsResponse dependencyResponse = (RetrieveDependentComponentsResponse)_serviceProxy.Execute( dependencyRequest); // Here you would check the dependencyResponse.EntityCollection property // and act as appropriate. However, we know there is exactly one // dependency so this example deals with it directly and deletes // the previously created attribute. DeleteAttributeRequest deleteAttributeRequest = new DeleteAttributeRequest { EntityLogicalName = Contact.EntityLogicalName, LogicalName = "sample_examplepicklist" }; _serviceProxy.Execute(deleteAttributeRequest); Console.WriteLine("Referring attribute deleted."); #endregion How to delete attribute #region How to delete global option set // Finally, delete the global option set. Attempting this before deleting // the picklist above will result in an exception being thrown. //<snippetWorkwithGlobalOptionSets.DeleteOptionSetRequest> DeleteOptionSetRequest deleteRequest = new DeleteOptionSetRequest { Name = _globalOptionSetName }; _serviceProxy.Execute(deleteRequest); //</snippetWorkwithGlobalOptionSets.DeleteOptionSetRequest> //</snippetWorkwithGlobalOptionSets12> Console.WriteLine("Global Option Set deleted"); #endregion How to delete global option set } }
/// <summary> /// This method creates any entity records that this sample requires. /// </summary> public void CreateRequiredRecords() { // Create a managed solution for the Install or upgrade a solution sample Guid _tempPublisherId = new Guid(); System.String _tempCustomizationPrefix = "new"; Guid _tempSolutionsSampleSolutionId = new Guid(); System.String _TempGlobalOptionSetName = "_TempSampleGlobalOptionSetName"; Boolean _publisherCreated = false; Boolean _solutionCreated = false; //Define a new publisher Publisher _crmSdkPublisher = new Publisher { UniqueName = "sdksamples", FriendlyName = "Microsoft CRM SDK Samples", SupportingWebsiteUrl = "http://msdn.microsoft.com/en-us/dynamics/crm/default.aspx", CustomizationPrefix = "sample", EMailAddress = "*****@*****.**", Description = "This publisher was created with samples from the Microsoft Dynamics CRM SDK" }; //Does publisher already exist? QueryExpression querySDKSamplePublisher = new QueryExpression { EntityName = Publisher.EntityLogicalName, ColumnSet = new ColumnSet("publisherid", "customizationprefix"), Criteria = new FilterExpression() }; querySDKSamplePublisher.Criteria.AddCondition("uniquename", ConditionOperator.Equal, _crmSdkPublisher.UniqueName); EntityCollection querySDKSamplePublisherResults = _serviceProxy.RetrieveMultiple(querySDKSamplePublisher); Publisher SDKSamplePublisherResults = null; //If it already exists, use it if (querySDKSamplePublisherResults.Entities.Count > 0) { SDKSamplePublisherResults = (Publisher)querySDKSamplePublisherResults.Entities[0]; _tempPublisherId = (Guid)SDKSamplePublisherResults.PublisherId; _tempCustomizationPrefix = SDKSamplePublisherResults.CustomizationPrefix; } //If it doesn't exist, create it if (SDKSamplePublisherResults == null) { _tempPublisherId = _serviceProxy.Create(_crmSdkPublisher); _tempCustomizationPrefix = _crmSdkPublisher.CustomizationPrefix; _publisherCreated = true; } //Create a Solution //Define a solution Solution solution = new Solution { UniqueName = "samplesolutionforImport", FriendlyName = "Sample Solution for Import", PublisherId = new EntityReference(Publisher.EntityLogicalName, _tempPublisherId), Description = "This solution was created by the WorkWithSolutions sample code in the Microsoft Dynamics CRM SDK samples.", Version = "1.0" }; //Check whether it already exists QueryExpression querySampleSolution = new QueryExpression { EntityName = Solution.EntityLogicalName, ColumnSet = new ColumnSet(), Criteria = new FilterExpression() }; querySampleSolution.Criteria.AddCondition("uniquename", ConditionOperator.Equal, solution.UniqueName); EntityCollection querySampleSolutionResults = _serviceProxy.RetrieveMultiple(querySampleSolution); Solution SampleSolutionResults = null; if (querySampleSolutionResults.Entities.Count > 0) { SampleSolutionResults = (Solution)querySampleSolutionResults.Entities[0]; _tempSolutionsSampleSolutionId = (Guid)SampleSolutionResults.SolutionId; } if (SampleSolutionResults == null) { _tempSolutionsSampleSolutionId = _serviceProxy.Create(solution); _solutionCreated = true; } // Add a solution Component OptionSetMetadata optionSetMetadata = new OptionSetMetadata() { Name = _tempCustomizationPrefix + _TempGlobalOptionSetName, DisplayName = new Label("Example Option Set", _languageCode), IsGlobal = true, OptionSetType = OptionSetType.Picklist, Options = { new OptionMetadata(new Label("Option A", _languageCode), null), new OptionMetadata(new Label("Option B", _languageCode), null ) } }; CreateOptionSetRequest createOptionSetRequest = new CreateOptionSetRequest { OptionSet = optionSetMetadata, SolutionUniqueName = solution.UniqueName }; _serviceProxy.Execute(createOptionSetRequest); //Export an a solution ExportSolutionRequest exportSolutionRequest = new ExportSolutionRequest(); exportSolutionRequest.Managed = true; exportSolutionRequest.SolutionName = solution.UniqueName; ExportSolutionResponse exportSolutionResponse = (ExportSolutionResponse)_serviceProxy.Execute(exportSolutionRequest); byte[] exportXml = exportSolutionResponse.ExportSolutionFile; System.IO.Directory.CreateDirectory(outputDir); File.WriteAllBytes(ManagedSolutionLocation, exportXml); // Delete the solution and the components so it can be installed. DeleteOptionSetRequest delOptSetReq = new DeleteOptionSetRequest { Name = (_tempCustomizationPrefix + _TempGlobalOptionSetName).ToLower() }; _serviceProxy.Execute(delOptSetReq); if (_solutionCreated) { _serviceProxy.Delete(Solution.EntityLogicalName, _tempSolutionsSampleSolutionId); } if (_publisherCreated) { _serviceProxy.Delete(Publisher.EntityLogicalName, _tempPublisherId); } Console.WriteLine("Managed Solution created and copied to {0}", ManagedSolutionLocation); }