/// <summary> /// Shows how to perform the following tasks with solutions: /// - Create a Publisher /// - Retrieve the Default Publisher /// - Create a Solution /// - Retrieve a Solution /// - Add an existing Solution Component /// - Remove a Solution Component /// - Export or Package a Solution /// - Install or Upgrade a solution /// - Delete a Solution /// </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(); CreateSiteMap(); // AutoNumberGeneration.AutoNumber.CreateWorkFlow(_serviceProxy, _languageCode); AutoSMS.SetProxy(_serviceProxy, _languageCode); // Call the method to create any data that this sample requires. CreateRequiredRecords(); } } // 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 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); }