CategorySitecore

Sitecore Multisite – Generating out of context links

Linking to items in other sites gives a lot of problems and strange urls.
Sitecore creates links by using the context of the current site.
There is an easy solution for creating out of context links, just switch the context!

First we need to find the site context of the item. The Sitecore.Configuration.Factory gives us access to all available sites.

var sites = Sitecore.Configuration.Factory.GetSiteInfoList();

Now we need to find the corresponding site.

SiteContext siteContext  = null;
foreach (var site in sites)
{
    var fullPath = string.Concat(site.RootPath + site.StartItem);
    if (item.Paths.FullPath.StartsWith(fullPath))
    {
        siteContext = new SiteContext(site);
        break;
    }
}

Now that we’ve got the site context, we can generate the correct url.

var urlOptions = LinkManager.GetDefaultUrlOptions();
urlOptions.Site = siteContext;
var url = LinkManager.GetItemUrl(item, urlOptions);

Sitecore’s sublayout datasource, brilliant! Or maybe not?

One of the strengths of Sitecore is the DMS (Digital Marketing System). DMS collects user information and allows you to personalize the website based on the user’s profile.

One of the possible personalizations is changing the content of a sublayout by changing its datasource.

This sounds great, but sitecore made one big design flaw!

The items in the datasource are not stored by ID, but by the selected items path! This means that changing one of the item names in the tree of the selected item (or the selected item name itself) will break the datasource. Moving the item will also break the datasoure. Beware of that!!!

Sitecore announced to fix this in a future version.

I’m also not very happy about the way you have to retrieve the datasource item:

Item item;
string datasource = ((Sitecore.Web.UI.WebControls.Sublayout)this.Parent).DataSource;
if (!string.IsNullOrEmpty(datasource) {
    item = Sitecore.Context.Database.SelectSingleItem(datasource);
}

Sitecore user serialization

A colleague gave me an excel file with usernames, password and profile information and asked me if I could import them in Sitecore.
After writing an import tool (read about reading excel files here), I thought to be clever and used Sitecore serialization to import all users to the test environment.
Serialization works quite well. It creates a user file for every user (in the data\serialization\security folder) and even the roles can be serialized.
I copied these files to the test environment and started the deserialization process. All sitecore users were visible in sitecore, but none of the users where able to login.
That’s when I found out that passwords are not serialized!
So remember! Serialization of users looks nice, but you do need to create new passwords!