I have moved!
I've taken up the blogging effort again, and moved to a new place. The old blog posts have moved with me to torsteinutne.com
See ya!
A Norwegian blog on everything about MS CRM
[CrmWorkflowActivity("Query Activity")]
public class WorkflowQuery: Activity
{
public static DependencyProperty entitynameProperty = DependencyProperty.Register("entityname", typeof(string), typeof(WorkflowQuery));
[CrmInput("Entity Name")]
[CrmDefault("account")]
public string entityname
{
get {return base.GetValue(entitynameProperty).ToString(); }
set { base.SetValue(entitynameProperty, value); }
}
public static DependencyProperty attributenameProperty = DependencyProperty.Register("attributename", typeof(string), typeof(WorkflowQuery));
[CrmInput("Attribute To Retrieve")]
[CrmDefault("statuscode")]
public string attributename
{
get { return base.GetValue(attributenameProperty).ToString(); }
set { base.SetValue(attributenameProperty, value); }
}
public static DependencyProperty conditionattributenameProperty = DependencyProperty.Register("conditionattributename", typeof(string), typeof(WorkflowQuery));
[CrmInput("Condition attribute")]
[CrmDefault("name")]
public string conditionattributename
{
get { return base.GetValue(conditionattributenameProperty).ToString(); }
set { base.SetValue(conditionattributenameProperty, value); }
}
public static DependencyProperty conditionvalueProperty = DependencyProperty.Register("conditionvalue", typeof(string), typeof(WorkflowQuery));
[CrmInput("Condition value")]
[CrmDefault("Example account name")]
public string conditionvalue
{
get { return base.GetValue(conditionvalueProperty).ToString(); }
set { base.SetValue(conditionvalueProperty, value); }
}
public static DependencyProperty resultProperty = DependencyProperty.Register("result", typeof(string), typeof(WorkflowQuery));
[CrmOutput("Result")]
[CrmDefault("")]
public string result
{
get { return base.GetValue(resultProperty).ToString(); }
set { base.SetValue(resultProperty, value); }
}
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
IWorkflowContext context = contextService.Context;
ICrmService crmService = context.CreateCrmService();
QueryExpression query = new QueryExpression();
query.EntityName = this.entityname;
ColumnSet columns = new ColumnSet();
columns.Attributes.Add(this.attributename);
query.ColumnSet = columns;
query.Criteria = new FilterExpression();
query.Criteria.FilterOperator = LogicalOperator.And;
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = this.conditionattributename;
condition1.Operator = ConditionOperator.Equal;
condition1.Values = new object[] { this.conditionvalue};
query.Criteria.Conditions.Add(condition1);
RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();
retrieve.Query = query;
retrieve.ReturnDynamicEntities = true;
RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse)crmService.Execute(retrieve);
DynamicEntity entity = (DynamicEntity)retrieved.BusinessEntityCollection.BusinessEntities[0];
if (entity.Properties[this.attributename] is Status)
{
Status prop = (Status)entity.Properties[this.attributename];
this.result = prop.name.ToString();
}
else if (entity.Properties[this.attributename] is CrmNumber)
{
CrmNumber prop = (CrmNumber)entity.Properties[this.attributename];
this.result = prop.Value.ToString();
}
else if (entity.Properties[this.attributename] is CrmDateTime)
{
CrmDateTime prop = (CrmDateTime)entity.Properties[this.attributename];
this.result = prop.Value;
}
else if (entity.Properties[this.attributename] is CrmMoney)
{
CrmMoney prop = (CrmMoney)entity.Properties[this.attributename];
this.result = prop.Value.ToString();
}
else if (entity.Properties[this.attributename] is Key)
{
Key prop = (Key)entity.Properties[this.attributename];
this.result = prop.Value.ToString();
}
else if (entity.Properties[this.attributename] is Picklist)
{
Picklist prop = (Picklist)entity.Properties[this.attributename];
this.result = prop.name;
}
else if (entity.Properties[this.attributename] is Owner)
{
Owner prop = (Owner)entity.Properties[this.attributename];
this.result = prop.Value.ToString();
}
else //stringproperty
{
String prop = (String)entity.Properties[this.attributename];
this.result = prop;
}
return ActivityExecutionStatus.Closed;
}
}
[CrmWorkflowActivity("Account maintenance")] |
[CrmWorkflowActivity("Rating")]
public class Rating : SequenceActivity
{
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
IWorkflowContext context = contextService.Context;
ICrmService crmService = context.CreateCrmService();
//Possible ratings
char[] ratings = new char[] { 'A', 'B', 'C' };
//Combinations:
string rating = this.CriteriaOne.ToUpper () + this.CriteriaOne.ToUpper () + this.CriteriaOne.ToUpper ();
//A equals 1, B equals 2 and C equals 3. Max 9, min 3. 3-5 equals a total A, 6-7 equals B 8-9 equals C
//Alternately a single A criteria can generate a total rating of A if this is specified
int i = 0;
bool A = false;
foreach (char c in rating)
{
if (c == 'A')
{
i = i + 1;
A = true;
}
if (c == 'B')
{
i = i + 2;
}
if (c == 'C')
{
i = i + 3;
}
}
if(i<6) totalrating = "A"> 5) && (i< totalrating = "B"> 7)
{
this.TotalRating = "C";
}
if (this.ARating.Value)
{
if (A)
{
this.TotalRating = "A";
}
}
return ActivityExecutionStatus.Closed;
}
public static DependencyProperty CriteriaOneProperty = DependencyProperty.Register ("CriteriaOne", typeof (System.String), typeof (Rating));
[CrmInput("Criteria One")]
public string CriteriaOne
{
get
{
return base.GetValue (CriteriaOneProperty).ToString ();
}
set
{
base.SetValue (CriteriaOneProperty, value);
}
}
public static DependencyProperty CriteriaTwoProperty = DependencyProperty.Register ("CriteriaTwo", typeof (System.String), typeof (Rating));
[CrmInput ("Criteria Two")]
public string CriteriaTwo
{
get
{
return base.GetValue (CriteriaTwoProperty).ToString ();
}
set
{
base.SetValue (CriteriaTwoProperty, value);
}
}
public static DependencyProperty CriteriaThreeProperty = DependencyProperty.Register ("CriteriaThree", typeof (System.String), typeof (Rating));
[CrmInput ("Criteria Three")]
public string CriteriaThree
{
get
{
return base.GetValue (CriteriaThreeProperty).ToString ();
}
set
{
base.SetValue (CriteriaThreeProperty, value);
}
}
public static DependencyProperty ARatingProperty = DependencyProperty.Register ("ARating", typeof (CrmBoolean), typeof (Rating));
[CrmInput ("Return always A if one criteria is A)]
public CrmBoolean ARating
{
get
{
return (CrmBoolean)base.GetValue (ARatingProperty);
}
set
{
base.SetValue (ARatingProperty, value);
}
}
public static DependencyProperty TotalRatingProperty = DependencyProperty.Register ("TotalRating", typeof (System.String), typeof (Rating));
[CrmOutput ("Total Rating")]
public string TotalRating
{
get
{
return this.GetValue (TotalRatingProperty).ToString ();
}
set
{
this.SetValue (TotalRatingProperty, value);
}
}
}
[CrmWorkflowActivity("Write E-mail address To Batch File" )]
[PersistOnClose]
public class WriteEmailAddressToBatchFile : Activity
{
public static DependencyProperty ContactProperty = DependencyProperty.Register("Contact", typeof(Lookup), typeof(WriteEmailAddressToBatchFile));
public static DependencyProperty FilepathProperty = DependencyProperty.Register("Filepath", typeof(string), typeof(WriteEmailAddressToBatchFile));
//Define the properties
[CrmInput("Recipient")]
[ValidationOption(ValidationOption.Required)]
[CrmReferenceTarget("contact")]
public Lookup Contact
{
get
{
return (Lookup)base.GetValue(ContactProperty);
}
set
{
//Validate the argument
if (value == null || (value.IsNullSpecified && value.IsNull))
{
throw new ArgumentNullException("Contact Lookup cannot be null or have IsNullSpecified = true", "");
}
else if (value.type != null && value.type != "contact")
{
throw new ArgumentNullException("Contact Lookup must be a contact entity", "");
}
else if (value.Value == Guid.Empty)
{
throw new ArgumentException("Contact Lookup must contain a valid Guid", "");
}
base.SetValue(ContactProperty, value);
}
}
public string Filepath
{
get
{
return base.GetValue(FilepathProperty).ToString();
}
set
{
base.SetValue(FilepathProperty, value);
}
}
The last thing we need to do is to add methods to handle xml file loading and saving. If the xml file doesn't exists it will be created.
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
// Get the context service.
IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
IWorkflowContext context = contextService.Context;
// Use the context service to create an instance of CrmService.
ICrmService crmService = context.CreateCrmService(true);
try
{
XmlDocument xmldoc = WriteToFile.LoadFile(this.Filepath, FileType.PrintFile);
// Create a new XML-element
XmlElement newemail = xmldoc.CreateElement("Email");
// Attribute for ID
XmlAttribute emailid = xmldoc.CreateAttribute("ID");
// Generates a new Guid
emailid.Value = Guid.NewGuid().ToString();
//attach the attribute to the parent element
newemail.SetAttributeNode(emailid);
// New subelements
XmlElement emailaddress = xmldoc.CreateElement("EmailAddress");
XmlElement date = xmldoc.CreateElement("Date");
// Assigns values to the subelements
string _contactid = "";
if (this.Contact != null)
{
_contactid = this.Contact.Value.ToString();
}
else
{
_contactid = "00000000-0000-0000-0000-000000000000";
}
//retrieves values based on GUID
BusinessEntityCollection bec = new BusinessEntityCollection();
QueryExpression query = new QueryExpression();
query.EntityName = "contact";
ColumnSet columns = new ColumnSet();
columns.Attributes.Add("emailaddress1");
query.ColumnSet = columns;
query.Criteria = new FilterExpression();
query.Criteria.FilterOperator = LogicalOperator.And;
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = "contactid";
condition1.Operator = ConditionOperator.Equal;
condition1.Values = new object[] { _contactid };
query.Criteria.Conditions.Add(condition1);
bec = crmService.RetrieveMultiple(query);
contact con = new contact();
con.emailaddress1 = "";
if (bec.BusinessEntities.Count > 0)
{
con = (contact)bec.BusinessEntities[0];
}
emailaddress.InnerText = con.emailaddress1;
con = null;
date.InnerText = System.DateTime.Now.Date.ToLongDateString();
// attach the child elements to the parent
newemail.AppendChild(emailaddress);
newemail.AppendChild(date);
// Adds the new element to the end of the xml file
xmldoc.DocumentElement.InsertAfter(newemail, xmldoc.DocumentElement.LastChild);
//Saves the file
WriteToFile.SaveFile(xmldoc, this.Filepath);
xmldoc = null;
}
catch (Exception ex)
{
//Handle errors here;
}
return base.Execute(executionContext);
}
}
public class WriteToFile
{
public static XmlDocument LoadFile(string path)
{
XmlDocument xmldoc = new XmlDocument();
try
{
if (!File.Exists(path))//if the file doesnt exists we'll create it at the specified location.
{
xmldoc = CreateXmlFile(path);
}
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
xmldoc.Load(fs);
}
}
catch (Exception ex)
{
//Handle errors here;
}
return xmldoc;
}
private static XmlDocument CreateXmlFile(string path)
{
XmlDocument xmldoc = new XmlDocument();
try
{
//let's add the XML declaration section
XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xmldoc.AppendChild(xmlnode);
//let's add the root element
string rootElementName = "EmailRecipient";
XmlElement xmlelem = xmldoc.CreateElement("", rootElementName, "");
xmldoc.AppendChild(xmlelem);
xmldoc.Save(path);
System.Diagnostics.EventLog appLog = new System.Diagnostics.EventLog();
appLog.Source = "MS CRM Custom Workflow Activity";
System.Text.StringBuilder sb = new StringBuilder();
sb.AppendLine("Message: A new Batch File was created");
appLog.WriteEntry(sb.ToString(), System.Diagnostics.EventLogEntryType.Information);
}
catch (Exception ex)
{
//Handle errors here;
}
return xmldoc;
}
public static void SaveFile(XmlDocument xmldoc, string path)
{
try
{
using (FileStream fsxml = new FileStream(path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite))
{
xmldoc.Save(fsxml);
}
}
catch (Exception ex)
{
//Handle errors here;
}
}
}