Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.85 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.             var user = _service.Retrieve("systemuser", userId, new ColumnSet(true));
  83.  
  84.             if (userId != Guid.Empty)
  85.             {
  86.                 Console.WriteLine(user["fullname"]);
  87.             }
  88.  
  89.             else
  90.             {
  91.                 Console.WriteLine("not connected");
  92.             }
  93.  
  94.          
  95.             var query = new QueryExpression
  96.             {
  97.                 EntityName = "publisher",
  98.                 ColumnSet = new ColumnSet(true),
  99.                 Criteria = new FilterExpression()
  100.             };
  101.  
  102.             var result = _service.RetrieveMultiple(query);
  103.            
  104.             foreach(var entity in result.Entities)
  105.             {
  106.                 Console.WriteLine(entity["friendlyname"] + "\n");
  107.                 Console.WriteLine(entity["customizationprefix"] + "\n");
  108.             }
  109.  
  110.  
  111.             var reference = result.Entities[3].ToEntityReference();
  112.  
  113.  
  114.             var solution = new Entity("solution");
  115.             solution["friendlyname"] = "teeest";
  116.             solution["uniquename"] = "wbc_TESTseeolution";
  117.             solution["version"] = "1.0.0.0";
  118.             solution["publisherid"] = reference;
  119.             solution["ismanaged"] = false;
  120.  
  121.             Guid diditwork = _service.Create(solution);
  122.  
  123.             if (diditwork != Guid.Empty)
  124.             {
  125.                 Console.WriteLine(diditwork);
  126.             }
  127.  
  128.             else
  129.             {
  130.                 Console.WriteLine("Didnt work");
  131.             }
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.             bool confirm = false;
  140.  
  141.             string inEntitySchemaName = "";
  142.             string inEntityDisplayName = "";
  143.             string inEntityPluralName = "";
  144.             string inEntityDescription = "";
  145.             string inTargetSolutionName = "";
  146.  
  147.             while (!confirm)
  148.             {
  149.  
  150.                 Console.Write("Enter Entity SchemeName: ");
  151.  
  152.                 inEntitySchemaName = Console.ReadLine();
  153.  
  154.                 Console.Write("Enter Entity DisplayName: ");
  155.                 inEntityDisplayName = Console.ReadLine();
  156.                 Console.Write("Enter Entity PluralName: ");
  157.                 inEntityPluralName = Console.ReadLine();
  158.                 Console.Write("Enter Entity Description: ");
  159.                 inEntityDescription = Console.ReadLine();
  160.                 Console.Write("Enter Target SolutionName: ");
  161.                 inTargetSolutionName = Console.ReadLine();
  162.  
  163.                 Console.WriteLine("\nIs this information correct?: \n");
  164.                 Console.WriteLine("Entity SchemaName: " + inEntitySchemaName);
  165.                 Console.WriteLine("Entity DispalyName: " + inEntityDisplayName);
  166.                 Console.WriteLine("Entity PluralName: " + inEntityPluralName);
  167.                 Console.WriteLine("Entity Description: " + inEntityDescription);
  168.                 Console.WriteLine("Target Solution: " + inTargetSolutionName);
  169.  
  170.                 string input;
  171.  
  172.                 do
  173.                 {
  174.                     Console.WriteLine("Y/N");
  175.                     input = Console.ReadLine();
  176.  
  177.  
  178.                     input = input.ToUpper();
  179.  
  180.  
  181.                     if (!input.Equals("Y") && !input.Equals("N"))
  182.                     {
  183.                         Console.WriteLine("Please choose Y/N (Yes/No)");
  184.                     }
  185.                 }
  186.  
  187.                 while (!input.Equals("Y") && !input.Equals("N"));
  188.  
  189.  
  190.  
  191.                 if (input.Equals("Y")) confirm = true;
  192.             }
  193.  
  194.  
  195.             //Console.WriteLine("Define the primary attribute metadata: ");
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.             var createEntityRequest = new CreateEntityRequest
  207.             {
  208.                 Entity = new EntityMetadata
  209.                 {
  210.                     SchemaName = inEntitySchemaName,
  211.                     DisplayName = new Label(inEntityDisplayName, 1033),
  212.                     DisplayCollectionName = new Label(inEntityPluralName, 1033),
  213.                     Description = new Label(inEntityDescription, 1033),
  214.                     OwnershipType = OwnershipTypes.UserOwned,
  215.                     IsActivity = false,
  216.                    
  217.                    
  218.  
  219.  
  220.                 },
  221.  
  222.                 PrimaryAttribute = new StringAttributeMetadata
  223.                 {
  224.                     SchemaName = "wbc_name",
  225.                     RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  226.                     MaxLength = 100,
  227.                     FormatName = StringFormatName.Text,
  228.                     DisplayName = new Label("Name", 1033),
  229.                     Description = new Label("primary field of this entity", 1033)
  230.  
  231.                 }
  232.             };
  233.  
  234.        
  235.  
  236.            
  237.             var executeResponse = (CreateEntityResponse)_service.Execute(createEntityRequest);
  238.  
  239.             var createdEntityId = executeResponse.EntityId;
  240.  
  241.             var addSolutionComponentRequest = new AddSolutionComponentRequest()
  242.             {
  243.                 AddRequiredComponents = true,
  244.                 SolutionUniqueName = inTargetSolutionName,
  245.                 ComponentId = createdEntityId,
  246.                 ComponentType = 1
  247.             };
  248.  
  249.             _service.Execute(addSolutionComponentRequest);
  250.  
  251.             var caseIdLookup = new CreateOneToManyRequest
  252.             {
  253.                 OneToManyRelationship = new OneToManyRelationshipMetadata
  254.                 {
  255.                     ReferencedEntity = "incident",
  256.                     ReferencingEntity = inEntitySchemaName,
  257.                     SchemaName = "wbc_incident_" + inEntitySchemaName,
  258.                     AssociatedMenuConfiguration = new AssociatedMenuConfiguration
  259.                     {
  260.                         Behavior = AssociatedMenuBehavior.UseLabel,
  261.                         Group = AssociatedMenuGroup.Details,
  262.                         Label = new Label("Case", 1033),
  263.                         Order = 10000
  264.                     },
  265.  
  266.                     CascadeConfiguration = new CascadeConfiguration
  267.                     {
  268.                         Assign = CascadeType.NoCascade,
  269.                         Delete = CascadeType.RemoveLink,
  270.                         Merge = CascadeType.NoCascade,
  271.                         Reparent = CascadeType.NoCascade,
  272.                         Share = CascadeType.NoCascade,
  273.                         Unshare = CascadeType.NoCascade
  274.                     }
  275.  
  276.  
  277.                 },
  278.  
  279.                 Lookup = new LookupAttributeMetadata
  280.                 {
  281.                     SchemaName = "wbc_caseid",
  282.                     DisplayName = new Label("Case", 1033),
  283.                     RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  284.                     Description = new Label("Lookup to associated case", 1033)
  285.                 }
  286.             };
  287.  
  288.  
  289.             _service.Execute(caseIdLookup);
  290.  
  291.             Console.WriteLine("Created wbc_caseid lookupfield");
  292.  
  293.             var contactLookup = new CreateOneToManyRequest
  294.             {
  295.                 OneToManyRelationship = new OneToManyRelationshipMetadata
  296.                 {
  297.                     ReferencedEntity = "contact",
  298.                     ReferencingEntity = inEntitySchemaName,
  299.                     SchemaName = "wbc_contact_" + inEntitySchemaName,
  300.                     AssociatedMenuConfiguration = new AssociatedMenuConfiguration
  301.                     {
  302.                         Behavior = AssociatedMenuBehavior.UseLabel,
  303.                         Group = AssociatedMenuGroup.Details,
  304.                         Label = new Label("Requesting Contact", 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_requestingcontact",
  324.                     DisplayName = new Label("Requesting Contact", 1033),
  325.                     RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  326.                     Description = new Label("Lookup to associated contact", 1033)
  327.                 }
  328.             };
  329.  
  330.             _service.Execute(contactLookup);
  331.  
  332.             Console.WriteLine("Created contact lookup");
  333.  
  334.             var caseDefinitionLookup = new CreateOneToManyRequest
  335.             {
  336.                 OneToManyRelationship = new OneToManyRelationshipMetadata
  337.                 {
  338.                     ReferencedEntity = "wbc_casedefinition",
  339.                     ReferencingEntity = inEntitySchemaName,
  340.                     SchemaName = "wbc_casedefinition_" + inEntitySchemaName,
  341.                     AssociatedMenuConfiguration = new AssociatedMenuConfiguration
  342.                     {
  343.                         Behavior = AssociatedMenuBehavior.UseLabel,
  344.                         Group = AssociatedMenuGroup.Details,
  345.                         Label = new Label("case definition", 1033),
  346.                         Order = 10000
  347.                     },
  348.  
  349.                     CascadeConfiguration = new CascadeConfiguration
  350.                     {
  351.                         Assign = CascadeType.NoCascade,
  352.                         Delete = CascadeType.RemoveLink,
  353.                         Merge = CascadeType.NoCascade,
  354.                         Reparent = CascadeType.NoCascade,
  355.                         Share = CascadeType.NoCascade,
  356.                         Unshare = CascadeType.NoCascade
  357.                     }
  358.  
  359.  
  360.                 },
  361.  
  362.                 Lookup = new LookupAttributeMetadata
  363.                 {
  364.                     SchemaName = "wbc_casedefinitionid",
  365.                     DisplayName = new Label("Case Definition", 1033),
  366.                     RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  367.                     Description = new Label("Lookup to associated case definition", 1033)
  368.                 }
  369.             };
  370.  
  371.             _service.Execute(caseDefinitionLookup);
  372.  
  373.             Console.WriteLine("Created case definition lookup");
  374.  
  375.             var addedAttributes = new List<AttributeMetadata>();
  376.  
  377.             var KBSearchTextField = new StringAttributeMetadata
  378.             {
  379.                 SchemaName = "wbc_kbsearchdefaulttext",
  380.                 DisplayName = new Label("KB Search Text", 1033),
  381.                 RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  382.                 Description = new Label("Default text for the KB search", 1033),
  383.                 MaxLength = 100
  384.  
  385.             };
  386.  
  387.             addedAttributes.Add(KBSearchTextField);
  388.  
  389.             foreach(var metadata in addedAttributes)
  390.             {
  391.                 var createAttribute = new CreateAttributeRequest
  392.                 {
  393.                     EntityName = inEntitySchemaName,
  394.                     Attribute = metadata
  395.  
  396.                 };
  397.  
  398.                 _service.Execute(createAttribute);
  399.                 Console.WriteLine("Created attribute {0}", metadata.SchemaName);
  400.             }
  401.            
  402.             //var newSolution = new Entity("solution");
  403.  
  404.  
  405.  
  406.  
  407.         }
  408.  
  409.         public static void createLookup(string ReferencedEntityLogicalName, string ReferencingEntityLogicalName, string LookupFieldDisplayName,
  410.             string lookupFieldDescription, string lookupFieldSchemaName, string relationshipSchemaName)
  411.         {
  412.             var createLookUpField = new CreateOneToManyRequest
  413.             {
  414.                 OneToManyRelationship = new OneToManyRelationshipMetadata
  415.                 {
  416.                     ReferencedEntity = ReferencedEntityLogicalName,
  417.                     ReferencingEntity = ReferencingEntityLogicalName,
  418.                     SchemaName = "wbc_" + ReferencedEntityLogicalName + "_" + ReferencingEntityLogicalName,
  419.                     AssociatedMenuConfiguration = new AssociatedMenuConfiguration
  420.                     {
  421.                         Behavior = AssociatedMenuBehavior.UseLabel,
  422.                         Group = AssociatedMenuGroup.Details,
  423.                         Label = new Label(LookupFieldDisplayName, 1033),
  424.                         Order = 10000
  425.                     },
  426.  
  427.                     CascadeConfiguration = new CascadeConfiguration
  428.                     {
  429.                         Assign = CascadeType.NoCascade,
  430.                         Delete = CascadeType.RemoveLink,
  431.                         Merge = CascadeType.NoCascade,
  432.                         Reparent = CascadeType.NoCascade,
  433.                         Share = CascadeType.NoCascade,
  434.                         Unshare = CascadeType.NoCascade
  435.                     }
  436.  
  437.  
  438.                 },
  439.  
  440.                 Lookup = new LookupAttributeMetadata
  441.                 {
  442.                     SchemaName = lookupFieldSchemaName,
  443.                     DisplayName = new Label(LookupFieldDisplayName, 1033),
  444.                     RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  445.                     Description = new Label(lookupFieldDescription, 1033)
  446.                 }
  447.             };
  448.  
  449.             _service.Execute(createLookUpField);
  450.         }
  451.  
  452.         public static void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri)
  453.         {
  454.             try
  455.             {
  456.                 ClientCredentials credentials = new ClientCredentials();
  457.                 credentials.UserName.UserName = UserName;
  458.                 credentials.UserName.Password = Password;
  459.                 Uri serviceUri = new Uri(SoapOrgServiceUri);
  460.                
  461.                 OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
  462.                 proxy.EnableProxyTypes();
  463.                 _service = proxy;
  464.             }
  465.             catch (Exception ex)
  466.             {
  467.                 Console.WriteLine("Error while connecting to CRM " + ex.Message);
  468.                 Console.ReadKey();
  469.             }
  470.         }
  471.  
  472.     }
  473. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement