Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.01 KB | None | 0 0
  1. using Microsoft.Crm.Sdk.Messages;
  2. using Microsoft.Xrm.Client;
  3. using Microsoft.Xrm.Client.Services;
  4. using Microsoft.Xrm.Sdk;
  5. using Microsoft.Xrm.Sdk.Client;
  6. using Microsoft.Xrm.Sdk.Messages;
  7. using Microsoft.Xrm.Sdk.Metadata;
  8. using Microsoft.Xrm.Sdk.Query;
  9. using Microsoft.Xrm.Sdk.Workflow;
  10. using Microsoft.Xrm.Tooling;
  11. using Microsoft.Xrm.Tooling.Connector;
  12. using Microsoft.Xrm.Tooling.CrmConnectControl;
  13. using Microsoft.Xrm.Tooling.CrmConnector;
  14. using System;
  15. using System.Activities;
  16. using System.Collections.Generic;
  17. using System.Diagnostics;
  18. using System.Linq;
  19. using System.ServiceModel;
  20. using System.ServiceModel.Description;
  21.  
  22. namespace WBC.Workflow.Core.CustomSteps
  23. {
  24.     public class Generator
  25.  
  26.  
  27.     {
  28.         static IOrganizationService _service;
  29.  
  30.         static void Main(string[] args)
  31.         {
  32.  
  33.             //var connection = new CrmConnection("CRMOnline");
  34.             //var organizationService = new OrganizationService(connection);
  35.  
  36.             //var whoAmI = new WhoAmIRequest();
  37.  
  38.             //var result = organizationService.Execute(whoAmI) as WhoAmIResponse;
  39.  
  40.             //if(result != null)
  41.             //{
  42.             //    Console.WriteLine(result.ToString());
  43.             //}
  44.  
  45.             //else
  46.             //{
  47.             //    Console.WriteLine("null");
  48.             //}
  49.  
  50.  
  51.             //var createEntityRequest = new CreateEntityRequest
  52.             //{
  53.             //    Entity = new EntityMetadata
  54.             //    {
  55.             //        SchemaName = "testCustom",
  56.             //        DisplayName = new Label("This is a test", 1033),
  57.             //        DisplayCollectionName = new Label("these are tests", 1033),
  58.             //        Description = new Label("This is an entity used to test", 1033),
  59.             //        OwnershipType = OwnershipTypes.UserOwned,
  60.             //        IsActivity = false,
  61.  
  62.             //    },
  63.  
  64.             //    PrimaryAttribute = new StringAttributeMetadata
  65.             //    {
  66.             //        SchemaName = "wbc_testfield",
  67.             //        RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  68.             //        MaxLength = 100,
  69.             //        FormatName = StringFormatName.Text,
  70.             //        DisplayName = new Label("test field", 1033),
  71.             //        Description = new Label("primary field", 1033)
  72.             //    }
  73.             //};
  74.  
  75.             //organizationService.Execute(createEntityRequest);
  76.  
  77.  
  78.             ConnectToMSCRM("james.wray@wokingham.gov.uk", "Qwerty123!", "https://wokinghambc-dev-01.api.crm4.dynamics.com/XRMServices/2011/Organization.svc");
  79.  
  80.             var userId = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;
  81.  
  82.             if (userId != Guid.Empty)
  83.             {
  84.                 Console.WriteLine("connected");
  85.             }
  86.  
  87.             else
  88.             {
  89.                 Console.WriteLine("not connected");
  90.             }
  91.  
  92.  
  93.             var query = new QueryExpression
  94.             {
  95.                 EntityName = "publisher",
  96.                 ColumnSet = new ColumnSet(true),
  97.                 Criteria = new FilterExpression()
  98.             };
  99.  
  100.             var result = _service.RetrieveMultiple(query);
  101.            
  102.             foreach(var entity in result.Entities)
  103.             {
  104.                 Console.WriteLine(entity["friendlyname"] + "\n");
  105.                 Console.WriteLine(entity["customizationprefix"] + "\n");
  106.             }
  107.  
  108.             bool confirm = false;
  109.  
  110.             string inEntitySchemaName = "";
  111.             string inEntityDisplayName = "";
  112.             string inEntityPluralName = "";
  113.             string inEntityDescription = "";
  114.             string inTargetSolutionName = "";
  115.  
  116.             while (!confirm)
  117.             {
  118.  
  119.                 Console.Write("Enter Entity SchemeName: ");
  120.  
  121.                 inEntitySchemaName = Console.ReadLine();
  122.  
  123.                 Console.Write("Enter Entity DisplayName: ");
  124.                 inEntityDisplayName = Console.ReadLine();
  125.                 Console.Write("Enter Entity PluralName: ");
  126.                 inEntityPluralName = Console.ReadLine();
  127.                 Console.Write("Enter Entity Description: ");
  128.                 inEntityDescription = Console.ReadLine();
  129.                 Console.Write("Enter Target SolutionName: ");
  130.                 inTargetSolutionName = Console.ReadLine();
  131.  
  132.                 Console.WriteLine("\nIs this information correct?: \n");
  133.                 Console.WriteLine("Entity SchemaName: " + inEntitySchemaName);
  134.                 Console.WriteLine("Entity DispalyName: " + inEntityDisplayName);
  135.                 Console.WriteLine("Entity PluralName: " + inEntityPluralName);
  136.                 Console.WriteLine("Entity Description: " + inEntityDescription);
  137.                 Console.WriteLine("Target Solution: " + inTargetSolutionName);
  138.  
  139.                 string input;
  140.  
  141.                 do
  142.                 {
  143.                     Console.WriteLine("Y/N");
  144.                     input = Console.ReadLine();
  145.  
  146.  
  147.                     input = input.ToUpper();
  148.  
  149.  
  150.                     if (!input.Equals("Y") && !input.Equals("N"))
  151.                     {
  152.                         Console.WriteLine("Please choose Y/N (Yes/No)");
  153.                     }
  154.                 }
  155.  
  156.                 while (!input.Equals("Y") && !input.Equals("N"));
  157.  
  158.  
  159.  
  160.                 if (input.Equals("Y")) confirm = true;
  161.             }
  162.  
  163.  
  164.             //Console.WriteLine("Define the primary attribute metadata: ");
  165.  
  166.  
  167.  
  168.  
  169.             var createEntityRequest = new CreateEntityRequest
  170.             {
  171.                 Entity = new EntityMetadata
  172.                 {
  173.                     SchemaName = inEntitySchemaName,
  174.                     DisplayName = new Label(inEntityDisplayName, 1033),
  175.                     DisplayCollectionName = new Label(inEntityPluralName, 1033),
  176.                     Description = new Label(inEntityDescription, 1033),
  177.                     OwnershipType = OwnershipTypes.UserOwned,
  178.                     IsActivity = false
  179.  
  180.  
  181.                 },
  182.  
  183.                 PrimaryAttribute = new StringAttributeMetadata
  184.                 {
  185.                     SchemaName = "wbc_name",
  186.                     RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  187.                     MaxLength = 100,
  188.                     FormatName = StringFormatName.Text,
  189.                     DisplayName = new Label("Name", 1033),
  190.                     Description = new Label("primary field of this entity", 1033)
  191.  
  192.                 }
  193.             };
  194.  
  195.            
  196.             var executeResponse = (CreateEntityResponse)_service.Execute(createEntityRequest);
  197.  
  198.             var createdEntityId = executeResponse.EntityId;
  199.  
  200.             var addSolutionComponentRequest = new AddSolutionComponentRequest()
  201.             {
  202.                 AddRequiredComponents = true,
  203.                 SolutionUniqueName = inTargetSolutionName,
  204.                 ComponentId = createdEntityId,
  205.                 ComponentType = 1
  206.             };
  207.  
  208.             _service.Execute(addSolutionComponentRequest);
  209.  
  210.             var caseIdLookup = new CreateOneToManyRequest
  211.             {
  212.                 OneToManyRelationship = new OneToManyRelationshipMetadata
  213.                 {
  214.                     ReferencedEntity = "incident",
  215.                     ReferencingEntity = inEntitySchemaName,
  216.                     SchemaName = "wbc_incident_" + inEntitySchemaName,
  217.                     AssociatedMenuConfiguration = new AssociatedMenuConfiguration
  218.                     {
  219.                         Behavior = AssociatedMenuBehavior.UseLabel,
  220.                         Group = AssociatedMenuGroup.Details,
  221.                         Label = new Label("Case", 1033),
  222.                         Order = 10000
  223.                     },
  224.  
  225.                     CascadeConfiguration = new CascadeConfiguration
  226.                     {
  227.                         Assign = CascadeType.NoCascade,
  228.                         Delete = CascadeType.RemoveLink,
  229.                         Merge = CascadeType.NoCascade,
  230.                         Reparent = CascadeType.NoCascade,
  231.                         Share = CascadeType.NoCascade,
  232.                         Unshare = CascadeType.NoCascade
  233.                     }
  234.  
  235.  
  236.                 },
  237.  
  238.                 Lookup = new LookupAttributeMetadata
  239.                 {
  240.                     SchemaName = "wbc_caseid",
  241.                     DisplayName = new Label("Case", 1033),
  242.                     RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  243.                     Description = new Label("Lookup to associated case", 1033)
  244.                 }
  245.             };
  246.  
  247.  
  248.             _service.Execute(caseIdLookup);
  249.  
  250.             Console.WriteLine("Created wbc_caseid lookupfield");
  251.  
  252.             var contactLookup = new CreateOneToManyRequest
  253.             {
  254.                 OneToManyRelationship = new OneToManyRelationshipMetadata
  255.                 {
  256.                     ReferencedEntity = "contact",
  257.                     ReferencingEntity = inEntitySchemaName,
  258.                     SchemaName = "wbc_contact_" + inEntitySchemaName,
  259.                     AssociatedMenuConfiguration = new AssociatedMenuConfiguration
  260.                     {
  261.                         Behavior = AssociatedMenuBehavior.UseLabel,
  262.                         Group = AssociatedMenuGroup.Details,
  263.                         Label = new Label("Requesting Contact", 1033),
  264.                         Order = 10000
  265.                     },
  266.  
  267.                     CascadeConfiguration = new CascadeConfiguration
  268.                     {
  269.                         Assign = CascadeType.NoCascade,
  270.                         Delete = CascadeType.RemoveLink,
  271.                         Merge = CascadeType.NoCascade,
  272.                         Reparent = CascadeType.NoCascade,
  273.                         Share = CascadeType.NoCascade,
  274.                         Unshare = CascadeType.NoCascade
  275.                     }
  276.  
  277.  
  278.                 },
  279.  
  280.                 Lookup = new LookupAttributeMetadata
  281.                 {
  282.                     SchemaName = "wbc_requestingcontact",
  283.                     DisplayName = new Label("Requesting Contact", 1033),
  284.                     RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  285.                     Description = new Label("Lookup to associated contact", 1033)
  286.                 }
  287.             };
  288.  
  289.             _service.Execute(contactLookup);
  290.  
  291.             Console.WriteLine("Created contact lookup");
  292.  
  293.             var caseDefinitionLookup = new CreateOneToManyRequest
  294.             {
  295.                 OneToManyRelationship = new OneToManyRelationshipMetadata
  296.                 {
  297.                     ReferencedEntity = "wbc_casedefinition",
  298.                     ReferencingEntity = inEntitySchemaName,
  299.                     SchemaName = "wbc_casedefinition_" + inEntitySchemaName,
  300.                     AssociatedMenuConfiguration = new AssociatedMenuConfiguration
  301.                     {
  302.                         Behavior = AssociatedMenuBehavior.UseLabel,
  303.                         Group = AssociatedMenuGroup.Details,
  304.                         Label = new Label("case definition", 1033),
  305.                         Order = 10000
  306.                     },
  307.  
  308.                     CascadeConfiguration = new CascadeConfiguration
  309.                     {
  310.                         Assign = CascadeType.NoCascade,
  311.                         Delete = CascadeType.RemoveLink,
  312.                         Merge = CascadeType.NoCascade,
  313.                         Reparent = CascadeType.NoCascade,
  314.                         Share = CascadeType.NoCascade,
  315.                         Unshare = CascadeType.NoCascade
  316.                     }
  317.  
  318.  
  319.                 },
  320.  
  321.                 Lookup = new LookupAttributeMetadata
  322.                 {
  323.                     SchemaName = "wbc_casedefinitionid",
  324.                     DisplayName = new Label("Case Definition", 1033),
  325.                     RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  326.                     Description = new Label("Lookup to associated case definition", 1033)
  327.                 }
  328.             };
  329.  
  330.             _service.Execute(caseDefinitionLookup);
  331.  
  332.             Console.WriteLine("Created case definition lookup");
  333.  
  334.             var addedAttributes = new List<AttributeMetadata>();
  335.  
  336.             var KBSearchTextField = new StringAttributeMetadata
  337.             {
  338.                 SchemaName = "wbc_kbsearchdefaulttext",
  339.                 DisplayName = new Label("KB Search Text", 1033),
  340.                 RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  341.                 Description = new Label("Default text for the KB search", 1033),
  342.                 MaxLength = 100
  343.  
  344.             };
  345.  
  346.             addedAttributes.Add(KBSearchTextField);
  347.  
  348.             foreach(var metadata in addedAttributes)
  349.             {
  350.                 var createAttribute = new CreateAttributeRequest
  351.                 {
  352.                     EntityName = inEntitySchemaName,
  353.                     Attribute = metadata
  354.  
  355.                 };
  356.  
  357.                 _service.Execute(createAttribute);
  358.                 Console.WriteLine("Created attribute {0}", metadata.SchemaName);
  359.             }
  360.            
  361.             //var newSolution = new Entity("solution");
  362.  
  363.  
  364.  
  365.  
  366.         }
  367.  
  368.         public static void createLookup(string ReferencedEntityLogicalName, string ReferencingEntityLogicalName, string LookupFieldDisplayName,
  369.             string lookupFieldDescription, string lookupFieldSchemaName, string relationshipSchemaName)
  370.         {
  371.             var createLookUpField = new CreateOneToManyRequest
  372.             {
  373.                 OneToManyRelationship = new OneToManyRelationshipMetadata
  374.                 {
  375.                     ReferencedEntity = ReferencedEntityLogicalName,
  376.                     ReferencingEntity = ReferencingEntityLogicalName,
  377.                     SchemaName = "wbc_" + ReferencedEntityLogicalName + "_" + ReferencingEntityLogicalName,
  378.                     AssociatedMenuConfiguration = new AssociatedMenuConfiguration
  379.                     {
  380.                         Behavior = AssociatedMenuBehavior.UseLabel,
  381.                         Group = AssociatedMenuGroup.Details,
  382.                         Label = new Label(LookupFieldDisplayName, 1033),
  383.                         Order = 10000
  384.                     },
  385.  
  386.                     CascadeConfiguration = new CascadeConfiguration
  387.                     {
  388.                         Assign = CascadeType.NoCascade,
  389.                         Delete = CascadeType.RemoveLink,
  390.                         Merge = CascadeType.NoCascade,
  391.                         Reparent = CascadeType.NoCascade,
  392.                         Share = CascadeType.NoCascade,
  393.                         Unshare = CascadeType.NoCascade
  394.                     }
  395.  
  396.  
  397.                 },
  398.  
  399.                 Lookup = new LookupAttributeMetadata
  400.                 {
  401.                     SchemaName = lookupFieldSchemaName,
  402.                     DisplayName = new Label(LookupFieldDisplayName, 1033),
  403.                     RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  404.                     Description = new Label(lookupFieldDescription, 1033)
  405.                 }
  406.             };
  407.  
  408.             _service.Execute(createLookUpField);
  409.         }
  410.  
  411.         public static void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri)
  412.         {
  413.             try
  414.             {
  415.                 ClientCredentials credentials = new ClientCredentials();
  416.                 credentials.UserName.UserName = UserName;
  417.                 credentials.UserName.Password = Password;
  418.                 Uri serviceUri = new Uri(SoapOrgServiceUri);
  419.                 OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
  420.                 proxy.EnableProxyTypes();
  421.                 _service = proxy;
  422.             }
  423.             catch (Exception ex)
  424.             {
  425.                 Console.WriteLine("Error while connecting to CRM " + ex.Message);
  426.                 Console.ReadKey();
  427.             }
  428.         }
  429.  
  430.     }
  431. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement