Below is the C# code to create CRM 2011 solution and add entity component to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | /// <summary> /// Create solution and add entity component to it /// </summary> /// <param name="solutionName">The solution name to be created create.</param> /// <param name="entiyLogicalName">The logical name of an entity to be added to solution.</param> /// <param name="publisherName">The publisher name.</param> private void AddComponentToSolution(string solutionName, string entiyLogicalName, string publisherName) { Entity solution = CreateSolution(solutionName, publisherName); RetrieveEntityResponse retrieveEntityResponse = null; try { RetrieveEntityRequest retrieveEntityRequest = new RetrieveEntityRequest() { LogicalName = entiyLogicalName, }; retrieveEntityResponse = (RetrieveEntityResponse)service.Execute(retrieveEntityRequest); } catch (Exception ex) { throw new Exception("Error in retrieving entity: " + ex.Message); } try { AddSolutionComponentRequest retreieveSolutionRequest = new AddSolutionComponentRequest() { ComponentType = 1, ComponentId = (Guid)retrieveEntityResponse.EntityMetadata.MetadataId, SolutionUniqueName = solution.Attributes["uniquename"].ToString(), }; service.Execute(retreieveSolutionRequest); } catch (Exception ex) { throw new Exception("Error in adding component to solution: " + ex.Message); } } /// <summary> /// To create a solution /// </summary> /// <param name="solutionName">The solution name.</param> /// <param name="publisherName">The publisher name.</param> /// <returns>The solution entity.</returns> private Entity CreateSolution(string solutionName, string publisherName) { Entity publisher = GetPublisher(publisherName.ToLower()); Entity newSolution = new Entity("solution"); Entity existingSolution = null; int check = 0; if (publisher == null) return null; try { EntityReference defultPublisherReference = new EntityReference() { Id = publisher.Id, LogicalName = publisher.LogicalName, Name = publisher.Attributes["friendlyname"].ToString(), }; QueryExpression solutionQuery = new QueryExpression { EntityName = "solution", ColumnSet = new ColumnSet("publisherid", "version", "versionnumber", "friendlyname", "uniquename"), Criteria = new FilterExpression() }; newSolution["uniquename"] = solutionName.ToLower(); solutionQuery.Criteria.AddCondition("uniquename", ConditionOperator.Equal, newSolution["uniquename"]); EntityCollection querySampleSolutionResults = service.RetrieveMultiple(solutionQuery); // Delete if already exist if (querySampleSolutionResults.Entities.Count > 0) { existingSolution = (Entity)querySampleSolutionResults.Entities[0]; service.Delete(existingSolution.LogicalName, (Guid)existingSolution.Id); check = 1; } if (existingSolution == null || check == 1) { newSolution["uniquename"] = solutionName.ToLower(); newSolution["friendlyname"] = solutionName; newSolution["publisherid"] = defultPublisherReference; newSolution["description"] = "This is the solution created by plug-in."; newSolution["version"] = "1.0"; } } catch (Exception ex) { throw new InvalidPluginExecutionException("Error creating solution: " + ex.Message); } return newSolution; } /// <summary> /// To retrieve publisher /// </summary> /// <param name="publisherName">The publisher name.</param> /// <returns>The publisher entity.</returns> private Entity GetPublisher(string publisherName) { QueryExpression publisherQuery = null; EntityCollection publishersCollection = null; try { ConditionExpression condExp = new ConditionExpression("uniquename", ConditionOperator.Equal, publisherName); FilterExpression filterExp = new FilterExpression(); filterExp.Conditions.Add(condExp); publisherQuery = new QueryExpression(); publisherQuery.EntityName = "publisher"; publisherQuery.ColumnSet = new ColumnSet("publisherid", "customizationprefix", "friendlyname"); publisherQuery.Criteria = filterExp; publishersCollection = service.RetrieveMultiple(publisherQuery); } catch (Exception ex) { throw new Exception("Error getting publisher: :" + ex.Message); } if (publishersCollection.Entities.Count > 0) { return publishersCollection.Entities[0]; } else { return null; } } |
No comments:
Post a Comment