Blog

  • Web API routes failing after updating to 8.1 Update-3

    After updating to 8.1 Update-3 from 8.1 Initial Release our custom Web API routes on CM and CD were failing with the following exception:

    (more…)

  • Default Rich Text Editor Profile in a Multisite Environment

    Out of the box, the default Rich Text Editor profile can be set via the “HtmlEditor.DefaultProfile” setting.

    <setting name="HtmlEditor.DefaultProfile" value="/sitecore/system/Settings/Html Editor Profiles/Rich Text Default"/>
    

    Unfortunately, this only allows you to set the default profile for the entire Sitecore instance making it the default profile for each site.

    Set it up for Multisite

    Luckily, it is possible to create a custom EditorConfiguration which reads an attribute from Site Settings and replaces the default profile:

    Add the following config file and set correct namespaces for HtmlEditor.DefaultConfigurationType:

    Edit sites config and point to your preferred rich text editor profile:

    <site name="mySite" defaultRichTextProfile="/sitecore/system/Settings/Html Editor Profiles/Rich Text Full" ...
    

    Tested on 8.0 and 8.1.

    UPDATE 04.03.2016: Changed code to only override the default profile. If a different profile was defined at field level it will not be overridden.

  • Querying generated Template Models with Glass

    I have written some helper methods for working with generated Template Models and Glass.Mapper. Main goal is to make it easy for developers to query the Sitecore database using generated Template models.

    See Mike’s post for details on what Template Models are.

    Examples

    Let’s start with this fictional content tree:

    - Home (template: Home Page --> has Page as base template)
    -- Page 1 (template: Page)
    -- Page 2 (template: Page)
    ---- Sub Page 1 (template: Page)
    ---- Sub Page 2 (template: Page)
    ---- Data (template: Data folder)
    -- News Page (template: News Page --> has Page as base template)
    ---- News Entry 1 (template: News Detail Page --> hase News Page as base)
    ---- Data (template: Data folder)
    -- Page 3 (template: Page)
    -- Search Page (template: Search Page --> has Page as base template)
    -- Data (template: Data folder)
    

    Now, let us get the home node using glass:

    var homeItem = new SitecoreContext()
        .GetItem<IHomePageTemplate>("/sitecore/content/Home");
    

    The IHomePageTemplate interface has been generated using TDS or any other tool of choice. It implements the IGlassBase interface.

    Now, let’s get all the sub pages:

    var subItems = homeItem.Children<IPageTemplate>();  
    // Returns: Page 1, Page 2, News Page, 
    // Page 3, Search Page (all templates that inherit from PageTemplate)
    

    Now, let’s only get the news page(s):

    var subItems = homeItem.Children<INewsPageTemplate>(); 
    // Returns: News Page (as enumerable)
    

    Now, let’s get only the search page:

    var theSearchPage = homeItem.FirstChild<ISearchPageTemplate>(); 
    // Returns: Search Page (as model implementing ISearchPageTemplate)
    

    Let’s get all pages needed to build a page navigation:

    var subItems = homeItem.Descendants<IPageTemplate>(); 
    // Returns: Page 1, Page 2, Sub Page 1, 
    // Sub Page 2, News Page, News Entry 1, Page 3, Search Page
    

    Because IEnumerable is used, you can also go fancy with LinQ like this:

    var subItems = homeItem.Descendants<IPageTemplate>()
        .Where(p => !p.HideFromNavigation); 
    // Returns all items which inherit from Page 
    // and don't have the HideFromNavigation field set. 
    // (Assuming your Page Template has a HideFromNavigation checkbox field)
    

    Build a breadcrumb:

    var items = new SitecoreContext()
                    .GetCurrentItem<IPageTemplate>()
                    .Ancestors<IPageTemplate>()
                    .Where(p => !p.HideFromBreadcrumb);
    
    // (Assuming your Page Template has a HideFromBreadcrumb checkbox field)
    

    If you don’t like to use extension methods, you can also use the helpers in a more DI-friendly way:

    ITemplateModelHelper<IGlassBase> helper = new GlassTemplateModelHelper(new SitecoreContext());
    
    var children = helper.Children<ISomeTemplate>(myItem);
    

    Details

    The helpers use a custom attribute called TemplateModelHelper. It needs to be included in code generation templates to let the helpers know which interface maps to which templateID and also to handle template inheritance. See the documentation on GitHub for details how to set up.

    Example of a generated interface:

    [SitecoreType(TemplateId=IMyTemplate.TemplateIdString)] // , Cachable = true
    [TemplateModelHelper(TemplateId="076616fe-123f-443d-b627-ff4c1da8df57",BaseTemplates="453e35fc-46c5-46b3-a447-141c103f9989,0b34a6eb-d5b6-4d59-b5f9-3ce0bcb13fdd")]
    public partial interface IMyTemplate : IGlassBase, IMyBaseTemplate, IMyOtherBaseTemplate
    {
    }
    

    Performance

    The helper methods introduce as little overhead as possible and have been tested for performance. By making sure, only the relevant items (=matching TemplateId) are mapped to template models, the helpers are very efficient.
    The mapping Interface => TemplateID => Child Templates is done once and stored in memory as long as the application lives.

    Get the code

    If you like this way of querying Sitecore Items, feel free to grab the code from GitHub. Any feedback and improvements are most welcome.

  • Glass Mapper: Does the number of mapped fields have a performance impact?

    Recently at work, the discussion arose if the number of fields mapped from Sitecore Items to our models would have a performance impact. The question was if we should limit the number of mapped properties in our IGlassBase interface to gain performance.

    I did a simple test by mapping an item to a test interface (IGlassBaseTest) and measuring the execution time of these lines 50’000 times.

    stopwatch.Start();
    var item = context.GetItem<IGlassBaseTest>("/sitecore/content/GlassTest");
    var theID = item.Id;
    stopwatch.Stop();
    

    Test 1: Bare minimum IGlassBaseTest

    public interface IGlassBaseTest
    {
    [SitecoreId]
    Guid Id { get; }
    }
    

    Total time for 50’000 cycles: 34ms

    Test 2: Extended Interface

    I’ve added the [SitecoreItem] attribute guessing it might have an impact as it needs to map the entire Sitecore Item to a property.

    public interface IGlassBaseTest
    {
    [SitecoreId]
    Guid Id { get; }
    
    [SitecoreItem]
    Item Item { get; }
    }
    

    Total time for 50’000 cycles: 39ms

    Considering the large amount of cycles, the 5ms difference is irrelevant. In some measurements the Test 2 scenario was actually even faster than Test 1.

    Test 3: Fully blown interface

    And now for the whole thing.

    
    public interface IGlassBaseTest
    {
    [SitecoreId]
    Guid Id { get; }
    
    [SitecoreInfo(SitecoreInfoType.FullPath)]
    string FullPath { get; }
    
    [SitecoreInfo(SitecoreInfoType.DisplayName)]
    string DisplayName { get; }
    
    [SitecoreInfo(SitecoreInfoType.Name)]
    string Name { get; }
    
    [SitecoreInfo(SitecoreInfoType.Language)]
    Language Language { get; }
    
    [SitecoreInfo(SitecoreInfoType.TemplateId)]
    ID TemplateId { get; }
    
    [SitecoreInfo(SitecoreInfoType.TemplateName)]
    string Template { get; }
    
    [SitecoreInfo(SitecoreInfoType.Url)]
    string Url { get; }
    
    [SitecoreChildren(IsLazy = true)]
    IEnumerable Children { get; }
    
    [SitecoreParent(InferType = true, IsLazy = true)]
    IGlassBase Parent { get; }
    
    [SitecoreItem]
    Item Item { get; }
    }
    

    Total time for 50’000 cycles: 49ms

    Conclusion

    While the first two tests didn’t show a significant difference, the last test adds roughly 10ms to our total. But remeber, that’s only 0.0002ms per execution which can also be considered insignificant in my opinion.
    Based on these tests I can say, that the number of fields mapped by Glass Mapper only has a negligible impact on performance.

  • Experiences with Glass Cache

    In version 4, Glass Mapper introduced a caching mechanism to speed up access to mapped objects. Basically, once an object is mapped, it is stored in Glass Cache which by default utilizes HttpCache in Memory. The next time the same object is mapped, it will be returned from the cache instead of being read from the Sitecore database which in many cases speeds things up. The cache is cleared by publishing.

    glass cache

    Caveats

    When reading things like “Glass has no performance issues anymore, they have caching now” made me want to check this out for myself. After all we would be introducing yet another caching layer to the several existing layers Sitecore already has.

    I have evaluated the current implementation of Glass cache for the use in a large multisite solution with dedicated authoring and delivery instances. Models were being generated on the fly using TDS. In this scenario, I have found some shortcomings of the current Glass Cache implementation:

    ·         Glass Cache cannot be globally enabled / disabled via config

    ·         Enable / Disable cache on a model can only be done using code

    Glass Cache cannot be globally enabled / disabled via config

    On authoring instances, we wanted to globally disable caching. This is because authors were used to seeing what they edited in “normal” mode without having to publish.
    Unfortunately, there is currently no safe way to globally disable Glass cache. An option would be to use the following code on application startup:

     // Caution: this doesn’t globally disable cache
     new DisableCache();

    Unfortunately this leads to the issue that caching is re-activated in some cases when DisableCache() is used multiple times. For details see this ticket.

    Update 06.11.2015:
    According to Mike Edwards this issue is fixed and the fix will be part of next release of Glass Mapper.

    Cacheable=true / false only via code

    In this particular scenario, when generating models using TDS, we basically only have the option of enabling caching for ALL models or NONE at all. (add annotations in the T4 Templates)

    Some more control over which Templates should be cached would be great. This can probably be accomplished by reading a property of the model prior to adding it to cache.

    [SitecoreType(TemplateId=IMyTeaserTemplateConstants.TemplateIdString, Cachable = true)]
     public class MyTeaserTemplate : GlassBase, IMyTeaserTemplate 
     {
       ...

    Speed testing!

    I ended up testing several components built with Glass comparing loading times, finding that the difference between cached and uncached was relatively small. (Glass Cached components needed roughly 8% less time than uncached ones in my test). Of course, when accessing items very often i.E. hundreds of times in a row, Cached will be considerably faster but in a “day-to-day scenario”, i.E. a link list component with 10-20 links, the difference is insignificant in my opinion.

    Sitecore HTML Cache for the win

    When switching on Sitecore’s HTML Cache on a component, there is no difference between Glass Cached and uncached anymore. This is because the component’s code isn’t executed anymore on a cache hit, making the performance gain through Glass Cache even more insignifficant.

    Conclusion

    I love to use Glass Mapper especially since V4, but I currently don’t advise using Glass Cache. After all, Glass is already quite fast without it! I rather make use of Sitecore’s tried and tested HTML cache feature to speed up my components.

    Any thoughts on this are highly appreciated. Feel free to comment or send me a message.

  • Custom FxCop Ruleset for Sitecore

    Recently, my Co-Worker Daniel and I were allowed to present our way of ensuring code-quality using Sonar and custom Sitecore FxCop rules at the first Swiss Sitecore User Group.

    Our solution is based on the shared source module of Johannes Zijlstra to which we now were allowed to contribute our changes. Thanks for that!

    If you want to learn more about how to use custom FxCop rules for Sitecore, check out:

    http://trayek.wordpress.com/2014/01/07/sitecore-and-fxcop/

    The source code can be found on BitBucket:
    https://bitbucket.org/jzi/sitecore.fxcop/overview

  • Failing DNS lookups slowing down site after upgrade

    I ran into a problem today while setting up a content delivery server after upgrading from Sitecore 6.4 to 7.2. After setting everything up, the site ran very slowly and the Sitecore Log started filling with exceptions like this:

    System.Net.Sockets.SocketException: The requested name is valid, but no data of the requested type was found
    ...
    Sitecore.Web.WebUtil.GetHostIPAddress()

    When looking into what happens inside GetHostIPAddress() I found out that Sitecore calls this method to resolve the IP from the host name on each request.

    Normally not a problem but, in this setup, the CD running behind a load balancer couldn’t resolve the domain name it was serving. That way DNS lookup timed out on each request. By adding the domain names of the site with the local IP to the hosts file, I could resolve the problem and the site was immediately working at full speed again.

    # example hosts file entry
    127.0.0.1 www.mydomain.com

    By the look of it, older versions of Sitecore didn’t do this kind of DNS resolving on each request. That’s why you should keep this in mind when upgrading CD servers.

  • Web Forms For Marketers meets Sitecore DMS

    Web Forms For Marketers meets Sitecore DMS

    I’ve had the chance lately to dive into some more advanced features of the Web Forms for Marketers Module in combination with Sitecore DMS. This blog post shows some of the advanced possibilities you have using WFFM in combination with DMS.

    Tagging

    enable-tagging
    Activate the Tag checkbox on any field.

    A neat feature you have in when working with WFFM and DMS is Tagging. You can enable the Tag checkbox on any field. As soon as a user fills out the form, a Tag will be written to the User’s DMS profile. The data inside the tags can then be used in reports or you can auto-fill tag values into a form.

    Note: The tag name is defined by the item name of the form field.

    Some pre-defined tags are already built into reports:

    • Email
    • First Name
    • Second Name
    • Company
    • Organization
    • Full Name
    • StateProvince
    tags-on-reports
    Visitor Tags in DMS Reports.

    If you name your form fields (Item Name) accordingly and set the Tag checkbox, the data of the user will be displayed on reports making it easier to identify your visitors once they’ve filled in a form.

    It is also possible to add custom tags to reports. Explaning how to do this would go too far for this post but you can check out this Stackoverflow post where I described how to do so.

    You can also use tag values to pre-populate your form fields. This can be done using field rules:

    prefill-fields
    Populate field using tag value.
    form-reports
    View Form Reports

    Reporting / Analytics

    The feature I personally find most valuable is the inbuilt analytics. In Form Designer, select your form and then go to “Form Reports”.

     

    Analyze user behaviour to qualify leads

    visit-details
    User’s visit context data when viewing a form entry.

    In Form Reports, you double click a specific form entry which will open the Visit Detail report for the specific visitor who filled out the form. The report provides valuable information about the user’s session like Scoring information, visited pages, time spent on each page and every page event registered during the visit.

    This is a whole new way of receiving form requests. Now, you can not only see the field values filled in by the user, you have the entire context of his visit. This can greatly help when qualifying leads i.E. if you know a customer has looked at specific products before requesting a quote, you might be able to tailor the quote accordingly.

    Dropout reporting

    When dropout reporting is activated, you can also analyze incomplete form requests. What WFFM does is send form data to the server via AJAX every time you enter data into a field in the form. That way you can see values the user filled in even if he didn’t submit the form. This can help you optimize your form design or even follow up on an incomplete request. Personally I’m not a big fan of this feature in terms of privacy.

    Goals / Engagement Value

    In Form Designer, you can register a goal when a user has submitted a form. You can also assign engagement points to goals. Usually a form submission means some kind of engagement/conversion so it makes sense to apply engagement value. This value can be used to measure the effectiveness of campaigns or A/B testing.

    Customizing fields using rules

    field-rules
    Field Rules

    As mentioned earlier in this post, you can define rules for each field of your form. It is possible to hide form fields if certain criteria matches (Profile value, Campaign triggered, Geo-Location,…). If you want to hide a field by default and only show it, when your rule matches, use a  except where condition.

     

    Summary

    Web Forms for Marketers adds a lot of value for you when you are using DMS. Especially the possibility of seeing details about the user’s visit context (visited pages, time spent on pages, triggered goals, profile scoring, geo location,… ) can help to qualify leads and is a real plus for Sitecore DMS with WFFM compared to other form engines and analytics tools like WooForms and Google Analytics.

  • Duplicate entries in custom index on incremental update

    I’ve had a problem with a custom index some time ago. Entries were showing up multiple times when doing incremental updates of the index i.E. when saving an item. The only thing that would clean up the index again would be a full index rebuild.

    The reason for this happening was, on incremental updates, Sitecore would add a new document to the index and leave the old one untouched thus duplicating the entry. The default indexes (core,master,web) would not show the same behaviour which made me check each and every field of the default fieldMap configuration… and voilà: The _uniqueid field needs to be converted to lowercase in order to be matched correctly. (GUIDs are saved as lowercase in lucene but are uppercase in .NET) To resolve the issue, all I needed to do is add the _uniqueid field to the fieldNames configuration as follows:

    I also added the _template field to ensure filtering by template ID works correctly. You generally need to do this with every GUID field you’re querying.

    Unfortunately, there are still several custom index tutorials out there which don’t take this into account. If you’re looking for a clean template to start a new custom index, check out Mikkel’s post.

  • Understanding Sitecore DMS Campaign triggering

    Understanding Sitecore DMS Campaign triggering

    campaign on landing pageSitecore DMS allows you to launch and track Campaigns. You can set up Campaigns inside Marketing Center. Check the Marketing Operations Cookbook on SDN for more detail on that. Make sure you deploy your Campaigns by clicking Deploy in the Review ribbon afer setting them up. Now your Campaigns are ready to be triggered.

    Campaigns can be triggered…

    • via URL parameter ?sc_camp=myCampaignId
    • via Landing page (see screen shot)

    Every time a Campaign is triggered, the triggerCampaign pipeline runs. Here, rules are executed, automation is run and the Campaign page event is registered.

    If you want to access the Campaign for the current Visit, use:

    var currentCampaignId = Tracker.CurrentVisit.CampaignId;
    

    How Campaigns work together with Visits

    What is a bit hard to understand at first is, how Campaigns play along with DMS Visits. It is important to know that by default, Campaigns are associated to visits only if

    • no other Campaign has been associated with the visit yet
    • the page view is the first page of the current visit (Landing Page).

    To clarify this, here’s what happens inside the StartTracking pipeline:

    if (Tracker.CurrentVisit.IsCampaignIdNull() && Tracker.CurrentVisit.IsOnLandingPage())
    {
        Tracker.CurrentVisit.CampaignId = campaign.ID.ToGuid();
    }
    ... run the triggerCampaign pipeline

    This makes debugging a bit complicated because you need to clear cookies and make sure you trigger the Campaign on the first page you’re opening. But in most real world scenarios it will work well. (i.e. leading customers to your site through social media, landing pages,…)

    Modifying the way Campaigns are associated to Visits

    But what if you would want to be able to trigger a Campaign during a Visit (not only on the landing page) or if you would want to be able to override a previously assigned campaign?
    This is possible. You need to hook into the triggerCampaign pipeline and set the Visit’s Campaign ID when a Campaign gets triggered:

    Keep in mind that this will affect your Campaign reporting. Visits assigned to Campaign A and then re-assigned to Campaign B won’t be shown on the stats for Campaign A anymore. The engagement value earned during Campaign A will then show up on stats for Campaign B.

    I hope this gets you started with Sitecore DMS Campaigns.