Sitecore contact facets – Create your own facet

This article describes how to create a simple Sitecore facet consisting of a DateTime and a list of strings.

A contact is made up of facets. Here are all the facets Sitecore uses (you will find the facets in \App_Config\Include\Sitecore.Analytics.Model.Config):

<facets>
  <facet name="Personal" contract="Sitecore.Analytics.Model.Entities.IContactPersonalInfo, Sitecore.Analytics.Model" />
  <facet name="Addresses" contract="Sitecore.Analytics.Model.Entities.IContactAddresses, Sitecore.Analytics.Model" />
  <facet name="Emails" contract="Sitecore.Analytics.Model.Entities.IContactEmailAddresses, Sitecore.Analytics.Model" />
  <facet name="Phone Numbers" contract="Sitecore.Analytics.Model.Entities.IContactPhoneNumbers, Sitecore.Analytics.Model" />
  <facet name="Picture" contract="Sitecore.Analytics.Model.Entities.IContactPicture, Sitecore.Analytics.Model" />
  <facet name="Communication Profile" contract="Sitecore.Analytics.Model.Entities.IContactCommunicationProfile, Sitecore.Analytics.Model" />
  <facet name="Preferences" contract="Sitecore.Analytics.Model.Entities.IContactPreferences, Sitecore.Analytics.Model" />
</facets>

In this example I will add a facet that consists of a date and a list of strings. I will call it “AvailablePublishers“.

This is a real-life example where I needed to store a list of publishers that were available the last time the user was online. Each publisher is just an ID (a string) and I store these as a list on the Contact:

Available Publishers Facet

Available Publishers Facet

It sounds simple, and it is – but there is a lot of code involved. So hang on, lets code.

STEP 1: THE BASIC INTERFACES

The “AvailablePublishers” is a Facet, the list below consists of Elements. So I need to create a IFacet interface and a IElement interface.

Here is the IFacet:

using System;
using Sitecore.Analytics.Model.Framework;

namespace PT.AvailablePublishers
{
  public interface IAvailablePublishersFacet : IFacet
  {
    IElementCollection<IAvailablePublishersElement> AvailablePublishers { get; }
    DateTime Updated { get; set; }
  }
}

The IFacet contains a list (IElementCollection) of my IElement. Here is the IElement:

using Sitecore.Analytics.Model.Framework;

namespace PT.AvailablePublishers
{
  public interface IAvailablePublishersElement : IElement, IValidatable
  {
    string PublisherID { get; set; }
  }
}

STEP 2: THE IMPLEMENTATION

Now we need concrete classes implementing IAvailablePublishersFacet and IAvailablePublishersElement:

Here is the AvailablePublishersFacet class:

using System;
using Sitecore.Analytics.Model.Framework;

namespace PT.AvailablePublishers
{
  [Serializable]
  public class AvailablePublishersFacet : Facet, IAvailablePublishersFacet
  {
    public static readonly string _FACET_NAME = "AvailablePublishers";
    private const string _UPDATED_NAME = "LastUpdated";

    public AvailablePublishersFacet()
    {
      EnsureCollection<IAvailablePublishersElement>(_FACET_NAME);
    }

    public IElementCollection<IAvailablePublishersElement> AvailablePublishers
    {
      get
      {
        return GetCollection<IAvailablePublishersElement>(_FACET_NAME);
      }
    }


    public DateTime Updated
    {
      get
      {
        return GetAttribute<DateTime>(_UPDATED_NAME);
      }
      set
      {
        SetAttribute(_UPDATED_NAME, value);
      }
    }
  }
}

and the AvailablePublishersElement class:

using System;
using Sitecore.Analytics.Model.Framework;

namespace PT.AvailablePublishers
{
  [Serializable]
  public class AvailablePublishersElement : Element, IAvailablePublishersElement
  {
    private const string _PUBLISHERID = "PublisherID";

    public AvailablePublishersElement()
    {
      EnsureAttribute<string>(_PUBLISHERID);
    }
    
    public string PublisherID
    {
      get
      {
        return GetAttribute<string>(_PUBLISHERID);
      }
      set
      {
        SetAttribute(_PUBLISHERID, value);
      }
    }
  }
}

Both classes are serializable.

Getting and setting properties are done using GetAttribute and SetAttribute methods retrieved from Sitecore.Analytics.Model.Framework.Element and Sitecore.Analytics.Model.Framework.Facet.

Lists are done using IElementCollection or IElementDictionary, provided by Sitecore.

STEP 3: REGISTER FACET IN SITECORE CONFIGURATION

The facets and elements are registered in Sitecore. In the configuration you also register the  IAvailablePublishersFacet as an element, even when it inherits from IFacet.

This is a simplified version of the \App_Config\Include\Sitecore.Analytics.Model.config where I have removed all other elements but my own:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <model>
      <elements>
        <element interface="PT.AvailablePublishers.IAvailablePublishersElement, MyDLL" implementation="PT.AvailablePublishers.AvailablePublishersElement, MyDLL" />
        <element interface="PT.AvailablePublishers.IAvailablePublishersFacet, MyDLL" implementation="PT.AvailablePublishers.AvailablePublishersFacet, MyDLL" />
      </elements>
      <entities>
        <contact>
          <facets>
            <facet name="AvailablePublishers" contract="PT.AvailablePublishers.IAvailablePublishersFacet, MyDLL" />
          </facets>
        </contact>
      </entities>
    </model>
  </sitecore>
</configuration>

So as you can see, both my interfaces are defined in <elements/>. The <elements/> describes the implementation of the interface, just like IOC would do it.

The facet is defined in <facets/> with a unique name. This unique name is the name you use to find the facet when you need to access it.

STEP 4: CALL THE FACET

The facet is now defined. Next step is to use the facet. To retrieve a facet you need a Contact. The contact is usually retrieved from the Sitecore Tracker:

Contact contact = Tracker.Current.Session.Contact;

This is a repository than can get the list of available publishers from a contact, and update the list of available publishers:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PT.AvailablePublishers;
using Sitecore.Analytics.Tracking;

namespace PT.Forum.Domain.NejTakPlus.Model.Repositories
{
  public class AvailablePublishersRepository
  {
    public IEnumerable<string> Get(Contact contact)
    {
      IAvailablePublishersFacet facet = contact.GetFacet<IAvailablePublishersFacet>(AvailablePublishersFacet._FACET_NAME);
      return facet.AvailablePublishers.Select(element => element.PublisherID);
    }

    public void Set(Contact contact, IEnumerable<string> availablePublisherKeys)
    {
      IAvailablePublishersFacet facet = contact.GetFacet<IAvailablePublishersFacet>(AvailablePublishersFacet._FACET_NAME);
      facet.Updated = DateTime.Now;
      while (facet.AvailablePublishers.Count > 0)
        facet.AvailablePublishers.Remove(0);
      foreach (string availablePublisherKey in availablePublisherKeys)
      {
        facet.AvailablePublishers.Create().PublisherID = availablePublisherKey;
      }
    }
  }
}

Notice the contact.GetFacet<>() method. Here you specify the name of the facet you defined in the <facets/> section of the config file. Luckily you also define the same name in the code, so I can get the facet name from my AvailablePublishersFacet class.

Also notice the Set() method. If you wish to clear the list before inserting, you need to iterate through the list and remove them one by one. There is no Clear() method.

Remember that facets are not written to MongoDB before your session ends. So you cannot see your fact in Mongo before your session is abandoned. You can try calling:

Session.Abandon();

To force Sitecore to write the data to MongoDB.

That’s it. Happy coding.

MORE TO READ:

 

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

Sitecore 8 and Tracker.Current.Session.Identify – Overriding expired contact session lock for contact id

The new Sitecore 8 Experience Profile is a vital part, yes almost a cornerstone of the new xDB concept.

In xDB, you store information about the current user, anonymous or named, as a Contact in the Experience Profile (stored in the MongoDB).
Any user begins his life as an anonymous user, and the associated Contact object has no identifier, only a key matched in the cookie of the current user.

In order to connect the Contact with a named user, you use the Tracker.Current.Session.Identify(userName) method:

if (!Tracker.IsActive)
  return;
Tracker.Current.Session.Identify("extranet\\user@domain.com");

The method identifies the user@domain.com as a Contact, creates a named Contact if the contact does not exist, or merges the current contact data into one named Contact. Also, the Contact will be locked for this user.

Please note that Tracker.Current.Session.Identify(userName) accepts a string. You can create named contacts with any key. This is great if your named users does not come from Sitecore Users.

But here lies the danger. If you by accident Identifies extranet\anonymous, every anonymous visitor will be merged and locked to the same Contact. And since a contact can only be locked to one session at a time, any subsequent calls to Tracker.Current.Session.Identify will wait, and wait, and wait, … until the previous contact unlocks the Contact.

And the log will be filled with:

Message: Failed to extend contact lease for contact 43188c31-cbe9-4386-8739-c12c3dc049c2

Or even:

2844 18:01:11 ERROR Cannot finish Analytics page tracking
Exception: Sitecore.Analytics.Exceptions.ContactLockException
Message: Failed to extend contact lease for contact 43188c31-cbe9-4386-8739-c12c3dc049c2
Source: Sitecore.Analytics
at Sitecore.Analytics.Tracking.ContactManager.SaveAndReleaseContact(Contact contact)
at Sitecore.Analytics.Pipelines.EndAnalytics.ReleaseContact.Process(PipelineArgs args)
at (Object , Object[] )
at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
at Sitecore.Analytics.Pipelines.HttpRequest.EndAnalytics.Process(HttpRequestArgs args)

So it is VERY important that you check that the user identified is not your anonymous user:

// THIS IS BAD!!!
// The user could be extranet\anonymous
if (!Tracker.IsActive)
  return;
Tracker.Current.Session.Identify(Sitecore.Context.User.Name);

// THIS COULD BE A SOLUTION:
if (!Tracker.IsActive)
  return;
if (Sitecore.Current.User.Name.ToLower() == "extranet\\anonymous")
  return;
Tracker.Current.Session.Identify(Sitecore.Context.User.Name);

// OR MAYBE THIS?
if (!Tracker.IsActive)
  return;
if (!Sitecore.Context.User.IsAuthenticated)
  return;
Tracker.Current.Session.Identify(Sitecore.Context.User.Name);

MORE TO READ:

Posted in c#, Sitecore, Sitecore 8 | Tagged , , , , | 2 Comments

Extend the Sitecore FieldRenderer

The Sitecore FieldRenderer is the render controls used to render fields from Sitecore in a way that makes them page editable from the Page Editor (or Experience Editor as it is called in Sitecore 8).

The FieldRenderer has been known since Sitecore 5, and is still the preferred method of rendering fields when using User Controls (Sublayouts):

<%@ Register TagPrefix="sc" Namespace="Sitecore.Web.UI.WebControls" Assembly="Sitecore.Kernel" %>

<sc:FieldRenderer runat="server" FieldName="MyField" />
<sc:Text ID="txtText" runat="server" Field="FlowProfileEulaAndTermsSubTitle" Item="<%# this.DataSource %>"/>

The examples above shows how to use the FieldRenderer from User Controls. The sc:FieldRenderer is the base class, and the sc:Text is a text specific renderer inheriting from the FieldRenderer.

This is an example of an extended FieldRenderer I have been using for the last couple of years. The extension is used to render simple text fields such as headers, text etc. To make this easier we have added a few properties to our FieldRenderer such as:

To create a new FieldRenderer you need to inherit from Sitecore.Web.UI.WebControls.FieldControl.
The important method is protected override void DoRender(HtmlTextWriter output). Inside this method you instantiate your own FieldRenderer control, and voila, your control is Page Editable.

Here is the structure in pseudocode:

using System;
using System.ComponentModel;
using System.Web.UI;
using Sitecore.Collections;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Web;
using Sitecore.Web.UI.WebControls;
using Sitecore.Xml.Xsl;

namespace MyFieldRendererControls
{
  [ParseChildren(false)]
  [PersistChildren(true)]
  public class FieldRendererExt : FieldControl
  {
 
    protected override void DoRender(HtmlTextWriter output)
    {
      string renderValue = "";
      string renderFirstPart = "";
      string renderLastPart = "";
      var fr = new FieldRenderer
        {
		  ...
		  ...
        };

      RenderFieldResult rendered = fr.RenderField();
      renderFirstPart = rendered.FirstPart;
      renderLastPart = rendered.LastPart;
      renderValue = rendered.ToString();


      output.Write(renderFirstPart);
      RenderChildren(output);
      output.Write(renderLastPart);
    }
  }
}

And here is the complete code for the enhanced FieldRenderer:

using System;
using System.ComponentModel;
using System.Web.UI;
using Sitecore.Collections;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Web;
using Sitecore.Web.UI.WebControls;
using Sitecore.Xml.Xsl;

namespace MyFieldRendererControls
{
  [ParseChildren(false)]
  [PersistChildren(true)]
  public class TextExt : FieldControl
  {
    private string after = string.Empty;
    private string before = string.Empty;
    private string cssClass = string.Empty;
    private string cssId = string.Empty;
    private string cssStyle = string.Empty;
    private string enclosingTag = string.Empty;
    private string fieldName = string.Empty;

    [Category("Method"), Description("Always output enclosing tag regardless of it being empty or not")]
    public bool AlwaysEnclosingTag
    { get; set; }

    [Category("Method"), Description("HTML tag to wrap the field value with")]
    public string EnclosingTag
    {
      get { return enclosingTag; }
      set { enclosingTag = value; }
    }

    [Category("Method"), Description("CSS class-attribute on enclosing tag")]
    public new string CssClass
    {
      get { return cssClass; }
      set { cssClass = value; }
    }

    [Category("Method"), Description("CSS style-attribute on enclosing tag")]
    public new string CssStyle
    {
      get { return cssStyle; }
      set { cssStyle = value; }
    }

    [Category("Method"), Description("CSS id-attribute on enclosing tag - will be overridden if control ClientIDMode == Static")]
    public string CssId
    {
      get { return cssId; }
      set { cssId = value; }
    }

    [Category("Method"), Description("FieldName to be rendered from datasource")]
    public string FieldName
    {
      get { return fieldName; }
      set { fieldName = value; }
    }

    [Category("Method"), Description("Disables the page editor for the control")]
    public new bool DisableWebEditing
    { get; set; }

    [Category("Method"), Description("Put some text before")]
    public string Before
    {
      get { return before; }
      set { before = value; }
    }

    [Category("Method"), Description("Put some text after")]
    public string After
    {
      get { return after; }
      set { after = value; }
    }

    [Category("Method"), Description("Set explicit what item to process")]
    public new Item Item
    { get; set; }

    protected override void DoRender(HtmlTextWriter output)
    {
      if (string.IsNullOrEmpty(Field))
      {
        Field = FieldName;
      }

      // GET ITEM
      Item currentContextItem = null;
      try
      {
        currentContextItem = GetItem();
      }
      catch (Exception ex)
      {
        Log.Error("currentContextItem exception", ex, this);
      }

      // RENDER ITEM VALUE
      bool itemValid;
      try
      {
        itemValid = (currentContextItem != null && currentContextItem.Fields[Field] != null);
      }
      catch (Exception)
      {
        itemValid = false;
      }

      string renderValue = "";
      string renderFirstPart = "";
      string renderLastPart = "";
      if (itemValid)
      {
        var fr = new FieldRenderer
        {
          Before = Before,
          After = After,
          DisableWebEditing = DisableWebEditing,
          EnclosingTag = "",
          Item = currentContextItem,
          FieldName = Field,
          Parameters = WebUtil.BuildQueryString(GetParameters(), false)
        };

        RenderFieldResult rendered = fr.RenderField();
        renderFirstPart = rendered.FirstPart;
        renderLastPart = rendered.LastPart;
        renderValue = rendered.ToString();
      }


      // OUTPUT DATA
      if (string.IsNullOrEmpty(EnclosingTag) || (string.IsNullOrEmpty(renderValue) && (!AlwaysEnclosingTag)))
      {
        // Simple value...
        output.Write(renderFirstPart);
        RenderChildren(output);
        output.Write(renderLastPart);
      }
      else
      {
        // Otherwise...
        string attributes = "";

        if (ClientIDMode == ClientIDMode.Static)
        {
          attributes += " id='" + ID + "'";
        }
        else if (!string.IsNullOrEmpty(CssId))
        {
          attributes += " id='" + CssId + "'";
        }

        if (!string.IsNullOrEmpty(CssClass))
        {
          attributes += " class='" + CssClass + "'";
        }

        if (!string.IsNullOrEmpty(CssStyle))
        {
          attributes += " style='" + CssStyle + "'";
        }

        // Wrap it in enclosing tag and attributes
        output.Write("<" + EnclosingTag + attributes + ">");
        output.Write(renderFirstPart);
        RenderChildren(output);
        output.Write(renderLastPart);
        output.Write("</" + EnclosingTag + ">");
      }
    }

    protected override Item GetItem()
    {
      var datasource = GetDatasource();
      if (datasource != null)
      {
        return datasource;
      }

      return Item ?? base.GetItem();
    }

    protected SafeDictionary<string> GetParameters()
    {
      var parameters = new SafeDictionary<string>();

      if (Controls.Count > 0)
      {
        parameters.Add("haschildren", "true");
      }

      foreach (string key in Attributes.Keys)
      {
        string str = Attributes[key];
        parameters.Add(key, str);
      }

      return parameters;
    }

    private Item GetDatasource()
    {
      var layout = GetParent(this);

      if (layout == null)
      {
        return null;
      }

      return string.IsNullOrEmpty(layout.DataSource) ? null : Sitecore.Context.Database.GetItem(layout.DataSource);
    }

    private Sublayout GetParent(Control control)
    {
      if (control.Parent == null)
      {
        return null;
      }

      var sublayout = control.Parent as Sublayout;
      return sublayout ?? GetParent(control.Parent);
    }

  }
}

To use the new control:

<%@ Register tagPrefix="EXT" namespace="MyFieldRendererControls" assembly="MyFieldRendererControls" %>

<EXT:TextExt FieldName="NameOfField" ID="fldTitle" runat="server" EnclosingTag="h1" CssClass="class" CssStyle="color:red" />

Several of my colleagues has worked on this extension, and I would like to thank them all.

 

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

Sitecore 8 EXM Get the email recipient from a Sublayout

The Sitecore 8 Email Experience Manager (EXM) is not only about sending bulk emails. It can be used to send one-time messages like “Thank you for signing up” or “forgot your password?” directly from code.

In the previous versions of EXM, back when it was called ECM, the sublayouts of an email could get the email address of the recipient directly from the querystring parameter “ec_recipient“. But with the introduction of the xDB and the Contacts, things have complicated.

This method will return the username (not the email address) of the recipient of the email:

using System;
using System.Text;
using System.Web;
using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Modules.EmailCampaign;
using Sitecore.Modules.EmailCampaign.Core;
using Sitecore.Modules.EmailCampaign.Factories;
using Factory = Sitecore.Configuration.Factory;

namespace MyEXMService
{
  public class RecipientService
  {
    public string GetRecipientName(HttpRequest request)
    {
      string contactIdValue = request.QueryString[GlobalSettings.AnalyticsContactIdQueryKey];
      string itemId = Context.Request.QueryString["sc_itemid"];
      Item messageItem = Factory.GetDatabase("master").GetItem(ShortID.Parse(itemId).ToID()).Parent;
      Guid messageIdGuid = messageItem.ID.Guid;
      Guid contactId;
      using (GuidCryptoServiceProvider provider = new GuidCryptoServiceProvider(Encoding.UTF8.GetBytes(GlobalSettings.PrivateKey), messageIdGuid.ToByteArray()))
      {
        contactId = provider.Decrypt(new Guid(contactIdValue));
      }
      string contactName = EcmFactory.GetDefaultFactory().Gateways.AnalyticsGateway.GetContactIdentifier(contactId);
      return contactName;
    }
  }
}

With the username in hand, you can either look up the user in the Contacts xDB database, or in the .NET membership database:

RecipientService receipientService = new RecipientService();
string userName = receipientService.GetRecipientName(Request); 
MembershipUser user = Membership.GetUser(userName);

MORE TO READ:

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

Sitecore 8 ServerTimeZone – my dates are all wrong

This issue appeared when I upgraded from Sitecore 7.5 to Sitecore 8. Suddenly my dates on the website were 2 hours behind – on my development server it was one hour behind.

In Sitecore, my date-time was (example) 2015-04-12 00:00:00. This date was presented as 2015-04-11 22:00:00+2.

The issue arises, because Sitecore have implemented a feature that tries to handle local time zones on the server. Old Sitecore 7.5 dates are stored without any timezone information:

  • 20150412T000000

In Sitecore 8, Sitecore have changed the raw data format of the date field by taking the timezone into consideration. Which in my case means that my 2014-04-12 00:00:00 becomes 2015-04-11 22:00:00:

  • 20150411T220000Z

Notice the “Z” at the end? This implies that this is a datetime that have a timezone, and the timezone is now 2 hours behind UTC.

To fix the old datetime fields, I need to tell Sitecore which timezone non-timezone-datetime fields have. This is the setting, ServerTimeZone:

&lt;setting name=&quot;ServerTimeZone&quot; value=&quot;UTC&quot; /&gt;

I also needed to update the Lucene index, as the invalid datetime was added to the index as well.

Thanks to Peter Wind who helped find this issue.

MORE TO READ:

Posted in Sitecore 8 | Tagged , , | 3 Comments

Sitecore Transfer items from one database to another

In this article I will describe how to transfer items from one Sitecore database to another. Usually you transfer content by publishing. But in rare cases this is not an option:

  • You are not moving content from master to web
  • You are not moving content to a publishing target.
  • You are moving content to another path in another database.
  • You would like to avoid raising any events that would clear cache.

Before you start transferring content, you need to know the following:

  • Transfer does not retain the path structure, because a transfer is like a copy, just between databases. If you would like to retain the path, you must do it yourself.
  • The templates of the transferred items must exist in the target database. If not, Sitecore will raise an TemplateNotFoundException.
  • Transferring is done by copying the OuterXML of an item into a string and pasting this string to another database. This could be a very CPU and memory heavy process if you choose to copy the root of a large website.

Enough talk. Here is the code:

using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Data.Proxies;
using Sitecore.Diagnostics;
using Sitecore.Exceptions;

namespace MyNameSpace
{
  public class ArchiveRepository
  {
    public void Put(Item source, Item destination, bool deep)
    {
      using (new ProxyDisabler())
      {
        ItemSerializerOptions defaultOptions = ItemSerializerOptions.GetDefaultOptions();
        defaultOptions.AllowDefaultValues = false;
        defaultOptions.AllowStandardValues = false;
        defaultOptions.ProcessChildren = deep;
        string outerXml = source.GetOuterXml(defaultOptions);
        try
        {
          destination.Paste(outerXml, false, PasteMode.Overwrite);
          Log.Audit(this, "Transfer from {0} to {1}. Deep: {2}", new[]{ AuditFormatter.FormatItem(source), AuditFormatter.FormatItem(destination), deep.ToString() });
        }
        catch (TemplateNotFoundException)
        {
          // Handle the template not found exception
        }
      }
    }

  }
}

Parameter source is the item to copy, parameter destination is the item in the destination database to copy to. Parameter deep determines if you transfer one item of the whole tree.

I created a few more methods to allow for transferring an item from one database to another whilst retaining the path structure. By adding the destination database as private property I hard-code the repository to a specific destination database.

The method Put(item) takes a source item, creates the item path if not found, then transfer the item:

    private readonly Database _database = Factory.GetDatabase("DestinationDatabase");

    public void Put(Item item)
    {
      EnsurePath(item);
      Put(item, true);
    }

    private void Put(Item item, bool deep)
    {
      Item destination = _database.GetItem(item.Parent.ID);
      Put(item, destination, deep);
    }

    private void EnsurePath(Item item)
    {
      foreach (Item ancestor in item.Axes.GetAncestors())
      {
        if (_database.GetItem(ancestor.ID) == null)
          Put(ancestor, false);
      }
    }

MORE TO READ:

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

All my Sitecore items are called __Standard Values – The fix

In the previous post i described how a tiny error in Sitecore made you believe that all of the Sitecore items are called __Standard Values.

The error occurs in the Danish language because Sitecore unintentionally added the word “__Standard Values” to the Display Name of several items.

A friend of mine asked for a fix. Here it is. You should:

  • Create a new .aspx page in the /sitecore modules/shell folder
  • Ensure that the website is running in the Danish (da) language
  • Copy the following contents to the .aspx page you created.
  • Call the page.

The code iterates through all Sitecore templates, identifies the __Standard Values templates where the display name is “__Standard Values” and deletes the contents in the Display Name field.

<%@ Page language="c#" EnableEventValidation="false" AutoEventWireup="true" %>

<script runat="server">
  
  void Page_Load(object sender, System.EventArgs e) 
  {
    Response.Buffer = false;
    Response.BufferOutput = false;
    Response.Write(string.Format("{0} {1}<br/>", Sitecore.Context.ContentDatabase.Name, Sitecore.Context.Language));
    Sitecore.Data.Items.Item templateRoot = Sitecore.Context.ContentDatabase.GetItem("/sitecore/templates");
    Iterate(templateRoot);
  }

  void Iterate(Sitecore.Data.Items.Item root)
  {
    foreach (Sitecore.Data.Items.Item child in root.GetChildren())
    {
      if (child.Name == "__Standard Values" && child["__display name"] == "__Standard Values")
      {
        Response.Write(string.Format("path: {0}. Name: {1}. Display Name: {2}<br/>", child.Paths.FullPath, child.Name, child["__display name"]));
        FixDisplayName(child);
      }
      if (child.HasChildren)
        Iterate(child);
    }
  }

  void FixDisplayName(Sitecore.Data.Items.Item item)
  {
    item.Editing.BeginEdit();
    item.Fields["__display name"].Value = string.Empty;
    item.Editing.EndEdit();
  }

</script>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
  <head>
    <title>www.sitecore.net</title>
    <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    <link href="/default.css" rel="stylesheet">
  </head>
  <body>
    <form runat="server">
    </form>
  </body>
</html>

As always, use at own risk, take a backup before running etc.

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

All my Sitecore items are called __Standard Values

After I upgraded to Sitecore 7.5, all of my Sitecore items are called “__Standard Values“. This happens in the “Danish” language, not the “English”:

All items are called __Standard Values

All items are called __Standard Values

Don’t worry, your items have not been renamed. Sitecore have just made a tiny mistake, and added the phrase “__Standard Values” to the “Display name” of all __Standard Values:

Display Name is wrong

Display Name is wrong

  • Find the template
  • Find the __Standard Values of the template
  • Click View|Standard Fields
  • Find the field “Display name”
  • Delete the text “__Standard Values” in the field
  • Repeat for all __Standard Values that contains a text in the “Display name” field.

UPDATE:

I made an .aspx page that fixes the issue. Get the contents here.

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

Cannot cache phrase for invariant language – Sitecore 8

When adding dictionary items to the CORE database in Sitecore 8, this error can occur:

Message: Cannot cache phrase for invariant language.
Source: Sitecore.Kernel
at Sitecore.Globalization.Translate.CachePhrase(String key, String phrase, Language language, DictionaryDomain domain)
at Sitecore.Globalization.ItemEventHandler.OnItemSaved(String dictionaryKey, Item item, Boolean reloadDomainCache, Boolean saveToDisk)
at … … … …

The error is related to a cached dictionary file. Sitecore caches all dictionary entries in a dictionary.dat file located in the /temp/ folder.

To solve the issue you should:

  • Delete the /temp/dictionary.dat file
  • Recycle the website

That’s it. Problem solved. Thanks to Sitecore Support for the solution.

Posted in Sitecore 8 | Tagged , | Leave a comment

Sitecore 8 and Engagement Plans

With the introduction of the xDB in Sitecore 7.5, Sitecore also changed the analytics API. The VisitorManager have been replaced by the Tracker.

The Tracker is one of the base API’s for Sitecore the new Sitecore Experience Platform (the new name for Sitecore DMS, which was the new name for Sitecore OMS – do you follow me?) and the API handles the tracking of users in Sitecore.

For automatic engagement plan handling, this means that instead of using the VisitorManager to enroll users in your engagement plan, you use the Tracker and the AutomationStateManager:

using System.Linq;
using Sitecore.Analytics;
using Sitecore.Analytics.Automation.Data;
using Sitecore.Analytics.Automation.MarketingAutomation;
using Sitecore.Data.Items;

public void AddUserToEngagementPlan(string user, Item engagementPlan)
{
  Tracker.Current.Session.Identify(user);
  AutomationStateManager manager = Tracker.Current.Session.CreateAutomationStateManager();
  manager.EnrollInEngagementPlan(engagementPlan.ID, engagementPlan.Children.First().ID);
}

Parameter user is the complete username with domain (for example extranet\bp). The engagementPlan is the engagementPlan item.

The remove a user from an engagement plan you simply call RemoveFromEngagementPlan:

public void RemoveUserFromEngagementPlan(string user, Item engagementPlan)
{
  Tracker.Current.Session.Identify(user);
  AutomationStateManager manager = Tracker.Current.Session.CreateAutomationStateManager();
  manager.RemoveFromEngagementPlan(engagementPlan.ID);
}

Thanks to Alin Parjolea for the code.

MORE TO READ:

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