Sitecore ContentSearch Get Latest Version

The Sitecore ContentSearch API allows you to index content in either .NET Lucene or SOLR, dramatically speeding up retrieval of content, especially when querying items that are scattered across your content tree.

Content retrieved from the ContentSearch API is not Sitecore Content Items (of type Sitecore.Data.Items.Item), they are objects that you have defined yourself. This is why you will experience that when querying from the MASTER database index (SITECORE_MASTER_INDEX), you will receive one result per version (and one result per language) rather than one Item object containing all versions and languages.

To overcome this issue, Sitecore have added a few nifty fields to the index, for example the _latestversion field:

Latest Version Field from SOLR

Latest Version Field found in my SOLR index

So when querying the index, you can add the _latestversion field:

query = query.Where(item => item["_latestversion"].Equals("1")); 

BTW, _latestversion is defined in a constant value in Sitecore:

Sitecore.ContentSearch.BuiltinFields.LatestVersion;

MORE TO READ: 

Posted in .net, c#, General .NET, Sitecore 7, Sitecore 8 | Tagged , , , , , | 1 Comment

Sitecore Caching – Clear caches individually

The Sitecore caching engine is a simple yet powerful tool where Sitecore not only stores it’s own data for fast retrieval, but allows you to store your own data of any kind. Sitecore have a built in overview of the contents of the caches, but the overview lacks the possibility to clear caches individually.
The cache admin is located on the /sitecore/admin/cache.aspx URL, and as you can see, you can only clear all caches:

Sitecore Cache Overview

Sitecore Cache Overview

Clearing one cache at a time is very easy:

Sitecore.Caching.Cache cache = Sitecore.Caching.CacheManager.FindCacheByName("name of cache");
cache.Clear();

So it is not that hard to create your own cache admin page that can clear each cache individually:

Your own Cache Overview Page

Your own Cache Overview Page

This is a simple .aspx page that lists all caches, displays some nice cache stats, and sports a nice “Clear Cache” button that clears that specific cache. The code is simple:

<%@ Page language="c#" EnableEventValidation="false" AutoEventWireup="true" EnableViewState="false" %>
<%@ Import Namespace="System.Security.Permissions" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Security.Principal" %>

<script runat="server">
  void Page_Load(object sender, System.EventArgs e)
  {
    Response.Buffer = false;
    Response.BufferOutput = false;
    DataBind();
  }

  IEnumerable<Sitecore.Caching.Cache> Caches
  {
    get
    {
      return Sitecore.Caching.CacheManager.GetAllCaches().OrderBy(c => c.Name);
    }
  }

  double PercentageUsed(Sitecore.Caching.Cache cache)
  {
    if (cache.MaxSize == 0)
      return 0;
    return Math.Round(((double)cache.Size/(double)cache.MaxSize*100), 0);
  }

  string PercentageColor(double percentage)
  {
    if (percentage >= 0 && percentage <= 50)
      return "green";
    if (percentage >= 50 && percentage <= 75)
      return "orange";
    return "red";
  }

  private void repCaches_Command(object source, RepeaterCommandEventArgs e)
  {
    switch (e.CommandName)
    {
      case "ClearCache":
        string cacheName = e.CommandArgument as string;
        Sitecore.Caching.Cache cache = Sitecore.Caching.CacheManager.FindCacheByName(cacheName);
        cache.Clear();
        repCaches.DataBind();
        break;
    }
  }

  long TotalMaxSize()
  {
    long ac = 0;
    foreach (var cache in Caches)
      ac = ac + cache.MaxSize;
    return ac;
  }

  long TotalSize()
  {
    long ac = 0;
    foreach (var cache in Caches)
      ac += cache.Size;
    return ac;
  }

  double TotalPercentageUsed()
  {
    if (TotalMaxSize() == 0)
      return 0;
    return Math.Round(((double)TotalSize()/(double)TotalMaxSize()*100), 1);
  }
</script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
  <head>
    <title>Cache Plus</title>
    <style type="text/css">
      body {
        font: normal 12pt arial, verdana;
        padding:20px;
      }

      .box {
        padding:20px;
        margin:20px;
        border: solid 1px black;
        background-color:#efefef;
      }

      input {
        height: 30px;
        width: 100px;
      }

      td {
        border-bottom: solid 1px #aaa;
        padding-right: 20px;
        padding-left: 5px;
        padding-top: 5px;
        padding-bottom: 5px;
      }

      table {
        width:100%;
      }

      thead td {
        font-weight: bold;
        border-bottom: solid 1px #aaa;
        padding-right: 20px;
      }
    </style>
  </head>
  <body style="font-size:14px">
    <form runat="server">
      <div style="padding:20px; background-color:#eaeaea; border-bottom: solid 1px #777777; font-size:16px">
        <%# Sitecore.StringUtil.GetSizeString(TotalSize()) %> of <%# Sitecore.StringUtil.GetSizeString(TotalMaxSize()) %> used <strong>(<%# TotalPercentageUsed() %>%)</strong>
      </div>
      <asp:Repeater ID="repCaches" runat="server" EnableViewState="false" OnItemCommand="repCaches_Command" DataSource="<%# Caches %>">
        <HeaderTemplate>
          <table>
            <thead>
            <tr>
              <td>Name</td>
              <td></td>
              <td>Size</td>
              <td>MaxSize</td>
              <td>% Used</td>
            </tr>
            </thead>
        </HeaderTemplate>
        <FooterTemplate>
          </table>
        </FooterTemplate>
        <ItemTemplate>
          <tr>
            <td style="width:250px">
              <%# (Container.DataItem as Sitecore.Caching.Cache).Name %>
            </td>
            <td style="width:100px">
              <asp:Button ID="btnClearCache" runat="server" CommandArgument="<%# (Container.DataItem as Sitecore.Caching.Cache).Name %>" CommandName="ClearCache" text="Clear Cache"/>
            </td>
            <td style="text-align: right; width:80px">
              <%# Sitecore.StringUtil.GetSizeString((Container.DataItem as Sitecore.Caching.Cache).Size) %>
            </td>
            <td style="text-align: right; width:80px">
              <%# Sitecore.StringUtil.GetSizeString((Container.DataItem as Sitecore.Caching.Cache).MaxSize) %>
            </td>
            <td>
              <div style="width:<%# PercentageUsed((Container.DataItem as Sitecore.Caching.Cache)) %>%; height:30px; background-color:<%# PercentageColor(PercentageUsed((Container.DataItem as Sitecore.Caching.Cache))) %>; float:left;"></div>
              <div style="width:<%# (100 - PercentageUsed((Container.DataItem as Sitecore.Caching.Cache))) %>%; height:30px; background-color:#ccffcc; float:left;"></div>
            </td>
          </tr>
        </ItemTemplate>
      </asp:Repeater>
    </form>
  </body>
</html>

Copy the code to a file named .aspx, and put the file in /sitecore modules/shell.

MORE TO READ:

Thanks to Richard Hauuer @richardhauer for the inspiration to write this article.

Posted in .net, c#, General .NET, Sitecore 7, Sitecore 8 | Tagged , | 2 Comments

Sitecore and Feature Flags using LaunchDarkly

Feature flags are a software development best practice of gating functionality. Functionality can be deployed “off”, then turned on via the feature flag, separate from deployment. With feature flags, you can manage the entire lifecycle of a feature.

  • Launchdarkly.com

In essence, a feature flag is a if..then..else statement, where the outcome of the statement is controlled by feeding a flag into a feature flag system:

if (GetFeatureFlag("feature-flag") == true)
{
  // enable my feature
}
else
{
  // disable my feature
}

Feature flags are user specific and can be used cross platform, and is therefore a great way to release new features:

  • At the same time on all platforms
  • To 10% of all users
  • To all users from a specific segment
  • To website only

Feature flags should not be confused with Sitecore Personalization, although they both share similarities. The purpose of Personalization is to tailor the user experience to the end users needs, whilst feature flags (or feature toggling) is enabling or disabling features based on rules separate from deployment or code.

A feature flag is often short-lived, and the flag is removed when the gated feature is considered stable.

 

ABOUT THE FEATURE FLAG PLATFORM

I have lately worked with a feature flag platform called “Launchdarkly“. They offer API’s for every possible programming language and for every possible platform. Their code is blazingly fast, and you do not notice any performance loss when checking for a flag.

Feature Flags Overview

Feature Flags Overview

In Launchdarkly, you can enable features by flipping a switch, or by segmenting on user data:

Feature Flags Targeting

Feature Flags Targeting

 

IMPLEMENTING LAUNCHDARKLY IN SITECORE

In order to check for a feature flag, we need to send user data and a feature flag name to Launchdarkly. The Launchdarkly API uses a Launchdarkly.Client.User and you can fill any property from your Sitecore User to the Launchdarkly Client User.

This is a simple example where I create a Launchdarkly user containing a unique ID, the email address and a name:

using System;
using LaunchDarkly.Client;

namespace MyNamespace
{
  static internal class LaunchDarklyFactory
  {
    static internal User CreateUser()
    {
      var user = Sitecore.Context.User;
      var key = user.IsAuthenticated ? user.Name : Guid.NewGuid().ToString();
      var ldUser = User.WithKey(key).AndAnonymous(!user.IsAuthenticated).AndEmail(user.Profile.Email).AndFirstName(user.Profile.FullName).AndLastName("."); //LaunchDarkly won't update display name unless both first and last is set.
      return ldUser;
    }
  }
}

With the user in hand it is easy to create a feature flag repository:

using System;
using LaunchDarkly.Client;
using Sitecore.Configuration;
using Sitecore.Diagnostics;

namespace MyNamespace
{
  public static class LaunchDarklyRepository
  {
    // My Launchdarkly key is stored in a setting in a config file
    private static readonly LdClient _client = new LdClient(Settings.GetSetting("LaunchDarkly.Client"));

    public static bool GetFeatureFlag(string flag, bool defaultValue)
    {
      try
      {
        var ldUser = LaunchDarklyFactory.CreateUser();
        return _client.BoolVariation(flag, ldUser, defaultValue);
      }
      catch (Exception ex)
      {
        Log.Error(typeof (LaunchDarklyRepository) + ".GetFeatureFlag() failed to get feature flag '" + flag + "', returning default value " + defaultValue + ". Message:" + ex.Message, typeof (LaunchDarklyRepository));
        return defaultValue;
      }
    }
  }
}

The repository can now be used to enable or disable features:

if (LaunchDarklyRepository.GetFeatureFlag("my-feature-flag", false))
{
  // feature enabled
}
else
{  
  // feature disabled
}

 

INTEGRATING LAUNCHDARKLY INTO THE EXPERIENCE EDITOR

Experience Editor

Experience Editor

With the new feature flag framework in place, it could be nice with a Sitecore Rule. This rule will allow me to enable and disable components on my website by flipping a feature flag switch. The rule itself is very simple:

using System;
using Sitecore.Diagnostics;
using Sitecore.Rules;
using Sitecore.Rules.Conditions;

namespace MyNamespace
{
  public class FeatureFlagHasBoolVariation<T> : WhenCondition<T> where T : RuleContext
  {
    public string FeatureFlag
    {
      get;
      set;
    }

    public bool BoolVariation
    {
      get;
      set;
    }

    protected override bool Execute(T ruleContext)
    {
      try
      {
        LaunchDarklyUser user = LaunchDarklyUserFactory.GetLaunchDarklyUser();
        var boolVariation = LaunchDarklyRepository.GetFeatureFlag(FeatureFlag, false);
        return boolVariation == BoolVariation;
      }
      catch (Exception ex)
      {
        Log.Error(string.Format("{0}: {1} ", GetType(), ex.Message), ex, this);
        return false;
      }
    }
  }
}

To enable the rule in Sitecore, add an item under /sitecore/system/Settings/Rules/Definitions/Elements/…

Feature Flag Personalisation Rule

Feature Flag Personalization Rule

The rule can now applied to a component:

Rule Set Editor

Rule Set Editor

And the component can be visible if the flag is set:

Personalize Content

Personalize Content

MORE TO READ

Posted in c#, General .NET, Sitecore, Sitecore 8 | Tagged , , , , | Leave a comment

Sitecore how to configure SSL (HTTPS) in your Sitecore config files

SSL and HTTPS have been the hot topic since Google began to use HTTPS as a signal in their search rankings.

Fortunately, Sitecore really does not care if it runs in HTTP or HTTPS, so most of the configurations are purely .NET related. Here is what you do if you have a single domain Sitecore installation, or a Sitecore installation that shares the root domain (en.mydomain.com/de.mycomain.com/…).

STEP 1: OBTAIN A SSL CERTIFICATE AND INSTALL IT ON YOUR SERVER

This usually involves a hosting company. If you are lucky enough (as I am) to know a hosting company that takes responsibility and provide a great service, this requires sending a mail and wait for the answer. If not you can follow one of the many guides online on how to obtain and install the SSL certificate.

STEP 2: MODIFY FORMS AUTHENTICATION

In web.config, add your domain to the forms authentication. This is pure .NET, nothing to do with Sitecore. Replace the MYCOMAIN.COM with your own domain.

<authentication mode="None">
  <forms domain=".MYDOMAIN.COM" timeout="43200" slidingExpiration="true" name=".ASPXAUTH" cookieless="UseCookies" />
</authentication>

STEP 3: MODIFY HTTPCOOKIES

In web.config, add your domain and set requireSSL in the httpCookies property. Again, this is .NET, not Sitecore specific. This binds any cookies to the root domain we specify, and secures the cookie (no cookies are sent to the server unless the connection is secure (HTTPS)).

<httpCookies httpOnlyCookies="true" requireSSL="true" lockItem="true" domain=".YOURDOMAIN.COM" />

STEP 4: CHANGE THE SCHEME PROPERTY IN THE SITES SECTION

In Sitecore.config, set the scheme=”https” in your sites section. This is used by Sitecore when URL’s are generated, and ensures that when a fully qualified URL is generated, the URL is prefixed with https://

<site name="website" 
      scheme="https"
      hostName="YOURDOMAIN.COM" 
      targetHostName="YOURDOMAIN.COM" 
      ... ... 
/>

STEP 5: ALLOW YOUR IIS OR LOAD BALANCER TO REDIRECT ALL REQUESTS FROM HTTP TO HTTPS

The site as configured now will still serve pages using the HTTP protocol. But any cookies will not be persisted unless we use the proper domain and proper scheme. So you need to redirect any HTTP request to HTTPS. This is always a fun coding project, but can also be achieved by configuring the load balancer, or using the IIS URL Rewrite Module.

MORE INFO

 

Posted in Sitecore 6, Sitecore 7, Sitecore 8 | Tagged , , | 1 Comment

Sitecore Hiding items: Clean up the cluttered content tree

As a followup to a question from Derek Dysart on my previous post, How to make the experience editor a great experience for your editors I will explain how you can clean up the Sitecore content tree by hiding system items.

A common system item is the “components” (or whatever you call it) folder containing the data sources for components belonging to a certain content item. This folder is usually placed below your content item. The folder have no presentation, no URL, and does only exist because we like to build pages from many data sources in order to be able to personalize every component of a page.

Components Folder

A Components folder below the content item

This is not something the editors should see. Their tree should only contain the actual contents of the website.

A tree containing content only

A tree containing content only

All you have to do to is to select the “components” folder on your master, go to the “Configure” tab and select “Hide item“:

Hide Item

Hide Item

When the item is hidden, the button text changes:

Hidden Item

Hidden Item

Hiding items does only mean that the item is gone from the content tree. The item is still fully available from Sitecore, and also from the Experience Editor:

Hidden folder visible in the Experience Editor

Hidden folder visible in the Experience Editor

Also, as an administrator you can still see the item, provided you check the “Hidden Items” on the “View” tab:

View Hidden Items Enabled

View Hidden Items Enabled

MORE TO READ:

Posted in Sitecore 6, Sitecore 7, Sitecore 8 | Tagged , , , | 2 Comments

Sitecore how to make the experience editor a great experience for your editors

In Sitecore it is very easy to toss in a few renderings on a page, set the renderings flag to “editable”, and voila, the page is editable using the Sitecore Experience Editor.
But that kind of laziness often ends up being a pain for the people using the Experience Editor. Just image that it is you who gets this screen when selecting a rendering:

Select a Rendering - the bad way

Select a Rendering – hmm what to choose?

Wouldn’t you prefer a screen like this:

Select a rendering - the better way

Select a rendering – with images and translated text

Stuff like this takes more time that knowledge to do. So here is a quick guide on how to set up Sitecore so your renderings are presented properly in the experience editor.

 

SETTING UP YOUR RENDERING OR SUBLAYOUT

I still use sublayouts, the webform (.ascx) based renderings, but the techniques are the same for MVC based renderings. I will use the term “rendering” to cover both technologies.

THE DISPLAY TITLE

First of all, you need to set a display title for your renderings, as this is what Sitecore displays below the image. Remember that the language used for the display title is not the language you are editing, but the language in which Sitecore is running – exactly the opposite of what the Sitecore Shell does.

A rendering with a display title

All renderings with a display title, here in Danish

THE DATASOURCE LOCATION AND DATASOURCE TEMPLATE

In my project I save all data sources in a hidden folder below my item. This means that they are not shareable between items. A small trade off for a better overview of which components are placed on the page (this is only half the truth – some renderings are in fact shared but for the most part they are not. You should carefully decide what content is shareable and what is not). Setting up the data source location is done using a Sitecore query pointing to the folder below the current item:

Datasource Location and Template

Datasource Location and Template

When the user is seeing the “Select the Associated Content” window, only the components on the page is displayed. Also note that I have given each of my templates their own icon. This helps also.

Select the Associated Content

Select the Associated Content window

THE IMAGE

The field for the image shown in the “Select a Rendering” window is hidden in the appearance section of Sitecore, so you must enable the “Standard Fields” to find it. It is the field called “Thumbnail“.
Now, I know that the field have a “Take screenshot” function, the thumbnail is only 128×128 pixels, making a screenshot very small. I always make a stylized image, upload it to the media library in my own “System” folder and use that instead.

Thumbnail

Thumbnail

When moving the rendering to production you must remember to include the image in your package.

 

SETTING UP YOUR TEMPLATE

The template needs to be user friendly too. Here is what you can do:

THE ICON

Sitecore contains 16.000+ icons, and yes, none of these match your needs. However, spend a few minutes to see if you can find an icon that matches as close as possible. The icon is presented in the “Select the Associated Content” window so although it is not the most important part of your user experience, it is cheap to implement.

Templates with icons

Templates with icons

THE FIELD TITLE

Remember to set a title for your field. My field names are namespaced and rather technical. The user should not be aware of my strange naming. Use the field “Title” and again, remember that it is the language of the Sitecore instance that is displayed, not the language of your content – the exact opposite of what the Sitecore Shell does.

Editing the title

Editing the title – here with a translated text for the field

 

SUMMARY

Setting up the renderings for the experience editor requires more time than knowledge. Be lazy and the end user will hate you. Be thorough and your end user will love you.

 

Posted in Sitecore 7, Sitecore 8 | Tagged , | 6 Comments

Sitecore Failed to update profile pattern

My Sitecore CD servers logged the following errors 2-3 times per minute:

7760 11:10:46 ERROR Failed to update profile pattern
Exception: System.ArgumentNullException
Message: Value cannot be null.
Parameter name: item
Source: Sitecore.Kernel
at Sitecore.Diagnostics.Assert.ArgumentNotNull(Object argument, String argumentName)
at Sitecore.Analytics.Data.Items.ItemRecords`1.<GetDescendants>d__8.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at Sitecore.Analytics.Data.Items.ProfileItem.get_MappedPatterns()
at Sitecore.Analytics.Data.Items.ProfileItem.MatchPattern(IProfileData profile, IPatternDistance distance)
at Sitecore.Analytics.Tracking.Profile.UpdatePattern()

The error occurs because the editors who set up the profile and pattern cards on the solution accidentally renamed the Pattern Cards folder:

Profile and Pattern Cards

Profile and Pattern Cards

Do not rename the folder names. Sitecore uses the names to find the cards. Instead, change the Display Name if the name of the folder does not suit you.

MORE TO READ:

Posted in Sitecore 8 | Tagged , , , | Leave a comment

Get Sitecore System Info

Would you like to know the System Info of your current Sitecore instance:

Sitecore System Info

Sitecore System Info

It’s fairly easy. I created an SysInfo.aspx page and dumped in into the /sitecore modules/shell folder. The page is now available on the URL <yourwebsite>/sitecore modules/shell/sysinfo.aspx.

The top info is retrieved directly from Sitecore using the Sitecore.Configuration.About.ToHtml() method. The rest is retrieved from the Sitecore Configuration:

<%@ Page Language="c#" EnableEventValidation="false" AutoEventWireup="true" EnableViewState="false" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Security.Principal" %>
<%@ Import Namespace="System.Threading" %>

<script runat="server">
  void Page_Init(object sender, System.EventArgs e)
  {
  }

  void Page_Load(object sender, System.EventArgs e)
  {
    Response.Buffer = false;
    Response.BufferOutput = false;
    DataBind();
  }
</script>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>System Info</title>
</head>
<body>
  <form id="MainForm" runat="server"></form>
  <%# Sitecore.Configuration.About.ToHtml() %><br />
  <br />
  <table>
    <tr>
      <td>Instance name</td>
      <td><%# Sitecore.Configuration.Settings.InstanceName %></td>
    </tr>
    <tr>
      <td>Data Folder</td>
      <td><%# Sitecore.Configuration.Settings.DataFolder %></td>
    </tr>
    <tr>
      <td>Log folder</td>
      <td><%# Sitecore.Configuration.Settings.LogFolder %></td>
    </tr>
    <tr>
      <td>Temp folder</td>
      <td><%# Sitecore.Configuration.Settings.TempFolderPath %></td>
    </tr>
    <tr>
      <td>Version file</td>
      <td><%# Sitecore.Configuration.Settings.VersionFilePath %> <%# File.Exists(Sitecore.Configuration.Settings.VersionFilePath) ? "found" : "Not found" %></td>
    </tr>
    <tr>
      <td>License file</td>
      <td><%# Sitecore.Configuration.Settings.LicenseFile %> <%# File.Exists(Sitecore.Configuration.Settings.LicenseFile) ? "found" : "Not found" %></td>
    </tr>
    <tr>
      <td>Analytics enabled</td>
      <td><%# Sitecore.Configuration.Settings.Analytics.Enabled %></td>
    </tr>
    <tr>
      <td>License ID</td>
      <td><%# Sitecore.SecurityModel.License.License.LicenseID %></td>
    </tr>
    <tr>
      <td>Sitecore.xDB.Base is present</td>
      <td><%# Sitecore.SecurityModel.License.License.HasModule("Sitecore.xDB.Base") %></td>
    </tr>
    <tr>
      <td>xDB enabled</td>
      <td><%# Sitecore.Configuration.Settings.GetBoolSetting("Xdb.Enabled", true) %></td>
    </tr>
  </table>
  <hr />
  <%# System.Web.Helpers.ServerInfo.GetHtml() %>
</body>
</html>

To top it off I also added a call to System.Web.Helpers.ServerInfo.GetHtml() to get the .ASP Net info:

.ASP Net Server Info

.ASP Net Server Info

 

CAUTION! THIS IS HIGHLY CLASSIFIED INFORMATION

Please be aware that the information on a page like this is highly classified and is pure candy for any hacker. So make sure that the page is not available from outside, or at least protected by security of some kind.

I always protect pages like this using Declarative security as described in this blog post: Using Declarative Security in Sitecore. Even if the page is not available from the web.

MORE TO READ: 

Posted in c#, General .NET, Sitecore 5, Sitecore 6, Sitecore 7, Sitecore 8 | Tagged , | Leave a comment

Sitecore MongoDB – What do we know about a contact?

The Sitecore Analytics can be somewhat of a black box. All we know is that Sitecore stores everything about a site visitor in a Contact object and stuffs it into MongoDB when the visitor session expires.

A while ago my colleague Lasse Pedersen gave me the following scripts to be executed in RoboMongo to debug and see what Sitecore does know about my contact. The script is especially useful when working with named contacts. Replace the IDENTIFIER_NAME with the name of your contact and run it inside RoboMongo:

identifier = db.Identifiers.findOne({_id: /IDENTIFIER_NAME/i});
contact = db.Contacts.findOne({_id: identifier.contact});
classifications = db.ClassificationsMap.findOne({_id: contact._id });
userAgents = db.UserAgents.find({_id:{$in: classifications.Classificators.UserAgents}});
devices = db.Devices.find({LastKnownContactId: contact._id});
interactions = db.Interactions.find({ContactId: contact._id});

So what does the scripts return?

CONTACT IDENTIFIER

Contact Identifier

Contact Identifier

This is just the identifier information.

CONTACT

Contact Contact

Contact Contact

This is the complete Contact object, including all the Contact Facets that you have added yourself.

CONTACT CLASSIFICATIONS

Contact Classifications

Contact Classifications

This is the data that helps Sitecore identify if the current contact is a human or a robot.

 CONTACT USERAGENTS

Contact Useragents

Contact Useragents

Here you will find the reference to the user agent data, and the information as to whether the contact was indeed identified as a human or a robot.

CONTACT DEVICES

Contact Devices

Contact Devices

A device is essential the cookie that was used when the user interacted with the website. Sitecore will use these data to merge contacts as it links one Contact to every device used.

CONTACT INTERACTIONS

Contact Interactions

Contact Interactions

This is each interaction that the contact have had with the website.

MORE TO READ:

Posted in Sitecore 8 | Leave a comment

Send email via SparkPost and C#

SparkPost is yet another email service. It is easy to work with, seems robust, and it will be fairly easy on your client’s budget, as you get 100.000 emails per month for free (as per jan 2017).

A SparkPost email is set up using a template where you specify replacement tokens using a {{tokenname}} syntax:

Sparkpost Template with token replacements

Sparkpost Template with token replacements

Sending an email is done by calling the SparkPost API endpoint. You submit a JSON document containing the recipient email address plus the replacement tokens of your choice.

So let’s send an email.

First I will create a class that can generate the JSON string containing the data to send to SparkPost. The real coder would create objects that get serialized, but for this example, a StringBuilder will do the trick. To send an email we need at least a template name (the name of the SparkPost template), a campaignID (when tracking the emails afterwards) and an email address. The rest is dynamic data (and there is much more to do than just replacing strings but let’s keep it simple):

public byte[] CreateJsonBody(string templateName, string campaignID, string email, string firstName, string lastName)
{
  StringBuilder sb = new StringBuilder();
  sb.Append(@"{");
  sb.AppendFormat(@"  ""campaign_id"": ""{0}"",", campaignID);
  sb.Append(@"  ""recipients"": [");
  sb.Append(@"    {");
  sb.AppendFormat(@"      ""address"": ""{0}"",", email);
  sb.Append(@"      ""substitution_data"": {");
  sb.AppendFormat(@"        ""email"": ""{0}"",", email);
  sb.AppendFormat(@"        ""firstname"": ""{0}"",", firstName);
  sb.AppendFormat(@"        ""lastname"": ""{0}""", lastName);
  sb.Append(@"      }");
  sb.Append(@"    }");
  sb.Append(@"  ],");
  sb.Append(@"  ""content"": {");
  sb.AppendFormat(@"    ""template_id"": ""{0}""", templateName);
  sb.Append(@"  }");
  sb.Append(@"}");

  byte[] body = Encoding.UTF8.GetBytes(sb.ToString());
  return body;
}

Next step is to create a Service that will post the data to the SparkPost service:

using System;
using System.IO;
using System.Net;
using System.Text;

namespace MyNamespace
{
  public class SparkPostService
  {
    private readonly string _SPARKPOSTURL = "https://api.sparkpost.com/api/v1/transmissions?num_rcpt_errors=3";
    private readonly string _sparkPostAuthorizationKey;

    public SparkPostService(string sparkPostAuthorizationKey)
    {
      _sparkPostAuthorizationKey = sparkPostAuthorizationKey;
    }

    public string SendEmail(byte[] jsonBody, out string contentType)
    {
      try
      {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(_SPARKPOSTURL);

        webRequest.KeepAlive = false;
        webRequest.ServicePoint.ConnectionLimit = 24;
        webRequest.Headers.Add("UserAgent", "MyUserAgent");
        webRequest.ProtocolVersion = HttpVersion.Version10;

        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;

        webRequest.ContentType = "application/json";
        webRequest.Accept = "application/json";
        webRequest.Headers.Add("Authorization", _sparkPostAuthorizationKey);
        webRequest.Method = WebRequestMethods.Http.Post;
        webRequest.ContentLength = jsonBody.Length;
        Stream dataStream = webRequest.GetRequestStream();
        dataStream.Write(jsonBody, 0, jsonBody.Length);
        dataStream.Close();
        byte[] bytes;
        using (WebResponse webResponse = webRequest.GetResponse())
        {
          contentType = webResponse.ContentType;
          using (Stream stream = webResponse.GetResponseStream())
          {
            using (MemoryStream memoryStream = new MemoryStream())
            {
              byte[] buffer = new byte[0x1000];
              int bytesRead;
              while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
              {
                memoryStream.Write(buffer, 0, bytesRead);
              }
              bytes = memoryStream.ToArray();
            }
          }
        }
        return Encoding.UTF8.GetString(bytes);
      }
      catch (Exception ex)
      {
        throw new Exception(GetType() + "Failed to retrieve data from '" + _SPARKPOSTURL + "': " + ex.Message, ex);
      }
    }


  }
}

To send an email I can now do the following:

string sparkPostKey = (found in SparkPost);
string contentType;

SparkPostService service = new SparkPostService(sparkPostKey);
string result = service.SendEmail(CreateJsonBody("my-first-email", "myCampaign", "bp@pentia.dk", "Brian", "Pedersen"), out contentType);

The SparkPost authorization key is created inside SparkPost.

SparkPost will now send an email where my name and email is substituted:

Sparkpost Email

Sparkpost Email

MORE TO READ:

Posted in c#, General .NET | Tagged , | Leave a comment