Create MS CRM Form using C#
Yes, We can create MS CRM Form using C# Code.
All you need is few lines of Code and one XML file which acts as a Form-XML.
Steps :
1> Get Form XML or create one using below form XML.
2> Create C# Console app
3> Use below Code to create form in CRM Entities.
Form XML is what CRM understands while creating form
Here is the Form XML, which has only one tab and section and i create custom web resource. you can change as per your requirement.
<form showImage="true" shownavigationbar="false" maxWidth="1920"> <ancestor id="{8448b78f-8f42-454e-8e2a-f8196b0419af}" /> <tabs> <tab name="Message" IsUserDefined="0" locklevel="0" showlabel="true" expanded="false"> <labels> <label description="Message" languagecode="1033" /> </labels> <columns> <column width="100%"> <sections> <section name="tab_4_section_1" showlabel="false" showbar="false" locklevel="0" IsUserDefined="0" layout="varwidth" columns="1" labelwidth="115"> <labels> <label description="Section" languagecode="1033" /> </labels> <rows> <row> <cell showlabel="false" colspan="1" auto="false" rowspan="6"> <labels> <label description="Message" languagecode="1033" /> </labels> <control id="WebResource_Message" classid="{9FDF5F91-88B1-47f4-AD53-C11EFC01A01D}"> <parameters> <Url>new_Message</Url> <PassParameters>false</PassParameters> <Security>true</Security> <Scrolling>auto</Scrolling> <Border>true</Border> <ShowOnMobileClient>false</ShowOnMobileClient> <WebResourceId>{C2EC617A-C3F0-E911-A83C-000D3AA058CB}</WebResourceId> </parameters> </control> <events> <event name="onload" application="false"> <dependencies /> </event> </events> </cell> </row> <row /> <row /> <row /> <row /> <row /> </rows> </section> </sections> </column> </columns> </tab> </tabs> <header id="{d5a03552-1183-4347-a237-1f894ba449eb}" columns="111" labelwidth="115" celllabelposition="Top"> <rows> <row> <cell id="{85e8169d-095b-28ca-181b-e5bea800b406}" showlabel="false"> <labels> <label description="" languagecode="1033" /> </labels> </cell> <cell id="{09b3d4db-45a1-c336-402a-1db54b9dc3ab}" showlabel="false"> <labels> <label description="" languagecode="1033" /> </labels> </cell> <cell id="{54b2d0a2-eb77-b3cd-b60a-03ea6944e98e}" showlabel="false" locklevel="0" labelid="{f9aa1083-9d13-42c8-a271-5a1f3da97354}" visible="false"> <labels> <label description="Owner" languagecode="1033" /> </labels> <control id="header_ownerid" datafieldname="ownerid" classid="{270BD3DB-D9AF-4782-9025-509E298DEC0A}" uniqueid="{7a6372d7-04ca-1470-5a33-69c01970ac29}" disabled="false"> <parameters> <AutoResolve>true</AutoResolve> <DisableMru>false</DisableMru> </parameters> </control> </cell> </row> </rows> </header> <controlDescriptions> <controlDescription forControl="{3ff9a528-dd50-4aca-8f10-2e5ed73513ad}"> <customControl formFactor="2" name="MscrmControls.CardFeedContainer.CardFeedContainer"> <parameters> <data-set name="SubGrid"> <ViewId>{92AFD454-0F2E-4397-A1C8-05E37C6AD699}</ViewId> <IsUserView>false</IsUserView> <TargetEntityType>actioncard</TargetEntityType> <FilteredViewIds>{92AFD454-0F2E-4397-A1C8-05E37C6AfD699}</FilteredViewIds> <EnableViewPicker>false</EnableViewPicker> </data-set> <EntityTypeCode type="Decimal" static="true">1</EntityTypeCode> <Location type="Decimal" static="true">1</Location> <msinternal.isvisibleinmocaonly static="true">true</msinternal.isvisibleinmocaonly> </parameters> </customControl> <customControl formFactor="0" name="MscrmControls.CardFeedContainer.CardFeedContainer"> <parameters> <data-set name="SubGrid"> <ViewId>{92AFD454-0F2E-4397-A1C8-05E37C6AD699}</ViewId> <IsUserView>false</IsUserView> <TargetEntityType>actioncard</TargetEntityType> <FilteredViewIds>{92AFD454-0F2E-4397-A1C8-05E37C6AfD699}</FilteredViewIds> <EnableViewPicker>false</EnableViewPicker> </data-set> <EntityTypeCode type="Decimal" static="true">1</EntityTypeCode> <Location type="Decimal" static="true">1</Location> <msinternal.isvisibleinmocaonly static="true">true</msinternal.isvisibleinmocaonly> </parameters> </customControl> <customControl formFactor="1" name="MscrmControls.CardFeedContainer.CardFeedContainer"> <parameters> <data-set name="SubGrid"> <ViewId>{92AFD454-0F2E-4397-A1C8-05E37C6AD699}</ViewId> <IsUserView>false</IsUserView> <TargetEntityType>actioncard</TargetEntityType> <FilteredViewIds>{92AFD454-0F2E-4397-A1C8-05E37C6AfD699}</FilteredViewIds> <EnableViewPicker>false</EnableViewPicker> </data-set> <EntityTypeCode type="Decimal" static="true">1</EntityTypeCode> <Location type="Decimal" static="true">1</Location> <msinternal.isvisibleinmocaonly static="true">true</msinternal.isvisibleinmocaonly> </parameters> </customControl> </controlDescription> </controlDescriptions> <DisplayConditions FallbackForm="true" Order="6"> <Everyone /> </DisplayConditions> </form>
Trick : Create one form in CRM with your requirement and use code to get FormXML of that form and use it to create other forms for other Entities.
Once you have FormXML ready.
You can use below code to create form for Entities.
static void Main(string[] args) { //Get Connection String var connectionString = ConfigurationManager.ConnectionStrings["CRMConnectionString"].ConnectionString; //Create CRM Service client to access CRM using (CrmServiceClient _client = new CrmServiceClient(connectionString)) { //Check if connection is ready if (_client.IsReady) { //Create list of string to Store Entities Name List<string> EntityNames = new List<string>(); //Get comma separated entities to list EntityNames.AddRange(EntitiesNamesstring.Split(',')); //loop though each entities foreach (string entityName in EntityNames) { Console.WriteLine("Currenct Entity Name " + entityName); QueryExpression queryExp = new QueryExpression { EntityName = "systemform", ColumnSet = new ColumnSet("name") }; //Filter System forms with name and current entity FilterExpression conditions = new FilterExpression(LogicalOperator.And); conditions.Conditions.Add(new ConditionExpression("name", ConditionOperator.Equal, "Block Access")); conditions.Conditions.Add(new ConditionExpression("objecttypecode", ConditionOperator.Equal, entityName)); queryExp.Criteria.AddFilter(conditions); //Sometime this line may fail, //reason : Some Entities Managed properties dont allow form creation. //So please implement Error handling over here if needed. EntityCollection forms = _client.RetrieveMultiple(queryExp); if (forms.Entities.Count > 0) // Entity Already have Block Access form. { //update Form, use this block to update existing form. Console.WriteLine("Update Form if needed."); return; } else // Entity Dont have block access form, so create new. { Console.WriteLine("Block Access Form will be created."); //read Form XML, Its good practice to keep XML file seprate. string xmlString = System.IO.File.ReadAllText("formXML.xml"); Entity createForm = new Entity("systemform"); createForm["name"] = "Block Access"; createForm["objecttypecode"] = entityName; createForm["type"] = new OptionSetValue(2); createForm["formxml"] = xmlString; try { //Sometime this line may fail //Reason : Form XML has some GUID which may be duplicate with your CRM application //when you get such error, log it, Search for those GUIDs in FormXML and remove that attribute, Not Node remove attribute and save and try again. //CRM will generate GUID by itself for those parts of form. _client.Create(createForm); Console.WriteLine("Form Created for Entity " + entityName); } catch (Exception) { //Even it fails log the Error and continue, so that you can run for others and deal with failed one later Console.WriteLine("Failed for Entity : " + entityName); continue; } } } } } }
Hope it works for you as it worked for me.
In case if you have get any error, feel free to post below and i will try to connect ASAP.
*This post is locked for comments