<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">Blog posts by Cecilia von Wachenfeldt</title><link href="http://world.optimizely.com" /><updated>2011-07-13T23:48:48.0000000Z</updated><id>https://world.optimizely.com/blogs/Cecilia-von-Wachenfeldt/</id> <generator uri="http://world.optimizely.com" version="2.0">Optimizely World</generator> <entry><title>An example using Business Foundation in EPiServer Commerce</title><link href="https://world.optimizely.com/blogs/Cecilia-von-Wachenfeldt/Dates/2011/7/An-example-using-Business-Foundation-in-EPiServer-Commerce/" /><id>&lt;p&gt;The current EPiServer Commerce project I am working on has a requirement to allow logged-in customers to register products they have purchased, including those not purchased via the e-commerce site. They should then be able to see all of their registered products on their account page.&lt;/p&gt;  &lt;p&gt;I decided to use the Business Foundation (BF) technology in EPiServer Commerce. You do this by creating a ‘Business Object’ in Commerce Admin, which you can read more about &lt;a href=&quot;http://docs.mediachase.com/display/ECFG52CommerceGuide/Creating+a+New+Contract+Object&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;My business object is called RegisteredProduct and contains two properties: CatalogEntryId and ContactId:&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;&lt;a href=&quot;/link/5b901159e3254d218d0faac092a25bf7.png&quot;&gt;&lt;img style=&quot;background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px&quot; title=&quot;businessobject&quot; border=&quot;0&quot; alt=&quot;businessobject&quot; src=&quot;/link/1ad10d0733ee44ca8acfbd71fe26d1af.png&quot; width=&quot;351&quot; height=&quot;154&quot; /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The CatalogEntryId will hold the id of the product being registered (note that the product has to be in the e-commerce site catalog even if purchased elsewhere) and the ContactId holds the id of the logged-in customer registering the product.&lt;/p&gt;  &lt;p&gt;In the code-behind for the ‘Register Products’ page, I have a method to create an instance of a RegisteredProduct BF object using the BusinessManager class. The properties are set from the values collected in the form and then the object is saved, again using the BusinessManager class:&lt;/p&gt;  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt; &lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; SaveRegisteredProduct()
 {
    &lt;font color=&quot;#0000ff&quot;&gt;var&lt;/font&gt; customerContact = &lt;font color=&quot;#006080&quot;&gt;CustomerContext&lt;/font&gt;.Current.CurrentContact;
    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (customerContact == &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;)
       &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;

     &lt;span class=&quot;rem&quot;&gt;// Creates an instance of your Business Object&lt;/span&gt;
     &lt;font color=&quot;#0000ff&quot;&gt;var&lt;/font&gt; registeredProduct = &lt;font color=&quot;#006080&quot;&gt;BusinessManager&lt;/font&gt;.InitializeEntity(&lt;span class=&quot;str&quot;&gt;&lt;font color=&quot;#804040&quot;&gt;&amp;quot;RegisteredProduct&amp;quot;&lt;/font&gt;&lt;/span&gt;);

     &lt;span class=&quot;rem&quot;&gt;// Set values to properties in your Business Object&lt;/span&gt;
     registeredProduct.Properties&lt;font color=&quot;#804040&quot;&gt;[&lt;/font&gt;&lt;span class=&quot;str&quot;&gt;&lt;font color=&quot;#804040&quot;&gt;&amp;quot;ContactId&amp;quot;&lt;/font&gt;&lt;/span&gt;].Value = 
          customerContact.PrimaryKeyId;
     registeredProduct.Properties[&lt;span class=&quot;str&quot;&gt;&lt;font color=&quot;#804040&quot;&gt;&amp;quot;RegisteredProduct&amp;quot;&lt;/font&gt;&lt;/span&gt;].Value = 
          DropDownListProductModel.SelectedItem.Text;
     registeredProduct.Properties[&lt;span class=&quot;str&quot;&gt;&lt;font color=&quot;#804040&quot;&gt;&amp;quot;CatalogEntryId&amp;quot;&lt;/font&gt;&lt;/span&gt;].Value = 
          &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;.Parse(DropDownListProductModel.SelectedValue);

     &lt;font color=&quot;#006080&quot;&gt;BusinessManager&lt;/font&gt;.Create(registeredProduct);
 }&lt;/code&gt;&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;To present a logged-in customer registered products, the BusinessManager’s List method is used. The method takes the name of the BF object type to query, the property to query on and a value to match against, in this case the logged-in customer’s id. Then for each RegisteredProduct object returned, the product can be read from the commerce catalog using the CatalogEntryId property as the key: &lt;/p&gt;

&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt; &lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; ShowRegisteredProducts()
 {
     &lt;font color=&quot;#0000ff&quot;&gt;var&lt;/font&gt; customer = &lt;font color=&quot;#006080&quot;&gt;CustomerContext&lt;/font&gt;.Current.CurrentContact;
     &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (customer == &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;)
         &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;

     &lt;font color=&quot;#0000ff&quot;&gt;var&lt;/font&gt; registeredProducts = &lt;font color=&quot;#006080&quot;&gt;BusinessManager&lt;/font&gt;.List
        (&lt;span class=&quot;str&quot;&gt;&lt;font color=&quot;#804040&quot;&gt;&amp;quot;RegisteredProduct&amp;quot;&lt;/font&gt;&lt;/span&gt;, &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt;[] { &lt;font color=&quot;#006080&quot;&gt;FilterElement&lt;/font&gt;.EqualElement
        (&lt;span class=&quot;str&quot;&gt;&lt;font color=&quot;#804040&quot;&gt;&amp;quot;ContactId&amp;quot;&lt;/font&gt;&lt;/span&gt;, customer.PrimaryKeyId) }).ToList();

     &lt;font color=&quot;#0000ff&quot;&gt;var&lt;/font&gt; entries = registeredProducts.Select
     (registeredProduct =&amp;gt; &lt;font color=&quot;#006080&quot;&gt;CatalogContext&lt;/font&gt;.Current.GetCatalogEntry
     ((&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;) registeredProduct.Properties&lt;font color=&quot;#804040&quot;&gt;[&lt;/font&gt;&lt;span class=&quot;str&quot;&gt;&lt;font color=&quot;#804040&quot;&gt;&amp;quot;CatalogEntryId&amp;quot;&lt;/font&gt;&lt;/span&gt;].Value)).ToList();

      RepeaterRegisteredProducts.DataSource = entries;
      RepeaterRegisteredProducts.DataBind();
 }&lt;/code&gt;&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;As I am new to EPiServer Commerce I don’t know if this is the best way to solve the problem. If anyone has an alternative solution then I welcome any feedback you have.&lt;/p&gt;</id><updated>2011-07-13T23:48:48.0000000Z</updated><summary type="html">Blog post</summary></entry> <entry><title>Implementing a simple email queue in EPiServer Commerce</title><link href="https://world.optimizely.com/blogs/Cecilia-von-Wachenfeldt/Dates/2011/7/Implementing-a-simple-email-queue-in-EPiServer-Commerce/" /><id>&lt;p&gt;Recently I’ve been working with a Commerce project where when a customer has placed an order we wanted to send a confirmation email. In the EPiServer Commerce sample site the code to send the confirmation email is in the code behind of the cart checkout control. This isn’t a good practice as if something goes wrong with the email sending, for example, if the SMTP configuration is incorrect, then the user will see an error saying something went wrong with their order, which in this case is not true.&lt;/p&gt;  &lt;p&gt;What we decided to do was to queue up an email request using the EPiServer Dynamic Data Store and then use an EPiServer Scheduled Job to service the email queue and send out the emails.&lt;/p&gt;  &lt;p&gt;First of all we created a simple class with the information needed to send the email:&lt;/p&gt;  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; System;
&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; EPiServer.Data.Dynamic;

&lt;span class=&quot;kwrd&quot;&gt;namespace&lt;/span&gt; Project.Foundation.CMS.Helpers
{   
    [&lt;font color=&quot;#006080&quot;&gt;EPiServerDataStore&lt;/font&gt;(AutomaticallyCreateStore = &lt;span class=&quot;kwrd&quot;&gt;true&lt;/span&gt;)]
    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; &lt;font color=&quot;#006080&quot;&gt;OrderEmailToBeSent&lt;/font&gt;
    {
        &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; OrderId { get; set; }
        &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;font color=&quot;#006080&quot;&gt;Guid&lt;/font&gt; Id { get; set; }
    }
}&lt;/code&gt;&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;




.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;Then when an order is placed we created an instance of the above class, set it’s properties and saved it in the DDS:&lt;/p&gt;

&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; SaveOrderId(&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; orderId)
{
     var store = &lt;span class=&quot;kwrd&quot;&gt;typeof&lt;/span&gt;(&lt;font color=&quot;#006080&quot;&gt;OrderEmailToBeSent&lt;/font&gt;).GetStore();
     var orderEmailToBeSent = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; &lt;font color=&quot;#006080&quot;&gt;OrderEmailToBeSent&lt;/font&gt; { OrderId = orderId };
     store.Save(orderEmailToBeSent);
}&lt;/code&gt;&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;




.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;The Scheduled Job class gets the items from the Dynamic Data Store, loads the order from the Commerce system, sends an email to the customer, and then deletes the item from the DDS:&lt;/p&gt;

&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;[&lt;font color=&quot;#006080&quot;&gt;ScheduledPlugIn&lt;/font&gt;(DisplayName = &lt;span class=&quot;str&quot;&gt;&lt;font color=&quot;#800000&quot;&gt;&amp;quot;Send order emails&amp;quot;&lt;/font&gt;&lt;/span&gt;)]
 &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; &lt;font color=&quot;#006080&quot;&gt;SendOrderEmails&lt;/font&gt;
 {
     &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; Execute()
     {
         var store = &lt;span class=&quot;kwrd&quot;&gt;typeof&lt;/span&gt;(&lt;font color=&quot;#006080&quot;&gt;OrderEmailToBeSent&lt;/font&gt;).GetStore();
         &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; skip = 0;
         &lt;span class=&quot;kwrd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; count = 50;
         &lt;span class=&quot;kwrd&quot;&gt;while&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;true&lt;/span&gt;)
         {
             var items = store.Items&amp;lt;&lt;font color=&quot;#006080&quot;&gt;OrderEmailToBeSent&lt;/font&gt;&amp;gt;().Skip(skip).Take(count);

             &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (items.Count() == 0)
                 &lt;span class=&quot;kwrd&quot;&gt;break&lt;/span&gt;;

             &lt;span class=&quot;kwrd&quot;&gt;foreach&lt;/span&gt; (var orderEmailToBeSent &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; items)
             {
                 var order = &lt;font color=&quot;#006080&quot;&gt;CartHelper&lt;/font&gt;.FindOrder(orderEmailToBeSent.OrderId);

                 SendEmail(order, &lt;span class=&quot;str&quot;&gt;&lt;font color=&quot;#800000&quot;&gt;&amp;quot;order-purchaseorder-notify&amp;quot;&lt;/font&gt;&lt;/span&gt;);
                 store.Delete(orderEmailToBeSent);
             }

             skip += count;
      }
         &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;str&quot;&gt;&lt;font color=&quot;#800000&quot;&gt;&amp;quot;Success!&amp;quot;&lt;/font&gt;&lt;/span&gt;;
     }
 }&lt;/code&gt;&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;




.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;And the code for sending the email:&lt;/p&gt;

&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; SendEmail(&lt;font color=&quot;#006080&quot;&gt;PurchaseOrder&lt;/font&gt; order, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; template)
{
      &lt;span class=&quot;rem&quot;&gt;// Add input parameter&lt;/span&gt;
      var dic = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; &lt;font color=&quot;#006080&quot;&gt;Dictionary&lt;/font&gt;&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;, &lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt;&amp;gt;();
      dic.Add(&lt;span class=&quot;str&quot;&gt;&amp;quot;OrderGroup&amp;quot;&lt;/span&gt;, order);

      &lt;span class=&quot;rem&quot;&gt;// Execute template processor&lt;/span&gt;
      var body = &lt;font color=&quot;#006080&quot;&gt;TemplateService&lt;/font&gt;.Process(template,                  &lt;br /&gt;                       &lt;font color=&quot;#006080&quot;&gt;Thread&lt;/font&gt;.CurrentThread.CurrentCulture, dic);

      &lt;span class=&quot;rem&quot;&gt;// Send out emails&lt;/span&gt;
      var msg = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; &lt;font color=&quot;#006080&quot;&gt;MailMessage&lt;/font&gt;();
      msg.From = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; &lt;font color=&quot;#006080&quot;&gt;MailAddress&lt;/font&gt;(&lt;span class=&quot;str&quot;&gt;&lt;font color=&quot;#800000&quot;&gt;&amp;quot;store@yourcompany.com&amp;quot;&lt;/font&gt;&lt;/span&gt;);
      msg.To.Add(&lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; &lt;font color=&quot;#006080&quot;&gt;MailAddress&lt;/font&gt;(&lt;span class=&quot;str&quot;&gt;&lt;font color=&quot;#800000&quot;&gt;&amp;quot;customer@customer.com&amp;quot;&lt;/font&gt;&lt;/span&gt;, &lt;span class=&quot;str&quot;&gt;&lt;font color=&quot;#800000&quot;&gt;&amp;quot; - &amp;quot;&lt;/font&gt;&lt;/span&gt; + order.Name));
      msg.Subject = &lt;span class=&quot;str&quot;&gt;&lt;font color=&quot;#800000&quot;&gt;&amp;quot;Subject&amp;quot;&lt;/font&gt;&lt;/span&gt;;
      msg.Body = body;
      msg.IsBodyHtml = &lt;span class=&quot;kwrd&quot;&gt;true&lt;/span&gt;;

      var client = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; &lt;font color=&quot;#006080&quot;&gt;SmtpClient&lt;/font&gt;();
      client.Send(msg);
 }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The TemplateService class used above takes an XSL template and transforms it into an HTML email. See &lt;a href=&quot;http://docs.mediachase.com/display/ecf52devguide/Email+and+Notification+Capabilities&quot; target=&quot;_blank&quot;&gt;this article&lt;/a&gt; for more details about this.&lt;/p&gt;</id><updated>2011-07-01T00:19:14.0000000Z</updated><summary type="html">Blog post</summary></entry></feed>