/// <summary> /// Returns an output sequence of given type /// </summary> /// <typeparam name="TOutput">The desired type</typeparam> /// <returns>Sequence in which all events are of type TOutput</returns> public IObservable <TOutput> GetObservable <TOutput>() { IObserver <object> o; if (!_outputs.TryGetValue(typeof(TOutput), out o)) { o = new OutputSubject <TOutput>(); _outputs.Add(typeof(TOutput), o); } var output = (IObservable <TOutput>)o; return(output); }
protected override void Execute(CodeActivityContext executionContext) { //Create the context IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); EntityReference emailTemplateSendId = EmailTemplateSendId.Get <EntityReference>(executionContext); Entity emailTemplateSend = service.Retrieve("ndxc_emailtemplatesend", emailTemplateSendId.Id, new ColumnSet("ndxc_accountid", "ndxc_emailtemplateid")); EntityReference accountId = emailTemplateSend.GetAttributeValue <EntityReference>("ndxc_accountid"); EntityReference emailTemplateId = emailTemplateSend.GetAttributeValue <EntityReference>("ndxc_emailtemplateid"); Entity emailTemplate = service.Retrieve("ndxc_emailtemplate", emailTemplateId.Id, new ColumnSet("ndxc_subject", "ndxc_body")); string templateSubject = emailTemplate.GetAttributeValue <string>("ndxc_subject"); string templateBody = emailTemplate.GetAttributeValue <string>("ndxc_body"); // Regular expression to find the tags within the string content var regex = new Regex(@"{(.*?)}"); // Use the regex to grab the tags from the string MatchCollection subjectTags = regex.Matches(templateSubject); MatchCollection bodyTags = regex.Matches(templateBody); MatchCollection allTags = regex.Matches(templateSubject + templateBody); // Create a new column set for each of the viable tables var accountColumnSet = new ColumnSet(false); // For each tag found foreach (Match tag in allTags) { // Get the tag name // By removing the first and last characters // '{' '}' var tagName = tag.Value.Substring(1, tag.Value.Length - 2); // Split tagName by '#' // Provides the entity logical name // And field name var tagComponents = tagName.Split('#'); // switch on the first tag component // which is the logical name of the table // then add the column - found in the second tag component switch (tagComponents[0]) { case "account": accountColumnSet.AddColumn(tagComponents[1]); break; } } // Retrieve the records that have been passed as parameters // Using the columnsets defined Entity account = service.Retrieve("account", accountId.Id, accountColumnSet); // Now we have the data // Loop through the tags again // Retrieve the data for the given column from the record provided // Replace the tag with the value foreach (Match tag in subjectTags) { var tagName = tag.Value.Substring(1, tag.Value.Length - 2); var tagComponents = tagName.Split('#'); switch (tagComponents[0]) { case "account": if (account != null) { var schemaTextValue = returnValueForSchemaName(service, account, tagComponents[1]); templateSubject = templateSubject.Replace(tag.Value, schemaTextValue); } break; } } foreach (Match tag in bodyTags) { var tagName = tag.Value.Substring(1, tag.Value.Length - 2); var tagComponents = tagName.Split('#'); switch (tagComponents[0]) { case "account": if (account != null) { var schemaTextValue = returnValueForSchemaName(service, account, tagComponents[1]); templateBody = templateBody.Replace(tag.Value, schemaTextValue); } break; } } // Set the resultant string as the output for the CWA // As well as the application reference OutputSubject.Set(executionContext, templateSubject); OutputBody.Set(executionContext, templateBody); }