Sitecore Scheduled Task – Schedule time format and other quirks

The Sitecore task runner, usually called Scheduled Tasks, is a simple way of executing code with intervals. You configure scheduled tasks in Sitecore, at /sitecore/system/Tasks/Schedules:

Scheduled Task

Scheduled Task

The quirkiest configuration setting is the “Schedule” field, which is a pipe separated string determining when the task should run:

{start timestamp}|{end timestamp}|{days to run bit pattern}|{interval}

  • Start timestamp and End timestamp: Determines the start and end of the scheduled task.
    Format is the Sitecore ISO datetime, YearMonthDayTHoursMinutesSeconds.
    Example: 20000101T000000 = January 1st 2000 at 00:00:00.
    (the font Sitecore uses does not help reading the timestamp at all, I know).
    NOTE: If you do the format wrong, the task will run.
  • Days to run: A 7 bit pattern determining which days the task must run:
    1 = Sunday
    2 = Monday
    4 = Tuesday
    8 = Wednesday
    16 = Thursday
    32 = Friday
    64 = Saturday
    So, 127 means to run the task every day. To run the task on Saturday and Sunday, add the 2 values, 1+64 = 65.
  • Interval: How long time between each run. 00:05:00 means that the task will run with 5 minute intervals.

WHY DOESN’T MY TASK RUN WITH MY SPECIFIED INTERVALS?

Sitecore uses no less than 2 sitecore.config settings to determine when the task runner should run:

<scheduling>
  <frequency>00:05:00</frequency>
  <agent type="Sitecore.Tasks.DatabaseAgent" method="Run" interval="00:05:00">
    <param desc="database">master</param>
    <param desc="schedule root">/sitecore/system/Tasks/Schedules</param>
    <LogActivity>true</LogActivity>
  </agent>
</scheduling>

The frequency setting determine when the global Sitecore task runner should run at all.

The agent determine when the tasks configured in the master database at the root /sitecore/system/Tasks/Schedules should run.

So, in the example above, my task runner runs every 5 minutes, checking the config file for tasks to run. It will then run the agent with 5 minute intervals. If another task is running, it could block the task runner, delaying the agent from running. With the above settings, my best case scenario is that my agent runs every 5 minutes.

The tasks configured in Sitecore could also block. If a task should run every 5 minutes, but the execution time is 11 minutes, the agent would run the task again after 15 minutes, in the best case scenario. To avoid this, you can mark your task as “async” in the configuration, but beware that long running (or never ending) tasks will then run simultaneously, slowing down Sitecore.

CAN I HAVE TASKS RUNNING ON MY CM SERVER ONLY?

Yes, you can add a new folder in Sitecore, and then add a new agent that points to the new folder as root, to the sitecore.config file of the CM server.

See more here: Sitecore Scheduled Tasks – Run on certain server instance.

CAN I RUN TASKS AT THE SAME TIME EVERY DAY?

Kind of. You can have your task running once a day within the same interval, using a little code.

See more here: Run Sitecore scheduled task at the same time every day.

IN WHAT CONTEXT DOES MY TASK RUN?

Sitecore have created a site called “scheduler” where the context is defined:

<sites>
  <site name="scheduler" database="master" language="da" enableTracking="false" domain="sitecore" />
</sites>

To run the task in a different context, use a context switcher.

DO I HAVE A HTTP CONTEXT WHEN RUNNING SCHEDULED TASKS?

No.

DO I HAVE A USER WHEN RUNNING SCHEDULED TASKS?

Do not expect to have a user. Expect the Sitecore Scheduled Task – Schedule time format and other quirks to be NULL, unless you use a UserSwitcher.

CAN I RUN THE SAME CODE FROM DIFFERENT TASKS?

Yes. Sitecore have split the definition of the code to run from the definition of the schedule. The code is defined as a “command” where you define the class and the method to run:

Task Commands

Task Commands

The schedule simply points to the command to run, and you can have as many schedules as you want:

Pointing to a command

Pointing to a command

WHAT ARE THE “ITEMS” FIELD FOR?

Items Field

Items Field

No one really knows what the items field are for, but according to old Sitecore folklore, you can add a pipe separated list of item GUIDS (or even item paths), and the “itemArray” property of the method you call will contain the list of items:

public void Execute(Item[] itemArray, CommandItem commandItem, ScheduleItem scheduleItem)
{
  foreach (Item item in itemArray)
  {
    // do something with the item
  }
}

MORE TO READ:

 

Posted in c#, Sitecore, Sitecore 5, Sitecore 6, Sitecore 7, Sitecore 8 | Tagged , , , | 1 Comment

Webhook Event Receiver with Azure Functions

Microsoft Azure Functions is a solution to run small pieces of code in the cloud. If your code is very small and have only one purpose, an Azure Function could be the cost effective solution.

This is an example of a generic Webhook event receiver. A webhook is a way for other systems to make a callback to your system whenever an event is raised in the other system. This Webhook event receiver will simply receive the Webhook event’s payload (payload = the JSON that the other system is POST’ing to you), envelope the payload and write it to a Queue.

STEP 1: SET UP AN AZURE FUNCTION

Select an Function App and create a new function:

Create New Azure Function

Create New Azure Function

 

STEP 2: CREATE A NEW FUNCTION

Select New Function and from the “API & Webhooks”, select “Generic Webhook – C#:

Create Generic Webhook

Create Generic Webhook

Microsoft will now create a Webhook event receiver boilerplate code file, which we will modify slightly later.

STEP 3: ADD A ROUTE TEMPLATE

Because we would like to have more than one URL to our Azure Function (each webhook caller should have it’s own URL so we can differentiate between them) we need to add a route template.

Select the “Integrate” section and modify the “Route template”. Add {caller} to the field:

Add a Route Template

Add a Route Template

STEP 4: INTEGRATE WITH AZURE QUEUE STORAGE

We need to be able to write to an Azure Queue. In Azure Functions, the integration is almost out of the box.

Select the “Integrate” section and under “Outputs”, click “New Output”, and select the “Azure Queue Storage”:

Azure Queue Storage

Azure Queue Storage

Configure the Azure Queue Settings:

Azure Queue Settings

Azure Queue Settings

  • Message parameter name: The Azure Function knows about the queue through a parameter to the function. This is the name of the parameter.
  • Storage account connection: The connection string to the storage where the azure queue is located.
  • Queue name: Name of queue. If the queue does not exist (it does not exist by default) a queue will be created for you.

STEP 5: MODIFY THE BOILERPLATE CODE

We need to make small but simple modifications to the boilerplate code (I have marked the changes form the boilerplate code with comments):

#r "Newtonsoft.Json"

using System;
using System.Net;
using Newtonsoft.Json;

// The string caller was added to the function parameters to get the caller from the URL.
// The ICollector<string> outQueue was added to the function parameters to get access to the output queue.
public static async Task<object> Run(HttpRequestMessage req, string caller, ICollector<string> outQueue, TraceWriter log)
{
    log.Info($"Webhook was triggered!");

    // The JSON payload is found in the request
    string jsonContent = await req.Content.ReadAsStringAsync();
    dynamic data = JsonConvert.DeserializeObject(jsonContent);

    // Create a dynamic JSON output, enveloping the payload with 
	// The caller, a timestamp, and the payload itself
    dynamic outData = new Newtonsoft.Json.Linq.JObject();
    outData.caller = caller;
    outData.timeStamp = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
    outData.payload = data;
    
	// Add the JSON as a string to the output queue
    outQueue.Add(JsonConvert.SerializeObject(outData));     
    
	// Return status 200 OK to the calling system.
    return req.CreateResponse(HttpStatusCode.OK, new
    {
        caller = $"{caller}",
        status = "OK"
    });
}

STEP 6: TEST IT

Azure Functions have a built in tester. Run a test to ensure that you have pasted the correct code and written the correct names in the “Integrate” fields:

Test

Test

Use the Microsoft Azure Storage Explorer to check that the event was written to the queue:

Azure Storage Explorer

Azure Storage Explorer

STEP 7: CREATE KEYS FOR THE WEBHOOK EVENT SENDERS

Azure Functions are not available unless you know the URL and the key. Select “Manage” and add a new Function Key.

Function Keys

Function Keys

The difference between Function Keys and Host Keys are that Function Keys are specific to that function, but the Host Keys are global keys that can be used for any function.

To call your Azure Function, the caller need to know the URL + the key. The key can be send in more than one way:

  • Using the URL, parameter ?code=(key value) and &clientid=(key name)
  • In the request header, using the x-functions-key HTTP header.

STEP 8: GIVE URL AND KEY TO CALLING SYSTEM

This is a Restlet Client example that calls my function. I use the QueryString to add the code and clientid parameters:

MORE TO READ:

 

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

Requesting Azure API Management URL’s

The Azure API Management is a scalable and secure API gateway/proxy/cache where you can expose your API’s externally and still have secure access.

In Azure API Management you create a “Product” which is a collection of API’s that is protected using the same product key.

2 Azure API Management products, protected with a key

2 Azure API Management products, protected with a key

The 2 products above contains a collection of API’s, and each product have it’s own key.

As a developer you can find the API Keys using the Azure API Management Service Developer Portal:

APIM Developer Portal

APIM Developer Portal

When clicking around you will end up finding the “Try it” button where you are allowed to test your API endpoints:

Try it button

Try it button

And here you can get the subscription key by clicking the icon shaped as an eye:

Find the key here

Find the key here

When calling any API, you simply need to add the subscription key to the request header in the field:

  • Ocp-Apim-Subscription-Key

This is an example on how to GET or POST to an API that is secured by the Azure API Management. There is many ways to do it, and this is not the most elegant. But this code will work in production with most versions of .NET:

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

namespace MyNamespace
{
  public class AzureApimService
  {
    private readonly string _domain;
    private readonly string _ocp_Apim_Subscription_Key;

    public AzureApimService(string domain, string subscriptionKey)
    {
      _domain = domain;
      _ocp_Apim_Subscription_Key = subscriptionKey;
    }

    public byte[] Get(string relativePath, out string contentType)
    {
      Uri fullyQualifiedUrl = GetFullyQualifiedURL(_domain, relativePath);
      try
      {
        byte[] bytes;
        HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create(fullyQualifiedUrl);
        webRequest.Headers.Add("Ocp-Apim-Trace", "true");
        webRequest.Headers.Add("Ocp-Apim-Subscription-Key", _ocp_Apim_Subscription_Key);
        webRequest.Headers.Add("UserAgent", "YourUserAgent");
        webRequest.KeepAlive = false;
        webRequest.ProtocolVersion = HttpVersion.Version10;
        webRequest.ServicePoint.ConnectionLimit = 24;
        webRequest.Method = WebRequestMethods.Http.Get;
        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();
            }
          }
        }
        // For test/debug purposes (to see what is actually returned by the service)
        Console.WriteLine("Response data (relativePath: \"{0}\"):\n{1}\n\n", relativePath, Encoding.Default.GetString(bytes));
        return bytes;
      }
      catch (Exception ex)
      {
        throw new Exception("Failed to retrieve data from '" + fullyQualifiedUrl + "': " + ex.Message, ex);
      }
    }

    public byte[] Post(string relativePath, byte[] postData, out string contentType)
    {
      Uri fullyQualifiedUrl = GetFullyQualifiedURL(_domain, relativePath);
      try
      {
        byte[] bytes;
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(fullyQualifiedUrl);
        webRequest.Headers.Add("Ocp-Apim-Trace", "true");
        webRequest.Headers.Add("Ocp-Apim-Subscription-Key", _ocp_Apim_Subscription_Key);
        webRequest.KeepAlive = false;
        webRequest.ServicePoint.ConnectionLimit = 24;
        webRequest.Headers.Add("UserAgent", "YourUserAgent");
        webRequest.ProtocolVersion = HttpVersion.Version10; 
        webRequest.ContentType = "application/json";
        webRequest.Method = WebRequestMethods.Http.Post;
        webRequest.ContentLength = postData.Length;
        Stream dataStream = webRequest.GetRequestStream();
        dataStream.Write(postData, 0, postData.Length);
        dataStream.Close();
        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();
            }
          }
        }
        // For test/debug purposes (to see what is actually returned by the service)
        Console.WriteLine("Response data (relativePath: \"{0}\"):\n{1}\n\n", relativePath, Encoding.Default.GetString(bytes));
        return bytes;
      }
      catch (Exception ex)
      {
        throw new Exception("Failed to retrieve data from '" + fullyQualifiedUrl + "': " + ex.Message, ex);
      }
    }

    private static Uri GetFullyQualifiedURL(string domain, string relativePath)
    {
      if (!domain.EndsWith("/"))
        domain = domain + "/";
      if (relativePath.StartsWith("/"))
        relativePath = relativePath.Remove(0, 1);
      return new Uri(domain + relativePath);
    }
  }
}

The service is simple to use:

AzureApimService service = new AzureApimService("https://yourapim.azure-api.net", "12a6aca3c5a242f181f3dec39b264ab5");
string contentType;
byte[] response = service.Get("/api/endpoint", out contentType);

MORE TO READ:

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

Sitecore contact cannot be loaded, code never responds

In Sitecore, it is possible to encounter a situation where the calls identifying or locking a contact never responds, but there is no errors returned.

A call to identify:

Tracker.Current.Session.Identify(contactName);

And a call to Load a contact:

Contact contact = 
    contactRepository.LoadContactReadOnly(username);

Can both take forever without any errors or any timeout.

This situation can occur if the Contact Successor points to the original Contact in a loop. When merging a contact, Sitecore will create a new contact, the Surviving contact. The existing contact (called the Dying contact) still contains all the interaction data from before the merge, so instead of Sitecore having to update all data fields with a new ID, it creates a “Successor” pointer to the Dying Contact.

Surviving Contact

Surviving Contact

But in certain situations, the Dying Contact will also have a Successor, which points back to the Surviving Contact, creating an infinite loop:

The Dying Contact's Successor points to the surviving contact.

The Dying Contact’s Successor points to the surviving contact.

The patterns for this situation are many, but usually involves merging and changing contact identifiers, and can be reproduced like this:

  • Create a contact “A”
  • Create a new contact “B”
  • Merge contact “B” with “A”
  • Merge contact “A” with “B”

To avoid this situation, it is customary to rename the dying contact’s (“A”) identifier to an obscure name (a guid). But the renaming might fail if the dying contact is locked, leaving a contact with a reusable identifier. The “Extended Contact Repository” which I have described previously will unfortunately gladly create a new contact with an existing name.

HOW TO RESOLVE IT:

The situation needs to be resolved manually. Find the contact, open RoboMongo and search for the contact:

identifier = db.Identifiers.findOne({_id: /NAME_OF_IDENTIFIER/i});
contact = db.Contacts.find({_id: identifier.contact});

Copy the “Successor” value from the contact, and find the Successor:

successor = db.Contacts.find({_id: NUUID("b1e760d7-7c60-4b1d-818f-e357f303ebef9")});

Right click the “Edit Document” button and delete the “Successor” field from the dying contact:

Delete Successor Field from the Dying Contact, breaking the infinite loop

Delete Successor Field from the Dying Contact, breaking the infinite loop

This can be done directly in production, and the code reacts instantly when the loop have been broken.

MORE TO READ:

 

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

Sitecore Media Library integration with Azure CDN using origin pull

If your Sitecore website is heavy on content from the media library you can offload your Sitecore instances by allowing images to be retrieved from a Content Delivery Network (CDN). If you use Microsoft Azure, you do not need to upload images to the CDN, as Azure support origin pull.

Origin pull is a mechanism where the CDN automatically retrieves any missing content item from an origin host if the content is missing. In Azure, even parameters to the content is supported, so if you scale images with ?w=100, these parameters are supported, and the Azure CDN will store the scaled image.

To set up origin pull in Azure CDN, you first go to your CDN profile:

Azure CDN Profile

Azure CDN Profile

Then you click the + sign to add an endpoint:

Azure CDN Add Endpoint

Azure CDN Add Endpoint

And add an endpoint with the type “Custom Origin”:

Azure CDN Endpoint with Custom Origin

Azure CDN Endpoint with Custom Origin

The “name” is the name of the endpoint. The “Origin hostname” is the URL to your public Sitecore website. And remember to specify the correct protocol. If your website is running HTTPS, the CDN should use HTTPS as well.

SETTING UP SITECORE:

The rest is configuration in Sitecore. You control the CDN properties using these settings, found in the Sitecore.config file:

<setting name="Media.MediaLinkServerUrl" value="https://myendpoint.azureedge.net" />
<setting name="Media.MediaLinkPrefix" value="-/media" />
<setting name="Media.AlwaysIncludeServerUrl" value="true" />
<setting name="MediaResponse.Cacheability" value="public" />
  • Media.MediaLinkServerUrl = The URL to the Azure CDN, as defined when creating the Azure Endpoint
  • Media.MediaLinkPrefix = The media library link URL. Together with the Media.MediaLinkServerUrl, the complete server URL is created. In the example, my url is https://myendpoint.azureedge.net/-/media/%5Bmedia library content]
  • Media.AlwaysIncludeServerUrl = Tells Sitecore to always include the server URL in the media requets
  • MediaResponse.Cacheability = Allows the cache settings of any item to be publicly available, allowing the Azure CDN to access the MaxAge, SlidingExpiration and VaryHeader parameters.

DRAWBACKS OF USING A CDN:

  • Your website needs to be public. When developing and testing you need to disable the CDN settings as the Azure CDN cannot read from a non-public website. Testing is therefore in production as the website runs.
  • Security settings on media library items cannot be used. Once a media library item is on the CDN it is public to everyone.

MORE TO READ:

 

Posted in Sitecore 8 | Tagged , , , , , , , | 3 Comments

Sitecore ContentSearch – Get items from SOLR or Lucene – A base class implementation

Reading items from Sitecore is pretty straight forward:

Sitecore.Data.Items.Item item = 
   Sitecore.Context.Database.GetItem("/sitecore/context/.../...");

And it is fast, unless you need to retrieve items from many paths, or need to retrieve every child of a certain base class. In these situations you resolve to using the built in ContentSearch, which is a Lucene or SOLR index.

When working with objects from the ContentSearch API you will have to create your own model classes that maps the indexed fields to class properties. This is done using an IndexFieldAttribute to the properties of the class that will represent the indexed data:

[IndexField("customerpage")]
public ID CustomerPageId {	get; internal set; }

[IndexField("customername")]
public string CustomerName { get; internal set; }

The default indexes called sitecore_core_index, sitecore_master_index and sitecore_web_index is born with a long list of default properties that is useful for every class. Because of this it makes sense to let every one of your model classes inherit from a base class that maps these fields for you.

So let’s code.

STEP 1: CREATE A BASE CLASS

This base class maps the most common fields. There are many more for you to explore, but this particular class have been the base class of a huge project that I have been working on for the past 4 years:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using Sitecore.Configuration;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.Converters;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;

namespace MySearch
{
  [Serializable]
  public abstract class SearchResultItem
  {
    [NonSerialized]
    private Item _item;

    // Get the actual Sitecore item. Beware that using this property 
    // will substantially slow your query, as it looks up the item
    // in Sitecore. Use with caution, and try to avoid using it in
    // LINQ or enumerations 
    public virtual Item Item
    {
      get { return _item ?? (_item = GetItem()); } set { _item = value; }
    }

    // Returns the Item ID (in SOLR this is stored as a short GUID in the _group field)
    [IndexField(Sitecore.ContentSearch.BuiltinFields.Group)]
    [TypeConverter(typeof(IndexFieldIDValueConverter))]
    public virtual ID ItemId
    {
      get; set;
    }

    // This is a combined key describing the Sitecore item in details
    // For example: sitecore://web/{7102ee6b-6361-41ad-a47f-832002082a1a}?lang=da&ver=1&ndx=sitecore_web_index
    // With the ItemUri class you can extract the individual values like database, id, language, version
    [IndexField(Sitecore.ContentSearch.BuiltinFields.UniqueId)]
    [TypeConverter(typeof(IndexFieldItemUriValueConverter))]
    public virtual ItemUri ItemUri
    {
      get; set;
    }

    // Return the item language
    [IndexField(Sitecore.ContentSearch.BuiltinFields.Language)]
    public virtual string Language
    {
      get; set;
    }

    // Returns true if the item is the latest version. When reading from the
    // web database index, this will alwaus be true.    
    [IndexField(Sitecore.ContentSearch.BuiltinFields.LatestVersion)]
    public bool IsLatestVersion
    {
      get; set;
    }

    // Returns the ID's of every parent sorted by top parent first
    [IndexField(Sitecore.ContentSearch.BuiltinFields.Path)]
    [TypeConverter(typeof(IndexFieldEnumerableConverter))]
    public virtual IEnumerable<ID> ItemAncestorsAndSelf
    {
      get; set;
    }

    // Returns the updated datetime
    [IndexField(Sitecore.ContentSearch.BuiltinFields.SmallUpdatedDate)]
    public DateTime Updated
    {
      get; set;
    }

    // Returns every template that this item implements and inherits
    [IndexField(Sitecore.ContentSearch.BuiltinFields.AllTemplates)]
    [TypeConverter(typeof(IndexFieldEnumerableConverter))]
    public virtual IEnumerable<ID> ItemBaseTemplates
    {
      get; set;
    }

    private Item GetItem()
    {
      Assert.IsNotNull(ItemUri, "ItemUri is null.");
      return Factory.GetDatabase(ItemUri.DatabaseName).GetItem(ItemUri.ItemID, ItemUri.Language, ItemUri.Version);
    }
  }
}

STEP 2: CREATE A MODEL CLASS FOR A SPECIFIC TEMPLATE

This example inherits from the SearchResultItem base class, and encapsulates a Customer template containing 2 fields, CustomerPage and CustomerName.

namespace MySearch
{
  [DataContract]
  [Serializable]
  public class CustomerModel : SearchResultItem
  {
    [DataMember]
    [IndexField("customername")]
    public string CustomerName { get; internal set; }

    [IndexField("customerpage")]
    public ID CustomerPageId { get; internal set; }
  }
}

STEP 3: USING THE BASE CLASS TO SEARCH USING PREDICATES

A Predicate are a Latin word for “making search soo much easier”. Predicates defines reusable static functions. When run, Predicates become part of the index query itself, further improving performance. So let’s start by making 3 predicates:

namespace MySearch
{
  public static class Predicates
  {
    // Ensure that we only return the latest version
    public static Expression<Func<T, bool>> IsLatestVersion<T>() where T : SearchResultItem
    {
      return searchResultItem => searchResultItem.IsLatestVersion;
    }

    // Ensure that the item returned is based on, or inherits from the specified template
    public static Expression<Func<T, bool>> IsDerived<T>(ID templateID) where T : SearchResultItem
    {
      return searchResultItem => searchResultItem.ItemBaseTemplates.Contains(templateID);
    }

    // Ensure that the item returned is a content item by checking that the 
    // content root is part of the item path 
    public static Expression<Func<T, bool>> IsContentItem<T>() where T : SearchResultItem
    {
      return searchResultItem => searchResultItem.ItemAncestorsAndSelf.Contains(ItemIDs.ContentRoot);
    }
  }
}

With these predicates in place, I can create a repository for my Customer items:

namespace MySearch
{
  public class CustomerModelRepository
  {
    private readonly Database _database;

    public CustomerModelRepository() : this(Context.Database)
    {
    }

    public CustomerModelRepository(Database database)
    {
      _database = database;
    }

    public IEnumerable<CustomerModel> GetAll()
    {
      return Get(PredicateBuilder.True<CustomerModel>());
    }

    private IEnumerable<CustomerModel> Get(Expression<Func<CustomerModel, bool>> predicate)
    {
      using (IProviderSearchContext context = GetIndex(_database).CreateSearchContext(SearchSecurityOptions.DisableSecurityCheck))
      {
        return context.GetQueryable<CustomerModel>()
          .Where(Predicates.IsDerived<CustomerModel>(new ID("{1EB6DC02-4EBD-427A-8E36-7D2327219B6C}")))
          .Where(Predicates.IsLatestVersion<CustomerModel>())
          .Where(Predicates.IsContentItem<CustomerModel>())
          .Where(predicate).ToList();
      }
    }
    
    private static ISearchIndex GetIndex(Database database)
    {
      Assert.ArgumentNotNull(database, "database");
      switch (database.Name.ToLowerInvariant())
      {
        case "core":
          return ContentSearchManager.GetIndex("sitecore_core_index");
        case "master":
          return ContentSearchManager.GetIndex("sitecore_master_index");
        case "web":
          return ContentSearchManager.GetIndex("sitecore_web_index");
        default:
          throw new ArgumentException(string.Format("Database '{0}' doesn't have a default index.", database.Name));
      }
    }
  }
}

The private Get() method returns every index item following these criteria:

  • Must implement or derive from the template with the specified GUID (the GUID of the Customer template) = Predicates.IsDerived
  • And must be the latest version = Predicates.IsLatestVersion
  • And must be a content item = Predicates.IsContentItem

The repository is used like this:

CustomerModelRepository rep = new CustomerModelRepository(Sitecore.Context.Database);
IEnumerable<CustomerModel> allCustomers = rep.GetAll();
foreach (CustomerModel customer in allCustomers)
{
  // do something with the customer
  customer.CustomerName;
}

I hope this introduction will help you create your own base class implementation and start making fast content searches.

MORE TO READ:

For more SOLR knowledge, you should read my colleague Søren Engel‘s posts about SOLR:

These resources could also be helpful:

 

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

Sitecore user:created event not fired on Membership.CreateUser

Sitecore have since 7.5’ish fired events each time you manipulate the users in the .NET Membership database:

<event name="user:created"></event>
<event name="user:deleted"></event>
<event name="user:updated"></event>
<event name="roles:usersAdded"></event>
<event name="roles:usersRemoved"></event>

But I noticed that the user:created event was not fired. This is because I call the .NET Membership provider directly:

string userNameWithDomain = "extranet\\myuser";
string password = "somepassword";
string email = "myuser@somewhere.com";
Membership.CreateUser(userNameWithDomain, password, email);

This call to Membership is not handled by Sitecore, thus no event is executed. To fix this I have found 2 solutions, one is not good and the other one is not good either.

SOLUTION 1: CALL THE SITECORE MEMBERSHIP PROVIDER DIRECTLY

This solution ignores the web.config settings and assume that you have not switched or overwritten the Membership Provider yourself. But it will fire the event though:

Sitecore.Security.SitecoreMembershipProvider provider = new Sitecore.Security.SitecoreMembershipProvider();
MembershipCreateStatus status = MembershipCreateStatus.Success;
provider.CreateUser(usernameWithDomain, password, email, "", "", true, null, out status);
if (status != MembershipCreateStatus.Success)
  throw new MembershipCreateUserException(status);

SOLUTION 2: RAISE THE EVENT YOURSELF

This solution requires you to raise the event yourself. You need encapsulate the call to create a user in your own class, and instruct everyone to never call Membership.CreateUser() directly:

MembershipUser user = Membership.CreateUser(usernameWithDomain, password, email);
Event.RaiseEvent("user:created", user);

I can see from other blog posts that the user events are not the most widely used events in the Sitecore toolbox. If you have found another solution to this problem please let me know.

MORE TO READ:

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

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