Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Crm.Sdk.Messages;
- using Microsoft.Xrm.Client;
- using Microsoft.Xrm.Client.Services;
- using Microsoft.Xrm.Sdk;
- using Microsoft.Xrm.Sdk.Client;
- using Microsoft.Xrm.Sdk.Messages;
- using Microsoft.Xrm.Sdk.Metadata;
- using Microsoft.Xrm.Sdk.Query;
- using Microsoft.Xrm.Sdk.Workflow;
- using Microsoft.Xrm.Tooling;
- using Microsoft.Xrm.Tooling.Connector;
- using Microsoft.Xrm.Tooling.CrmConnectControl;
- using Microsoft.Xrm.Tooling.CrmConnector;
- using System;
- using System.Activities;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.ServiceModel;
- using System.ServiceModel.Description;
- namespace WBC.Workflow.Core.CustomSteps
- {
- public class Generator
- {
- static IOrganizationService _service;
- static void Main(string[] args)
- {
- //var connection = new CrmConnection("CRMOnline");
- //var organizationService = new OrganizationService(connection);
- //var whoAmI = new WhoAmIRequest();
- //var result = organizationService.Execute(whoAmI) as WhoAmIResponse;
- //if(result != null)
- //{
- // Console.WriteLine(result.ToString());
- //}
- //else
- //{
- // Console.WriteLine("null");
- //}
- //var createEntityRequest = new CreateEntityRequest
- //{
- // Entity = new EntityMetadata
- // {
- // SchemaName = "testCustom",
- // DisplayName = new Label("This is a test", 1033),
- // DisplayCollectionName = new Label("these are tests", 1033),
- // Description = new Label("This is an entity used to test", 1033),
- // OwnershipType = OwnershipTypes.UserOwned,
- // IsActivity = false,
- // },
- // PrimaryAttribute = new StringAttributeMetadata
- // {
- // SchemaName = "wbc_testfield",
- // RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
- // MaxLength = 100,
- // FormatName = StringFormatName.Text,
- // DisplayName = new Label("test field", 1033),
- // Description = new Label("primary field", 1033)
- // }
- //};
- //organizationService.Execute(createEntityRequest);
- ConnectToMSCRM("james.wray@wokingham.gov.uk", "Qwerty123!", "https://wokinghambc-dev-01.api.crm4.dynamics.com/XRMServices/2011/Organization.svc");
- var userId = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;
- var user = _service.Retrieve("systemuser", userId, new ColumnSet(true));
- if (userId != Guid.Empty)
- {
- Console.WriteLine(user["fullname"]);
- }
- else
- {
- Console.WriteLine("not connected");
- }
- var query = new QueryExpression
- {
- EntityName = "publisher",
- ColumnSet = new ColumnSet(true),
- Criteria = new FilterExpression()
- };
- var result = _service.RetrieveMultiple(query);
- foreach(var entity in result.Entities)
- {
- Console.WriteLine(entity["friendlyname"] + "\n");
- Console.WriteLine(entity["customizationprefix"] + "\n");
- }
- var reference = result.Entities[3].ToEntityReference();
- var solution = new Entity("solution");
- solution["friendlyname"] = "teeest";
- solution["uniquename"] = "wbc_TESTseeolution";
- solution["version"] = "1.0.0.0";
- solution["publisherid"] = reference;
- solution["ismanaged"] = false;
- Guid diditwork = _service.Create(solution);
- if (diditwork != Guid.Empty)
- {
- Console.WriteLine(diditwork);
- }
- else
- {
- Console.WriteLine("Didnt work");
- }
- bool confirm = false;
- string inEntitySchemaName = "";
- string inEntityDisplayName = "";
- string inEntityPluralName = "";
- string inEntityDescription = "";
- string inTargetSolutionName = "";
- while (!confirm)
- {
- Console.Write("Enter Entity SchemeName: ");
- inEntitySchemaName = Console.ReadLine();
- Console.Write("Enter Entity DisplayName: ");
- inEntityDisplayName = Console.ReadLine();
- Console.Write("Enter Entity PluralName: ");
- inEntityPluralName = Console.ReadLine();
- Console.Write("Enter Entity Description: ");
- inEntityDescription = Console.ReadLine();
- Console.Write("Enter Target SolutionName: ");
- inTargetSolutionName = Console.ReadLine();
- Console.WriteLine("\nIs this information correct?: \n");
- Console.WriteLine("Entity SchemaName: " + inEntitySchemaName);
- Console.WriteLine("Entity DispalyName: " + inEntityDisplayName);
- Console.WriteLine("Entity PluralName: " + inEntityPluralName);
- Console.WriteLine("Entity Description: " + inEntityDescription);
- Console.WriteLine("Target Solution: " + inTargetSolutionName);
- string input;
- do
- {
- Console.WriteLine("Y/N");
- input = Console.ReadLine();
- input = input.ToUpper();
- if (!input.Equals("Y") && !input.Equals("N"))
- {
- Console.WriteLine("Please choose Y/N (Yes/No)");
- }
- }
- while (!input.Equals("Y") && !input.Equals("N"));
- if (input.Equals("Y")) confirm = true;
- }
- //Console.WriteLine("Define the primary attribute metadata: ");
- var createEntityRequest = new CreateEntityRequest
- {
- Entity = new EntityMetadata
- {
- SchemaName = inEntitySchemaName,
- DisplayName = new Label(inEntityDisplayName, 1033),
- DisplayCollectionName = new Label(inEntityPluralName, 1033),
- Description = new Label(inEntityDescription, 1033),
- OwnershipType = OwnershipTypes.UserOwned,
- IsActivity = false,
- },
- PrimaryAttribute = new StringAttributeMetadata
- {
- SchemaName = "wbc_name",
- RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
- MaxLength = 100,
- FormatName = StringFormatName.Text,
- DisplayName = new Label("Name", 1033),
- Description = new Label("primary field of this entity", 1033)
- }
- };
- var executeResponse = (CreateEntityResponse)_service.Execute(createEntityRequest);
- var createdEntityId = executeResponse.EntityId;
- var addSolutionComponentRequest = new AddSolutionComponentRequest()
- {
- AddRequiredComponents = true,
- SolutionUniqueName = inTargetSolutionName,
- ComponentId = createdEntityId,
- ComponentType = 1
- };
- _service.Execute(addSolutionComponentRequest);
- var caseIdLookup = new CreateOneToManyRequest
- {
- OneToManyRelationship = new OneToManyRelationshipMetadata
- {
- ReferencedEntity = "incident",
- ReferencingEntity = inEntitySchemaName,
- SchemaName = "wbc_incident_" + inEntitySchemaName,
- AssociatedMenuConfiguration = new AssociatedMenuConfiguration
- {
- Behavior = AssociatedMenuBehavior.UseLabel,
- Group = AssociatedMenuGroup.Details,
- Label = new Label("Case", 1033),
- Order = 10000
- },
- CascadeConfiguration = new CascadeConfiguration
- {
- Assign = CascadeType.NoCascade,
- Delete = CascadeType.RemoveLink,
- Merge = CascadeType.NoCascade,
- Reparent = CascadeType.NoCascade,
- Share = CascadeType.NoCascade,
- Unshare = CascadeType.NoCascade
- }
- },
- Lookup = new LookupAttributeMetadata
- {
- SchemaName = "wbc_caseid",
- DisplayName = new Label("Case", 1033),
- RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
- Description = new Label("Lookup to associated case", 1033)
- }
- };
- _service.Execute(caseIdLookup);
- Console.WriteLine("Created wbc_caseid lookupfield");
- var contactLookup = new CreateOneToManyRequest
- {
- OneToManyRelationship = new OneToManyRelationshipMetadata
- {
- ReferencedEntity = "contact",
- ReferencingEntity = inEntitySchemaName,
- SchemaName = "wbc_contact_" + inEntitySchemaName,
- AssociatedMenuConfiguration = new AssociatedMenuConfiguration
- {
- Behavior = AssociatedMenuBehavior.UseLabel,
- Group = AssociatedMenuGroup.Details,
- Label = new Label("Requesting Contact", 1033),
- Order = 10000
- },
- CascadeConfiguration = new CascadeConfiguration
- {
- Assign = CascadeType.NoCascade,
- Delete = CascadeType.RemoveLink,
- Merge = CascadeType.NoCascade,
- Reparent = CascadeType.NoCascade,
- Share = CascadeType.NoCascade,
- Unshare = CascadeType.NoCascade
- }
- },
- Lookup = new LookupAttributeMetadata
- {
- SchemaName = "wbc_requestingcontact",
- DisplayName = new Label("Requesting Contact", 1033),
- RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
- Description = new Label("Lookup to associated contact", 1033)
- }
- };
- _service.Execute(contactLookup);
- Console.WriteLine("Created contact lookup");
- var caseDefinitionLookup = new CreateOneToManyRequest
- {
- OneToManyRelationship = new OneToManyRelationshipMetadata
- {
- ReferencedEntity = "wbc_casedefinition",
- ReferencingEntity = inEntitySchemaName,
- SchemaName = "wbc_casedefinition_" + inEntitySchemaName,
- AssociatedMenuConfiguration = new AssociatedMenuConfiguration
- {
- Behavior = AssociatedMenuBehavior.UseLabel,
- Group = AssociatedMenuGroup.Details,
- Label = new Label("case definition", 1033),
- Order = 10000
- },
- CascadeConfiguration = new CascadeConfiguration
- {
- Assign = CascadeType.NoCascade,
- Delete = CascadeType.RemoveLink,
- Merge = CascadeType.NoCascade,
- Reparent = CascadeType.NoCascade,
- Share = CascadeType.NoCascade,
- Unshare = CascadeType.NoCascade
- }
- },
- Lookup = new LookupAttributeMetadata
- {
- SchemaName = "wbc_casedefinitionid",
- DisplayName = new Label("Case Definition", 1033),
- RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
- Description = new Label("Lookup to associated case definition", 1033)
- }
- };
- _service.Execute(caseDefinitionLookup);
- Console.WriteLine("Created case definition lookup");
- var addedAttributes = new List<AttributeMetadata>();
- var KBSearchTextField = new StringAttributeMetadata
- {
- SchemaName = "wbc_kbsearchdefaulttext",
- DisplayName = new Label("KB Search Text", 1033),
- RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
- Description = new Label("Default text for the KB search", 1033),
- MaxLength = 100
- };
- addedAttributes.Add(KBSearchTextField);
- foreach(var metadata in addedAttributes)
- {
- var createAttribute = new CreateAttributeRequest
- {
- EntityName = inEntitySchemaName,
- Attribute = metadata
- };
- _service.Execute(createAttribute);
- Console.WriteLine("Created attribute {0}", metadata.SchemaName);
- }
- //var newSolution = new Entity("solution");
- }
- public static void createLookup(string ReferencedEntityLogicalName, string ReferencingEntityLogicalName, string LookupFieldDisplayName,
- string lookupFieldDescription, string lookupFieldSchemaName, string relationshipSchemaName)
- {
- var createLookUpField = new CreateOneToManyRequest
- {
- OneToManyRelationship = new OneToManyRelationshipMetadata
- {
- ReferencedEntity = ReferencedEntityLogicalName,
- ReferencingEntity = ReferencingEntityLogicalName,
- SchemaName = "wbc_" + ReferencedEntityLogicalName + "_" + ReferencingEntityLogicalName,
- AssociatedMenuConfiguration = new AssociatedMenuConfiguration
- {
- Behavior = AssociatedMenuBehavior.UseLabel,
- Group = AssociatedMenuGroup.Details,
- Label = new Label(LookupFieldDisplayName, 1033),
- Order = 10000
- },
- CascadeConfiguration = new CascadeConfiguration
- {
- Assign = CascadeType.NoCascade,
- Delete = CascadeType.RemoveLink,
- Merge = CascadeType.NoCascade,
- Reparent = CascadeType.NoCascade,
- Share = CascadeType.NoCascade,
- Unshare = CascadeType.NoCascade
- }
- },
- Lookup = new LookupAttributeMetadata
- {
- SchemaName = lookupFieldSchemaName,
- DisplayName = new Label(LookupFieldDisplayName, 1033),
- RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
- Description = new Label(lookupFieldDescription, 1033)
- }
- };
- _service.Execute(createLookUpField);
- }
- public static void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri)
- {
- try
- {
- ClientCredentials credentials = new ClientCredentials();
- credentials.UserName.UserName = UserName;
- credentials.UserName.Password = Password;
- Uri serviceUri = new Uri(SoapOrgServiceUri);
- OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
- proxy.EnableProxyTypes();
- _service = proxy;
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error while connecting to CRM " + ex.Message);
- Console.ReadKey();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement