Friday, November 24, 2017

101 Most Used Dynamics 365 CRM code snippets

Here is an inventory of most used code snippet utilized in Dynamics 365 CRM.remember to add required namespace whenever required while inserting code. Hold the CRM SDK folder able to take the reference of the assemblies for beneath namespace.
  • Code to join organization (C#) String userName = ""; String userPwd = ""; String userDomain= ""; String crmServer = ""; String crmPort = ";"; String crmOrg = ""; String homeRealm = ""; NetworkCredential netCred = new system.internet.NetworkCredential(userName, userPwd, userDomain, homeRealm); CrmServiceClient crmSvc = new CrmServiceClient(netCred, crmServer, crmPort, crmOrg);
  • Derive Context, CRM service object in Plugin (C#) IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = ServiceFactory.CreateOrganizationService(context.UserId);
  • Validate if a specific attribute existing in the plugin context input parameter (C#) if (context.InputParameters.incorporates("goal") && Context.InputParameters["Target"] is Entity) //Your code goes right here
  • examine if a particular attribute latest in a entity (C#) if (entityObject.Attributes.ContainsKey("") //Your code goes here
  • Get cost of attribute from entity (C#) entityObject.GetAttributeValue(""); entityObject. GetAttributeValue ("").id.ToString(); bool myAttributeVal = entityObject.GetAttributeValue("") string myAttributeVal = entityObject.GetAttributeValue("")
  • Convert EntityCollection to DataTable (C#) public DataTable convertEntityCollectionToDataTable(EntityCollection EC) { DataTable dt = new DataTable(); int totalCount = EC.Entities.count; for (int i = 0; i 
  • assess if dataset is legitimate (C#) bool flag=false; if(Objdataset.Tables.count number>0) if(Objdataset.Tables[0].Rows.count number>0) Flag=proper;
  • Retrieve Optionset value from an AliasedValue (C#) AliasedValue aliasedval= entityObject.GetAttributeValue("").value; int optionSetVal = ((OptionSetValue) aliasedval).value; //word: for different datatypes simply use the datatype in above line of code to get the cost.
  • Code to stop Plugin from infinite loop (C#) // add this code earlier than your actual plugin common sense begins if (context.Depth >1) return;
  • Code to make use of Shared Variable in Plugins (C#) //Set the shared variable value context.SharedVariables.Add("PrimaryContact", (Object)contact.ToString()); //Get the Shared Variable cost Guid contact =new Guid((string)context.SharedVariables["PrimaryContact"]);
  • Get the Pre & put up Entity photos in Plugin (C#) Entity preEntityImage= (Entity)context.PreEntityImages["ImageName"]; Entity postEntityImage = (Entity)context.PostEntityImages["ImageName"]; //observe : The image Names are described in Plugin registration tool towards the SDK steps.
  • trace Messages the use of Tracing provider in Plugin (C#) ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); tracingService.trace ("");
  • Get Optionset text in Plugin (C#) if(entity.includes("")) string optionsettext = entity.FormattedValues["attributename"];
  • Retrieve entity listing facts the use of question Expression(C#) //question for the GUID of the Account using Account identify QueryExpression query = new QueryExpression("account"); string[] cols = "accountid", "identify"; query.criteria = new FilterExpression(); query.criteria.AddCondition("identify", ConditionOperator.Equal, ); query.ColumnSet = new ColumnSet(cols); EntityCollection account = crmServices.RetrieveMultiple(question); //Casting the reference to GUID Guid accountId = (Guid)account[0].Attributes["accountid"];
  • Retrieve entity list information using Fetch XML(C#) string fetchxml = @" "; EntityCollection outcomes = _serviceProxy.RetrieveMultiple(new FetchExpression(fetchxml));
  • Retrieve entity list facts the use of Guid of the listing(C#) ColumnSet cs = new ColumnSet("attribute1","attribute2"); Entity outcomes = _serviceProxy.Retrieve ("", new Guid(""), cs);
  • replace Entity listing (C#) Entity entityObj = new Entity(); entityObj.LogicalName = ""; entityObj.id = entityObj. = crmservice.replace(entityObj); //observe: whereas assigning the price use the correct datatype converison. lets say you are updating an //optionset then you have to use new OptionSetValue().
  • Delete Entity checklist (C#) crmservice.Delete("",new Guid(""));
  • Create an Entity listing the use of CRM provider (C#) Entity entityObj = new Entity(); entityObj.LogicalName = ""; entityObj. = ; entityObj. = ; crmservice.Create(entityObj);
  • Retrieve checklist the usage of paging if extra listing existing (C#) // The number of facts per page to retrieve. int queryCount = 200; // Initialize the web page quantity. int pageNumber = 1; // Initialize the variety of records. int recordCount = 0; // define the circumstance expression for retrieving facts. ConditionExpression pagecondition = new ConditionExpression(); pagecondition.AttributeName = "parentaccountid"; pagecondition.Operator = ConditionOperator.Equal; pagecondition.Values.Add(_parentAccountId); // Create the query expression and add circumstance. QueryExpression pagequery = new QueryExpression(); pagequery.EntityName = "account"; pagequery.criteria.AddCondition(pagecondition); pagequery.ColumnSet.AddColumns("identify", "emailaddress1"); // Assign the pageinfo houses to the question expression. pagequery.PageInfo = new PagingInfo(); pagequery.PageInfo.count = queryCount; pagequery.PageInfo.PageNumber = pageNumber; // The current paging cookie. When retrieving the primary web page // pagingCookie may still be null. pagequery.PageInfo.PagingCookie = null; Console.WriteLine("Retrieving account data in pages...\n"); Console.WriteLine("#\tAccount name\t\tEmail handle"); while (real) // Retrieve the page. EntityCollection effects =_service.RetrieveMultiple(pagequery); if (results.Entities != null) // Retrieve all information from the result set. foreach (Account acct in consequences.Entities) Console.WriteLine("0.\t1\t2", ++recordCount, acct.name, acct.EMailAddress1); // verify for extra information, if it returns genuine. if (results.MoreRecords) Console.WriteLine("\n****************\nPage number 0\n****************", pagequery.PageInfo.PageNumber); Console.WriteLine("#\tAccount name\t\tEmail tackle"); // Increment the page number to retrieve the next web page. pagequery.PageInfo.PageNumber++; // Set the paging cookie to the paging cookie back from current outcomes. pagequery.PageInfo.PagingCookie = consequences.PagingCookie; else // If no greater statistics are in the outcomes nodes, exit the loop. ruin;
  • Get Optionset Values and text Metadata in CRM (C#) var attributeRequest = new RetrieveAttributeRequest EntityLogicalName = "entityName", LogicalName = "attributeName", RetrieveAsIfPublished = actual ; var attributeResponse = (RetrieveAttributeResponse)provider.Execute(attributeRequest); var attributeMetadata = (EnumAttributeMetadata)attributeResponse.AttributeMetadata; var optionList = (from o in attributeMetadata.OptionSet.options select new price = o.cost, text = o.Label.UserLocalizedLabel.Label ).ToList();
  • replace the status of facts (C#) Guid leadId = new Guid("D020F344-A470-E111-B9DA-00155D04DC01"); SetStateRequest request = new SetStateRequest EntityMoniker = new EntityReference("lead", leadId), State = new OptionSetValue(2), popularity = new OptionSetValue(6) ; crmService.Execute(request); //observe: State : lively/Inactive
  • read Excel Sheet checklist to records Set (C#) //Use the under namespace first: the use of Excel = Microsoft.workplace.Interop.Excel; the use of gadget.statistics; //Code to study excel facts OleDbConnection conn = new OleDbConnection("issuer=Microsoft.Jet.OLEDB.four.0;data source='c:\\examine.xls';prolonged houses=Excel 8.0;"); OleDbDataAdapter cmd = new OleDbDataAdapter("opt for * from [Sheet1$]", conn); cmd.TableMappings.Add("desk", "TestTable"); DataSet DtSet = DataSet(); cmd.Fill(DtSet);
  • Supress duplicate Detection Rule while developing checklist (C#) // Create operation by using suppressing duplicate detection CreateRequest reqCreate = new CreateRequest(); reqCreate.goal = account; reqCreate.Parameters.Add("SuppressDuplicateDetection", authentic); // observe : trade to false to prompt the reproduction detection. CreateResponse createResponse = (CreateResponse)_service.Execute(reqCreate); Guid _dupAccountId = createResponse.identity; // Retrieve the account containing with its few attributes. ColumnSet cols = new ColumnSet( new String[] "identify", "accountnumber"); Entity retrievedAccount = _service.Retrieve("account", _dupAccountId, cols); // update the existing account with new account number. retrievedAccount.AccountNumber = "ACC006"; // update operation – replace record, if a duplicate is not discovered. UpdateRequest reqUpdate = new UpdateRequest(); reqUpdate.goal = retrievedAccount; reqUpdate["SuppressDuplicateDetection"] = false; // reproduction detection is activated. // replace the account listing. UpdateResponse updateResponse = (UpdateResponse)_service.Execute(reqUpdate);
  • Assigning Values to distinct DataType box whereas growing list (C#) DataType example Single Line of textual content account["name"] = Convert.ToString("Microsoft India"); //String choice Set account["accountcategorycode"] = new OptionSetValue(1); //Int Two alternate options account["donotbulkemail"] = false; //boolean value -authentic/false image entityObj["entityimage"] = File.ReadAllBytes("images\\144x144.png"); whole number account["numberofemployees"] = 50; //int cost Floating point number account["address1_latitude"] = Convert.ToDouble(-0.330); //double Decimal quantity account["attributename"] = Convert.ToDecimal(three.5); //deciaml foreign money account["revenue"] = new funds(50.60); //deciml cost multiple lines of textual content account["description"] = Convert.ToString("here is a fine acc"); Date and Time account["lastusedincampaign"] = new DateTime(2017,11,23); //yyyy,mm,dd search for account["address1_addressid"] = new EntityReference("tackle",new Guid("11111111-1111-1111-1111-111111111111")); //32 bit guid
  • Generate Entity Metadata the usage of C# RetrieveAllEntitiesRequest request = new RetrieveAllEntitiesRequest() EntityFilters = EntityFilters.Entity, RetrieveAsIfPublished = real ; // Retrieve the MetaData. RetrieveAllEntitiesResponse response = (RetrieveAllEntitiesResponse)_serviceProxy.Execute(request); // Create an instance of StreamWriter to write down text to a file. // The the usage of observation also closes the StreamWriter. // To view this file, right click the file and choose open with Excel. // Excel will work out the schema and display the suggestions in columns. String filename = String.Concat("EntityInfo.xml"); using (StreamWriter sw = new StreamWriter(filename)) // Create Xml creator. XmlTextWriter metadataWriter = new XmlTextWriter(sw); // delivery Xml File. metadataWriter.WriteStartDocument(); // Metadata Xml Node. metadataWriter.WriteStartElement("Metadata"); foreach (EntityMetadata currentEntity in response.EntityMetadata) //if (currentEntity.IsIntersect.value == false) if (true) // start Entity Node metadataWriter.WriteStartElement("Entity"); // Write the Entity's tips. metadataWriter.WriteElementString("EntitySchemaName", currentEntity.SchemaName); metadataWriter.WriteElementString("OTC", currentEntity.ObjectTypeCode.ToString()); metadataWriter.WriteElementString("OwnershipType", currentEntity.OwnershipType.cost.ToString()); if (currentEntity.DisplayName.UserLocalizedLabel != null) metadataWriter.WriteElementString("DisplayName", currentEntity.DisplayName.UserLocalizedLabel.Label); if (currentEntity.DisplayCollectionName.UserLocalizedLabel != null) metadataWriter.WriteElementString("DisplayCollectionName", currentEntity.DisplayCollectionName.UserLocalizedLabel.Label); metadataWriter.WriteElementString("IntroducedVersion", currentEntity.IntroducedVersion.ToString()); metadataWriter.WriteElementString("AutoRouteToOwnerQueue", currentEntity.AutoRouteToOwnerQueue.ToString()); metadataWriter.WriteElementString("CanBeInManyToMany", currentEntity.CanBeInManyToMany.cost.ToString()); metadataWriter.WriteElementString("CanBePrimaryEntityInRelationship", currentEntity.CanBePrimaryEntityInRelationship.value.ToString()); metadataWriter.WriteElementString("CanBeRelatedEntityInRelationship", currentEntity.CanBeRelatedEntityInRelationship.cost.ToString()); metadataWriter.WriteElementString("CanCreateAttributes", currentEntity.CanCreateAttributes.cost.ToString()); metadataWriter.WriteElementString("CanCreateCharts", currentEntity.CanCreateCharts.value.ToString()); metadataWriter.WriteElementString("CanCreateForms", currentEntity.CanCreateForms.price.ToString()); metadataWriter.WriteElementString("CanCreateViews", currentEntity.CanCreateViews.cost.ToString()); metadataWriter.WriteElementString("CanModifyAdditionalSettings", currentEntity.CanModifyAdditionalSettings.price.ToString()); metadataWriter.WriteElementString("CanTriggerWorkflow", currentEntity.CanTriggerWorkflow.cost.ToString()); metadataWriter.WriteElementString("IsActivity", currentEntity.IsActivity.cost.ToString()); //metadataWriter.WriteElementString("ActivityTypeMask", currentEntity.ActivityTypeMask.ToString()); metadataWriter.WriteElementString("IsActivityParty", currentEntity.IsActivityParty.value.ToString()); metadataWriter.WriteElementString("IsAuditEnabled", currentEntity.IsAuditEnabled.price.ToString()); metadataWriter.WriteElementString("IsAvailableOffline", currentEntity.IsAvailableOffline.ToString()); metadataWriter.WriteElementString("IsChildEntity", currentEntity.IsChildEntity.ToString()); metadataWriter.WriteElementString("IsConnectionsEnabled", currentEntity.IsConnectionsEnabled.ManagedPropertyLogicalName.ToString()); metadataWriter.WriteElementString("IsCustomEntity", currentEntity.IsCustomEntity.cost.ToString()); metadataWriter.WriteElementString("IsCustomizable", currentEntity.IsCustomizable.cost.ToString()); metadataWriter.WriteElementString("IsDocumentManagementEnabled", currentEntity.IsDocumentManagementEnabled.value.ToString()); metadataWriter.WriteElementString("IsDuplicateDetectionEnabled", currentEntity.IsDuplicateDetectionEnabled.value.ToString()); if (currentEntity.IsEnabledForCharts != null) metadataWriter.WriteElementString("IsEnabledForCharts", currentEntity.IsEnabledForCharts.cost.ToString()); metadataWriter.WriteElementString("IsImportable", currentEntity.IsImportable.price.ToString()); metadataWriter.WriteElementString("IsIntersect", currentEntity.IsIntersect.price.ToString()); metadataWriter.WriteElementString("IsMailMergeEnabled", currentEntity.IsMailMergeEnabled.price.ToString()); metadataWriter.WriteElementString("IsManaged", currentEntity.IsManaged.value.ToString()); metadataWriter.WriteElementString("IsMappable", currentEntity.IsMappable.cost.ToString()); metadataWriter.WriteElementString("IsReadingPaneEnabled", currentEntity.IsReadingPaneEnabled.cost.ToString()); metadataWriter.WriteElementString("IsRenameable", currentEntity.IsRenameable.cost.ToString()); metadataWriter.WriteElementString("IsValidForAdvancedFind", currentEntity.IsValidForAdvancedFind.value.ToString()); metadataWriter.WriteElementString("IsValidForQueue", currentEntity.IsValidForQueue.value.ToString()); metadataWriter.WriteElementString("IsVisibleInMobile", currentEntity.IsVisibleInMobile.cost.ToString()); metadataWriter.WriteElementString("PrimaryIdAttribute", currentEntity.PrimaryIdAttribute); metadataWriter.WriteElementString("PrimaryNameAttribute", currentEntity.PrimaryNameAttribute); metadataWriter.WriteElementString("ReportViewName", currentEntity.ReportViewName); metadataWriter.WriteElementString("RecurrenceBaseEntityLogicalName", currentEntity.RecurrenceBaseEntityLogicalName); if (currentEntity.Description.UserLocalizedLabel != null) metadataWriter.WriteElementString("Description", currentEntity.Description.UserLocalizedLabel.Label); // conclusion Entity Node metadataWriter.WriteEndElement(); // conclusion Metadata Xml Node metadataWriter.WriteEndElement(); metadataWriter.WriteEndDocument(); // shut xml author. metadataWriter.shut();
  • check if historic price and New value of box are different in Plugin photos (C#) Entity preEntityImage= (Entity)context.PreEntityImages["ImageName"]; Entity postEntityImage = (Entity)context.PostEntityImages["ImageName"]; var oldNameValue = preEntityImage.GetAttribute("name"); var newNameValue = postEntityImage.GetAttribute("identify"); if(oldNameValue.ToLower() != newNameValue.ToLower()) //Your code goes here
  • Early certain Syntax developing Contact record (C#) Contact contact = new Contact() FirstName = "Charles", LastName = "Brown", Address1_Line1 = "123 leading St." ; _contactId = _serviceProxy.Create(contact);
  • Late certain Syntax creating Contact record (C#) Entity contactent = new Entity("contact"); contactent["firstname"] = "Charles"; contactent["lastname"] = "Brown "; contactent["address1_line1"] = "123 leading St."; Guid _contactId = _serviceProxy.Create(contactent);
  • Declaring enter & Output Arguments,Default value & Set Requirement degree of the Parameter in custom Workflow activities(C#) For Boolean category(enter) [Input("Bool input")] [Default("True")] public InArgument BoolFlag get; set; For Boolean type(Output) [Output("Bool input")] public OutArgument BoolFlag get; set; For Boolean class (each enter and Output) [Input("Bool input")] [Output("Bool output")] [Default("False")] public InOutArgument BoolFlag get; set; DateTime [Input("DateTime input")] [Output("DateTime output")] [Default("2013-07-09T02:54:00Z")] public InOutArgument DateTime get; set; Decimal [Input("Decimal input")] [Output("Decimal output")] [Default("20.75")] public InOutArgument Decimal get; set; Double [Input("Double input")] [Output("Double output")] [Default("200.2")] public InOutArgument Double get; set; Integer [Input("Int input")] [Output("Int output")] [Default("2322")] public InOutArgument Int get; set; funds (foreign money) [Input("Money input")] [Output("Money output")] [Default("232.3")] public InOutArgument cash get; set; OptionSetValue [Input("OptionSetValue input")] [Output("OptionSetValue output")] [AttributeTarget("account", "industrycode")] [Default("3")] public InOutArgument OptionSetValue get; set; String [Input("String input")] [Output("String output")] [Default("string default")] public InOutArgument String get; set; Entity Reference [Input("EntityReference input")] [Output("EntityReference output")] [ReferenceTarget("account")] [Default("3B036E3E-94F9-DE11-B508-00155DBA2902", "account")] public InOutArgument AccountReference get; set; . Required Argument Attribute [RequiredArgument] [Input("Update next Anniversary date for")] [ReferenceTarget("contact")] public InArgument Contact get; set; Output Parameters //here's the identify of the parameter that may be lower back returned to the workflow [Output("Credit Score")] //this line identifies the selected attribute which will be passed returned to the workflow [AttributeTarget(CustomEntity, "new_creditscore")] // declares the output parameter and proclaims the relevant statistics classification of the parameter being passed lower back. public OutArgument CreditScore get;set;
  • Get cost from enter Argument in custom Workflow actions (C#) If enter argument is asserted as beneath then we can get the value as defined below. [Input("Date Of Birth")] public InArgument DOB get; set; blanketed override void Execute(CodeActivityContext context) DateTime dtDOB = DOB.Get(context);
  • Set value from Out Argument in customized Workflow actions (C#) If Output argument is declared as under then we are able to get the price as defined under. [Output("FinalAge")] public OutArgument Age get; set; covered override void Execute(CodeActivityContext context) int CalculatedAge=30; Age.Set(context, CalculatedAge);
  • A sample custom Workflow Code (C#) the use of device.Collections.regularly occurring; using gadget.Linq; using gadget.actions; using Microsoft.Xrm.Sdk.Workflow; namespace GetAge public sealed type CalculateAge:CodeActivity [Input("Date Of Birth")] public InArgument DOB get; set; protected override void Execute(CodeActivityContext context) DateTime dtDOB = DOB.Get(context); int CalculatedAge = Convert.ToInt32((DateTime.Now.Subtract(dtDOB).TotalDays)/365); Age.Set(context, CalculatedAge); [Output("FinalAge")] public OutArgument Age get; set; 
  • A pattern Plugin Code (C#) using equipment; the usage of system.ServiceModel; using Microsoft.Xrm.Sdk; namespace Microsoft.Crm.Sdk.Samples public category FollowupPlugin: IPlugin /// /// A plug-in that creates a comply with-up assignment undertaking when a brand new account is created. /// /// Register this plug-in on the Create message, account entity, /// and asynchronous mode. /// public void Execute(IServiceProvider serviceProvider) //Extract the tracing carrier for use in debugging sandboxed plug-ins. ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); // gain the execution context from the carrier company. IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); // The InputParameters collection incorporates all the records passed in the message request. if (context.InputParameters.contains("goal") && context.InputParameters["Target"] is Entity) // acquire the goal entity from the input parameters. Entity entity = (Entity)context.InputParameters["Target"]; // check that the goal entity represents an account. // If no longer, this plug-in changed into no longer registered accurately. if (entity.LogicalName != "account") return; are trying // Create a job activity to observe up with the account consumer in 7 days. Entity followup = new Entity("task"); followup["subject"] = "send e mail to the brand new customer."; followup["description"] = "observe up with the client. determine if there are any new concerns that want decision."; followup["scheduledstart"] = DateTime.Now.AddDays(7); followup["scheduledend"] = DateTime.Now.AddDays(7); followup["category"] = context.PrimaryEntityName; // consult with the account in the project exercise. if (context.OutputParameters.carries("identification")) Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString()); string regardingobjectidType = "account"; followup["regardingobjectid"] = new EntityReference(regardingobjectidType, regardingobjectid); // acquire the organization carrier reference. IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService carrier = serviceFactory.CreateOrganizationService(context.UserId); // Create the project in Microsoft Dynamics CRM. tracingService.trace("FollowupPlugin: growing the task endeavor."); provider.Create(followup); trap (FaultException ex) throw new InvalidPluginExecutionException("An error took place in the FollowupPlugin plug-in.", ex); seize (Exception ex) tracingService.hint("FollowupPlugin: 0", ex.ToString()); throw;
  • Write Log Messages into a textual content File Public void WriteLog(string msg) String datestamp = DateTime.Now.Date.ToString("d"); string course = string.structure(@"E:\AppServ\0_log.txt", datestamp); // outline the path to shop the file if (!File.Exists(path)) File.Create(route); TextWriter tw = new StreamWriter(route); tw.WriteLine(msg); tw.shut();
  • Code for Exception handling (C#) are attempting //Your code catch (FaultException ex) Console.WriteLine("The software terminated with an error."); Console.WriteLine("Timestamp: 0", ex.aspect.Timestamp); Console.WriteLine("Code: 0", ex.detail.ErrorCode); Console.WriteLine("Message: 0", ex.detail.Message); Console.WriteLine("internal Fault: 0", null == ex.aspect.InnerFault ? "No internal Fault": "Has internal Fault"); trap (equipment.TimeoutException ex) Console.WriteLine("The utility terminated with an error."); Console.WriteLine("Message: 0", ex.Message); Console.WriteLine("Stack trace: 0", ex.StackTrace); Console.WriteLine("inner Fault: 0", null == ex.InnerException.Message ? "No inner Fault": ex.InnerException.Message); trap (system.Exception ex) Console.WriteLine("The application terminated with an error."); Console.WriteLine(ex.Message); // reveal the details of the internal exception. if (ex.InnerException != null) Console.WriteLine(ex.InnerException.Message); FaultException fe = ex.InnerException as FaultException; if (fe != null) Console.WriteLine("Timestamp: 0", fe.element.Timestamp); Console.WriteLine("Code: 0", fe.detail.ErrorCode); Console.WriteLine("Message: 0", fe.detail.Message); Console.WriteLine("hint: 0", fe.detail.TraceText); Console.WriteLine("internal Fault: 0", null == fe.aspect.InnerFault ? "No internal Fault": "Has inner Fault");
  • Create Outlook offline Filters (C#) String contactName = String.layout("offlineFilteredContact 0", DateTime.Now.ToLongTimeString()); String fetchXml = String.layout(""+ ""+ "", contactName); SavedQuery filter = new SavedQuery(); filter.FetchXml = fetchXml; filter.IsQuickFindQuery = false; filter.QueryType = SavedQueryQueryType.OfflineFilters; filter.ReturnedTypeCode = Contact.EntityLogicalName; filter.name = "ReadOnlyFilter_"+ contactName; filter.Description = "sample offline filter for Contact entity"; _offlineFilter = _serviceProxy.Create(filter);
  • Encryption and Decryption the usage of C# the use of device; the use of device.protection.Cryptography; the usage of system.textual content; namespace SecurityModule public category EncryptionManager public static string Encrypt(string input, string key) byte[] inputArray = UTF8Encoding.UTF8.GetBytes(input); TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider(); tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key); tripleDES.Mode = CipherMode.ECB; tripleDES.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = tripleDES.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.size); tripleDES.Clear(); return Convert.ToBase64String(resultArray, 0, resultArray.size); public static string Decrypt(string input, string key) byte[] inputArray = Convert.FromBase64String(enter); TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider(); tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key); tripleDES.Mode = CipherMode.ECB; tripleDES.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = tripleDES.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.size); tripleDES.Clear(); return UTF8Encoding.UTF8.GetString(resultArray);
  • examine All text to string and skim all Line to array kind a textual content File (C#) string course = @"c:\temp\MyTest.txt"; // Open the file to study from. string readText = File.ReadAllText(path); string[] readText = File.ReadAllLines(route);
  • break up textual content the use of separator in C# string source = "ONE|TWO|THREE"; string[] stringSeparators = new string[] "; string[] outcomes= supply.break up(stringSeparators, StringSplitOptions.None);
  • access container by way of name on kind (Formscript/Javascript) var myAttribute = Xrm.web page.getAttribute("identify");
  • access box by using Index on form (Formscript/Javascript) var myAttribute = Xrm.page.getAttribute(0);
  • Get all Attributes on form (Formscript/Javascript) var allAttributes = Xrm.page.getAttribute();
  • Get All optionset Attributes of the kind var optionsetAttributes = Xrm.web page.getAttribute( feature (attribute, index) return attribute.getAttributeType() == "optionset"; );
  • Alert All attributes with Required Validation of the kind (Javascript) feature AlertRequiredAttributes () Xrm.web page.statistics.entity.attributes.forEach( characteristic (attribute, index) if (attribute.getRequiredLevel() == "required") alert(attribute.getName()); );
  • Get the price of a box/attribute on kind (Javascript) var fieldvalue= Xrm.web page.getAttribute("identify").getValue();
  • Set the cost of a box/attribute on kind (Javascript) Xrm.page.getAttribute("name").setValue("new value");
  • Get the textual content price of the currently chosen alternative of an optionset attribute (Javascript) var addressType = Xrm.web page.getAttribute("address1_addresstypecode").getText();
  • Get the at the moment chosen alternative object in an optionset attribute (Javascript) var addressTypeOption = Xrm.web page.getAttribute("address1_addresstypecode").getSelectedOption();
  • Get the category of attribute (Javascript) var attributeType = Xrm.web page.getAttribute("address1_addresstypecode").getAttributeType();
  • Get how the attribute is formatted (Javascript) var attributeFormat = Xrm.page.getAttribute(0).getFormat();
  • Get the initial value of a Booleanor Optionset attribute (Javascript) var attributeInitialValue = Xrm.page.getAttribute("address1_addresstypecode").getInitialValue();
  • assess even if an attribute cost has modified (Javascript) var isNameChanged = Xrm.page.getAttribute("identify").getIsDirty();
  • Get maximum cost of a container (Javascript) Var limitMaxvalue = Xrm.web page.getAttribute("creditlimit").getMax();
  • Get the optimum allowed size for an attribute that includes a string (Javascript) var maxNameLen= Xrm.page.getAttribute("name").getMaxLength();
  • Get minimal value of a container (Javascript) Var limitMinvalue = Xrm.web page.getAttribute("creditlimit").getMin();
  • Get the logical name of an attribute (Javascript) var attributeName = Xrm.page.getAttribute("creditlimit").getName();
  • Get the alternative set textual content by using value of attribute (Javascript) var addressTypeOption = Xrm.web page.getAttribute("address1_addresstypecode").getOption(1); alert(addressTypeOption.text); //shows 'invoice To'
  • Get the requirement degree of an attribute (Javascript) var creditLimitRequired = Xrm.web page.getAttribute("creditlimit").getRequiredLevel();
  • Set the requirement degree of an attribute as Required (Javascript) Xrm.page.getAttribute("creditlimit").setRequiredLevel("required"); //required/none/ informed
  • assess if the facts in an attribute might be submitted when the facts is saved (Javascript) var nameSubmitMode = Xrm.web page.getAttribute("identify").getSubmitMode(); // always/under no circumstances/ soiled
  • control whether statistics in an attribute should be saved when the list is saved Xrm.page.getAttribute("identify").setSubmitMode("always");
  • stay away from a record from being saved. feature My_PreventSaveFunction(eContext) eContext.getEventArgs().preventDefault(); //word: Use the eContext parameter to seize the execution context and use the preventDefault system protected with the experience arguments.
  • examine what action initiated the save.event (Javascript) characteristic GetSaveModeTextFunction(eContext) var saveModeCode = eContext.getEventArgs().getSaveMode(); var saveModeText = "Unknown"; change (saveModeCode) case 1: saveModeText = "retailer"; damage; case 2: saveModeText = "SaveAndClose"; ruin; case 5: saveModeText = "Deactivate"; smash; case 6: saveModeText = "Reactivate"; break; case 7: saveModeText = "send"; destroy; case 15: saveModeText = "Disqualify"; spoil; case sixteen: saveModeText = "Qualify"; wreck; case 47: saveModeText = "Assign"; damage; case fifty eight: saveModeText = "SaveAsCompleted"; destroy; case 59: saveModeText = "SaveAndNew"; damage; case 70: saveModeText = "AutoSave"; break; return saveModeText; //note :flow the execution context from adventure handler screen
  • screen a notification message close the box manage to point out that records is not valid. (Javascript) Xrm.page.getAttribute("name").controls.forEach( feature (handle, i) manage.setNotification("'input cost' isn't legitimate."); )
  • eradicate a notification message already displayed for a manage. Xrm.page.getAttribute("identify").controls.forEach( feature (manage, i) control.clearNotification(); )
  • screen form degree notifications message (javascript) Xrm.web page.ui.setFormNotification( "good day", "information", "myMsg" ); // myMsg is a uniquid used whereas showing the message.
  • remove kind stage notifications message (Javascript) Xrm.web page.ui.clearFormNotification("myMsg");
  • monitor a non-blocking alert dialog with a callback feature var alertDisplayedFlag = false; Xrm.Utility.alertDialog( "displaying Alert Window", function () alertDisplayedFlag = true; ) //word: display an alert and set the price of the alertDisplayedFlag variable when it's closed.
  • reveal a non-blocking confirm dialog with different callbacks counting on the button clicked by using the person. var agree = false; Xrm.Utility.confirmDialog( "Do you compromise?", feature () agree = real;, function () agree = false; );
  • access all the controls for a particular attribute var nameControls = Xrm.page.getAttribute("identify").controls.get();
  • access a control by means of name var nameControl = Xrm.page.getControl("name");
  • entry a control through index var firstControl = Xrm.web page.getControl(0);
  • entry all controls var allControls = Xrm.web page.getControl();
  • Get all optionset controls var optionsetControls = Xrm.web page.getControl(function (handle, index) return handle.getControlType() == "optionset"; )
  • Get all controls of a specific component to a Tab var generalTabAccountInfoSectionControls = Xrm.web page.ui.tabs.get("normal").sections.get("tackle").controls.get();
  • Disable all controls for an attribute Xrm.page.getAttribute("identify").controls.forEach(feature (handle, index) handle.setDisabled(proper); );
  • entry a field from header part with attribute identify is customername var nameControlInHeader = Xrm.web page.getControl("header_customername");
  • access a box control from business process circulate var nameControlInBPF = Xrm.web page.getControl("header_process_name");
  • verify if a manage is visible var isNameVisible = Xrm.page.getControl("identify").getVisible();
  • hide or demonstrate a handle of a box Xrm.page.getControl("identify").setVisible(false);
  • Get a reference to the attribute for the manage var nameAttribute = Xrm.web page.getControl("identify").getAttribute();
  • Get all optionset controls var optionSetControls = Xrm.page.getControl(feature (handle, index) return handle.getControlType() == "optionset"; );
  • Return all disabled controls var disabledControls = Xrm.web page.getControl(characteristic(handle, index) return control.getDisabled(); );
  • Disable or allow each and every handle Xrm.page.getAttribute("identify").controls.forEach(function (manage, index) manage.setDisabled(true); );
  • Get the label for a handle var nameControlLabel = Xrm.web page.getControl("identify").getLabel();
  • trade the label for a manage Xrm.web page.getControl("identify").setLabel("company name");
  • Get the identify of a control var firstControlName = Xrm.web page.getControl(0).getName();
  • Get the dad or mum of a handle var parentSection = Xrm.web page.getControl("name").getParent();
  • Set focus on a handle Xrm.web page.getControl("name").setFocus();
  • Add an choice to an optionset manage var addressTypeCodeControl = Xrm.page.getControl("address1_addresstypecode"); var billToAddressOption = addressTypeCodeControl.getAttribute().getOption(1); addressTypeCodeControl.clearOptions(); addressTypeCodeControl.addOption(billToAddressOption);
  • eradicate all of the alternate options from an optionset control Xrm.web page.getControl("address1_addresstypecode").clearOptions();
  • remove a single alternative from an optionset manage. Xrm.page.getControl("address1_addresstypecode").removeOption(1);
  • Get the URL for the content material at present displayed in an IFRAME. var iframeSource = Xrm.web page.getControl("IFRAME_targetPage").getSrc();
  • Set the URL for the content material to be displayed in an IFRAME. Xrm.page.getControl("IFRAME_targetPage").setSrc("http://www.bing.com");
  • Add a customized view for a lookup. var viewId = "C7034F4F-6F92-4DD7-BD9D-9B9C1E996380"; var viewDisplayName = "SDK pattern View"; var fetchXml = ""+ ""+ ""+ ""+ ""+ ""+ ""+ ""+ ""+ ""+ ""+ ""+ ""+ ""+ ""+ ""+ ""; var layoutXml = ""+ ""+ ""+ ""+ ""+ ""+ ""+ ""+ ""; Xrm.page.getControl("parentaccountid").addCustomView(viewId, "account", viewDisplayName, fetchXml, layoutXml, true);
  • Get and Set the default view for a search for. var defaultViewId = Xrm.web page.getControl("parentaccountid").getDefaultView(); var viewId = "C7034F4F-6F92-4DD7-BD9D-9B9C1E996380"; Xrm.page.getControl("parentaccountid").setDefaultView(viewId);
  • Filter the facts back for a look up control var Sdk = window.Sdk || ; Sdk.filterCustomerAccounts = characteristic () //only show bills with the classification 'favorite customer' var customerAccountFilter = ""; Xrm.web page.getControl("parentaccountid").addCustomFilter(customerAccountFilter, "account"); //set 'Sdk.setParentAccountIdFilter' within the chance form Onload adventure handler Sdk.setParentAccountIdFilter = characteristic () Xrm.page.getControl("parentaccountid").addPreSearch(Sdk.filterCustomerAccounts);
  • Refresh the records displayed in the subgrid Xrm.page.getControl("accountcontactsgrid").refresh();
  • Specify even if a date handle may still reveal the time component of the date. Xrm.web page.getControl("createdon").setShowTime(false);
  • kind Navigation Scripts, Tabs and Sections Script and other critical scripts assignment system illustration Get the identify of a navigation merchandise getId var navItemIds = [];Xrm.page.ui.navigation.items.forEach(characteristic (merchandise, index) navItemIds.push(item.getId()) );Create a navItemIds array that carries the identification values of every navigation merchandise in the Xrm.web page.ui.navigation.gadgets assortment. Get the label of a navigation merchandise. getLabel var navAddressesLabel = Xrm.page.ui.navigation.objects.get(“navAddresses”).getLabel();Assign the label for the greater Addresses navigation item to the navAddressesLabel variable. Set the label of a navigation item setLabel Xrm.page.ui.navigation.objects.get(“navAddresses”).setLabel(“other Addresses”);exchange the extra Addresses navigation item label to different Addresses. display or disguise a navigation merchandise setVisible Xrm.web page.ui.navigation.objects.get(“navAddresses”).setVisible(false);disguise the greater Addresses navigation merchandise. verify if a navigation item is visible getVisible var moreAddressesVisible = Xrm.web page.ui.navigation.gadgets.get(“navAddresses”).getVisible()Assign a Boolean cost to the moreAddressesVisible variable to symbolize even if the extra Addresses navigation item is visible. Set focus on a navigation merchandise. setFocus Xrm.web page.ui.navigation.objects.get(“navAddresses”).setFocus();Set focus on the greater Addresses navigation merchandise. determine if a tab is multiplied or collapsed getDisplayState var isGeneralTabExpanded = (Xrm.web page.ui.tabs.get(“widespread”).getDisplayState() == “elevated”)Assign a Boolean cost to the isGeneralTabExpanded variable that suggests even if the popular tab is expanded. expand or cave in a tab setDisplayState Xrm.web page.ui.tabs.get(“familiar”).setDisplayState(“collapsed”);give way the popular tab. assess if a tab is visible getVisible var isGeneralTabVisible = Xrm.page.ui.tabs.get(“conventional”).getVisible();Assign a Boolean cost to the isGeneralTabVisible variable indicating no matter if the Generaltab is seen. show or disguise a tab setVisible Xrm.web page.ui.tabs.get(“ordinary”).setVisible(false);conceal the commonplace tab. Get the label for a tab getLabel var generalTabLabel = Xrm.web page.ui.tabs.get(“ordinary”).getLabel();Assign the general tab label to the generalTabLabel variable. trade the label for a tab setLabel Xrm.page.ui.tabs.get(“established”).setLabel(“essential”);alternate the regular tab label to foremost. Set center of attention on a tab setFocus Xrm.web page.ui.tabs.get(“familiar”).setFocus();Set focus on the generic tab. Get the name of the tab getName var firstTabName = Xrm.page.ui.tabs.get(0).getName();Assign the name of the primary tab to the firstTabName variable. Get the mother or father tab of a bit getParent Xrm.web page.getControl(“industrycode”).getParent().getParent().setFocus();Set focus on the tab that incorporates the trade field. check if a bit is seen getVisible var industrySectionVisible = Xrm.web page.getControl(“industrycode”).getParent().getVisible();Assign a Boolean price to the industrySectionVisible variable indicating no matter if the part containing the business container is seen. display or disguise a piece setVisible Xrm.page.getControl(“industrycode”).getParent().setVisible(false);hide the section that includes the trade container. Get the label for a piece getLabel var industryFieldSectionLabel = Xrm.page.getControl(“industrycode”).getParent().getLabel();Assign the label of the section containing the industry container to the industryFieldSectionLabel variable. change the label for a bit setLabel Xrm.web page.getControl(“industrycode”).getParent().setLabel(“certain assistance”);alternate the label of the section that includes the industry field to designated counsel. Add a feature to the OnSaveevent addOnSave Xrm.web page.statistics.entity.addOnSave(myFunction);Add the myFunction function to the OnSave event. get rid of a function from the OnSaveevent removeOnSave Xrm.web page.records.entity.removeOnSave(myFunction);eradicate the myFunction characteristic to the OnSave experience. Add a feature to the OnChangeevent of an attribute. addOnChange Xrm.page.getAttribute(“name”).addOnChange(myFunction);Add the myFunction feature to the OnChange event of the Account identify field. eradicate a feature from the OnChangeevent of an attribute removeOnChange Xrm.page.getAttribute(“identify”).removeOnChange(myFunction);remove the myFunction characteristic to the OnChange experience of the Account identify container. Add a characteristic to the PreSearchevent of a search for manage addPreSearch here code sample is for the chance form Account (parentaccountid) lookup. When the Sdk.setParentAccountIdFilter feature is determined within the kind Onloadevent handler, the Sdk.filterCustomAccounts function is delivered to the PreSearch experience for that search for. The outcome is that handiest bills with the class(accountcategorycode) cost of favourite client (1) could be lower back.JavaScriptvar Sdk = window.Sdk || ;Sdk.filterCustomerAccounts = characteristic () //only demonstrate debts with the class ‘preferred customer’var customerAccountFilter = ““;Xrm.page.getControl(“parentaccountid”).addCustomFilter(customerAccountFilter, “account”);//set ‘Sdk.setParentAccountIdFilter’ in the probability kind Onload event handlerSdk.setParentAccountIdFilter = feature () Xrm.page.getControl(“parentaccountid”).addPreSearch(Sdk.filterCustomerAccounts); Get the URL to connect to the organization. getClientUrl var serverUrl = Xrm.web page.context.getClientUrl();Assign a string that represents the URL to the serverUrlvariable. Get the entertaining identifier for the existing person. getUserId var userId = Xrm.web page.context.getUserId();Assign a string that represents the person’s identification to the userId variable. Get the identify of the current person. getUserName var userName = Xrm.web page.context.getUserName();Assign a string that represents the person’s name to the userName variable.This system is only available for up-to-date entities. Get the language code that represents the person’s preferred person interface language. getUserLcid var userLCID = Xrm.page.context.getUserLcid();Assign a host that shows the consumer’s favorite language to the userLCID variable. Get an array of strings that represents the GUID values for each safety function assigned to the current consumer and any teams that the person is linked to. getUserRoles var userRoles = Xrm.page.context.getUserRoles();Assign an array of strings that represents the consumer’s security roles to the userRoles variable. verify whether the script is operating in the Microsoft Dynamics 365 for Outlook customer. customer.getClient var isOutlookClient = (Xrm.page.context.customer.getClient() == “Outlook”);Assign a Boolean cost that represents no matter if your code is running within the Dynamics 365 for Outlook customer to the isOutlookClient variable. investigate even if the user is working offine with the Microsoft Dynamics 365 for Microsoft office Outlook with Offline access client. customer.getClientState var IsOffline = (Xrm.web page.context.customer.getClientState() == “Offline”);Assign a Boolean cost that represents whether the consumer is currently working offline to the IsOfflinevariable. Get the logical identify of the present entity getEntityName var entityName = Xrm.page.statistics.entity.getEntityName();Assign the logical entity name to the entityName variable. Get the price of the fundamental attribute for the current entity.The primary attribute is the value used to identify the listing. as an example contact.fullname. getPrimaryAttributeValue var primaryAttributeValue = Xrm.web page.information.entity.getPrimaryAttributeValue();Assign the price of the basic attribute to the primaryAttributeValue variable.This method is barely attainable for up to date entities. Get the id of the latest list getId var recordId = Xrm.web page.statistics.entity.getId();Assign the id of the checklist to the recordId variable. Asynchronously refresh the facts of the form without reloading the web page. refresh Xrm.page.information.refresh();Refreshes the records in the kind.This components is simply obtainable for up to date entities. save the latest record Xrm.page.information.entity.retailer Xrm.page.records.entity.save();Saves the checklist. There are optional arguments. Use saveandcloseor saveandnew to function the equal movements. keep the existing listing asynchronously with the choice to setcallback services to be executed after the save operation is completed. Xrm.web page.records.store Xrm.web page.facts.store().then(function()Xrm.Utility.alertDialog(“record saved”);,characteristic(error)Xrm.Utility.alertDialog(error.message););Saves the list and shows a message displaying the repute of the shop.This formulation is barely accessible for updated entities. determine no matter if any statistics in the current listing is modified. getIsDirty var isDirty = Xrm.page.information.entity.getIsDirty();Assign a Boolean value that represents no matter if records within the list has modified to the isDirty variable. Get a string that represents the facts that will be sent to the server when the list is saved. getDataXml var dataXml = Xrm.page.information.entity.getDataXml();Assign a string that represents the records to be saved to the dataXmlvariable.
  • Thanks for analyzing the post.

    No comments:

    Post a Comment