Monday, September 14, 2015

How to Fix Missing Conditions in Engagement Plan Designer After Upgrading to Sitecore 8.x

Standard

Then and Now

Over the course of the last year, we upgraded a client's Sitecore instance from 6.6 to 8.0 Update-2.  I am pretty sure that we were probably the first in the country, if not the world, to get our client running on xDB Cloud.

There were definitely some bumps in the road, but things have been running pretty smoothly for us, and we have been starting to really see the benefits of the new experience platform.



Look Ma, No Conditions!

It wasn't until recently, when we started developing some pretty fancy engagement plans, that we discovered a new issue - when trying to set rules on Engagement Plan conditions, the dialog was completely empty!



Detective Mode Enabled

Lars and I started digging around the tree, and we discovered a legacy Marketing Workflows folder living in the rules location: /sitecore/system/Settings/Rules.


Digging deeper, we discovered that the Engagement Plan Condition Rule's Source field was pointing to one of the Marketing Workflows folders located at /sitecore/system/Settings/Rules.


We knew that we were onto something here, so I checked the links of the legacy Marketing Workflows folder. Presto! I discovered that the Engagement Plan Condition Rule's Source field was linked to it.



Out With the Old, In With the New

At this point, I decided to take an invasive approach by deleting the legacy folder. When I clicked the delete button, the Breaking Links dialog popped up. I selected "Link to another item" in the action options as shown below:



Next, I pointed the item that was linked to it to the proper Marketing Workflows folder:



Lift Off!

After changing the link to point to the proper Marketing Workflows folder, and removing the old folder, the rule set editor dialog was working again:


Thanks to Lars for helping get this issue resolved.

Monday, August 31, 2015

Setting up Sitecore's Geolocation Lookup Services in a Production Environment

Standard

Background

We have been working with Sitecore’s Business Optimization Services (SBOS) team for quite some time, helping one of our client's stretch the legs of the Experience Platform.

One of the tasks on the list included setting up Sitecore's Geolocation Service so that we could personalize based on the visitor's location. The SBOS team had some pretty slick rules set up on the Home Page of a site, where they switched out carousel slides and marketing content spots based on the visitor's location.


Sitecore Geolocation Lookup Service and MaxMind

There have been some changes with regards to the Geolocation / MaxMind set up because Sitecore launched IP Geolocation directly to their customers via the Sitecore App Center instead of going through MaxMind to use their service. Here is a link to Sitecore's documentation site that contains set up instructions, and how to migrate from MaxMind if purchased and have been working directly with them in the past:
https://doc.sitecore.net/Sitecore%20Experience%20Platform/Analytics/Setting%20up%20Sitecore%20IP%20Geolocation

Setup

After sifting through the documentation, we highlighted the following steps that needed to be completed in order to get up and running, and validate that things were indeed working:

  1. Download and install the Geolocation client package from https://dev.sitecore.net/Downloads.aspx
  2. Enable ALL configuration files in the CES folder
  3. Whitelist geoIp-ces.cloud.sitecore.net and discovery-ces.cloud.sitecore.net
  4. Test personalization
NOTE: Depending on your firewall, you may only have the option to whitelist by IP address. If this applies to you, you will need obtain the list of Azure IP addresses from the following link: Azure Datacenter IP Address Ranges. This is what happened to us, and I call tell you that the list is looooooooooooooong!!! Your network guy or gal will hate you!

Unfortunately for us, there was one piece of configuration that isn't documented. It so happened to be one of the most important pieces.

You will know what I am referring to as you read further along.

Testing

With all the pieces in place, we started testing our personalization.

Things worked beautiful on our Staging Server, but Production was a non-starter! So, as good detectives, we started our troubleshooting by looking at the differences between Staging and Production.

Load Balancer / Firewall / CDN woes

Our client uses Incapsula to protect their production websites. It does a great job protecting and caching their various site's to ensure optimal performance. It has however given us some grey hairs in the past when dealing with Sitecore's Federated Experience Manager. But, that's a story for another time.

The Incapsula CDN was the main difference between Staging and Production.

After running several tests with Fiddler and capturing packets using Wireshark, we were able to the determine that the Geolocation service was not obtaining the visitor's actual IP address. Instead, it was passing along Incapsula's IP address.

The reason for this was identified in the CreateVisitProcessor within the CreateVisits analytics pipeline. As you can see below, it was passing over the Request.UserHostAddress value.


This doesn't work when you are behind a load balance or proxy, as described by this article: http://stackoverflow.com/questions/200527/userhostaddress-gives-wrong-ips

Digging further, we discovered another interesting processor in the CreateVisits pipeline called XForwardedFor. Aha! As we know; the "...header field is a de facto standard for identifying the originating IP address of a client connecting to a web server through an HTTP proxy or load balancer."

Looking at the code below, you will notice that it's pulling in a setting, and if it's not empty, it is used as the key to obtain the value from the request's header NameValueCollection.



After digging around, and talking to support, we discovered the config file named Sitecore.Analytics.Tracking.config and setting below:

 <!-- ANALYTICS FORWARDED REQUEST HTTP HEADER  
    Specifies the name of an HTTP header variable containing the IP address of the webclient.  
    Only for use behind load-balancers that mask web client IP addresses from webservers.  
    IMPORTANT: If this setting is used incorrectly, it allows IP address spoofing.  
    Typical values are "X-Forwarded-For" and "X-Real-IP".  
    Default value: "" (disabled)  
 -->  
 <setting name="Analytics.ForwardedRequestHttpHeader" value="" />  

Light at the end of the tunnel

After setting the value to "X-Forwarded-For" as shown below, the magical Geolocation based personalization started working like a champ!

<setting name="Analytics.ForwardedRequestHttpHeader" value="X-Forwarded-For" />

NOTE: We discovered that casing matters when setting the value. "X-FORWARDED-FOR" will NOT work. It needs to be set exactly like I have it above. For more information on this, you can read this Stack Overflow article:
http://stackoverflow.com/questions/11616964/is-request-headersheader-name-in-asp-net-case-sensitive


I hope that this information helps make your Sitecore IP Geolocation configuration go smoothly for your environment!

A special thanks to Kyle Heon from Sitecore for his support through this process.

Sunday, August 2, 2015

Mastering Sitecore MVC Forms : CRUD that works on both Management and Delivery servers

Standard

The Use Case

This is a very common scenario; you need to build a form that has the ability to create or update items in Sitecore after a user has input data and clicked the submit button.

This sounds pretty straightforward right? Well, it gets a bit tricky when you don't have direct access to the master database. For example, if you are trying to perform CRUD operations on a security hardened content delivery server, you don't have access to the master database, and this will fail.

Most folks will create a new web service on their management server that they could consume to handle this. A good option, but something extra to build and maintain.

In this post, I am going to show you how to make your form logic "smart" enough to be able to create or update items even when they are posting from content delivery servers.

The Form

Let's start out by looking at a simple form where we ask a user to input some basic information so that they can receive a reminder about an event. We intend to use this data to create an item in the master database so that a scheduled task running in the management instance will determine when to email them to remind them about the event.



The Model

The model is pretty simple. As you can tell, the class' properties almost match the form's input.

1:  public class ReminderViewModel : ModelBase<Item>  
2:  {  
3:      public ReminderStrings FormStringsAndLinks { get; set; }  
4:      public Event RemindEvent { get; set; }  
5:      public string EventId { get; set; }  
6:      public string FirstName { get; set; }  
7:      public string LastName { get; set; }  
8:      public string Email { get; set; }  
9:      public bool Consent { get; set; }  
10: }  

FormsStringsAndLinks and ReminderEvent are complex properties used to display some form input strings, links and the event information as shown above.

The Client Script

We are using AJAX to post our form. The following JavaScript snippet is an AngularJS / jQuery mix where there is a validation check on the form after the user clicks the schedule reminder button. If it's valid, we build a view model object, and post it to the SubmitReminder action in my Forms controller.

1:  if (isValid) {  
2:        var reminderViewModel = {};  
3:        reminderViewModel.firstname = $scope.txtreminderfirstname;  
4:        reminderViewModel.lastname = $scope.txtreminderlastname;  
5:        reminderViewModel.email = $scope.txtreminderemail;  
6:        reminderViewModel.eventId = jQuery("#hdnEventId").val();//Angular doesn't like hidden fields with guids  
7:        reminderViewModel.consent = $scope.chkreminderconsent ? $scope.chkreminderconsent : false;  
8:        $http.post("/api/mysite/forms/SubmitReminder", reminderViewModel )  
9:          .success(function () {  
10:            jQuery("#reminder-app").slideUp("slow");  
11:            jQuery("#reminder-set").removeAttr("style");  
12:          })  
13:          .error(function () {  
14:            alert("An error occurred while adding you to our reminder list.");  
15:          });  
16:      }  

The Submit Reminder Action

The action method takes the model object, and then passes it to a business layer method in line 5. If it is successful, it will use some key values to identify the contact in xDB. The xDB piece is beyond the scope of this post.

1:  [HttpPost]  
2:      public bool SubmitReminder(ReminderViewModel reminderViewModel)  
3:      {  
4:        var reminderManager = new ReminderManager();  
5:        var success = reminderManager.CreateEventReminder(reminderViewModel);  
6:        if (success)  
7:        {  
8:          var identifiedContact = new IdentifyContact  
9:          {  
10:            FirstName = reminderViewModel.FirstName,  
11:            LastName = reminderViewModel.LastName,  
12:            Email = reminderViewModel.Email  
13:          };  
14:          ExperienceProfile.ContactIdentifier = reminderViewModel.Email;  
15:          ExperienceProfile.UpdateProfile(identifiedContact);  
16:        }  
17:        return success;  
18:      }  

Nothing special happening in the business layer method. It is strictly a wrapper around the repository in this case.

1:  public bool CreateEventReminder(ReminderViewModel reminderViewModel)  
2:      {  
3:        return _eventRepository.CreateEventReminder(reminderViewModel);  
4:      }  

Handling the CRUD

Config Settings

In our set up, we have an element where we define the application instance type, default database and the url for the management instance.

We also have other config nodes that help to dynamically switch the Solr or Lucene indexes that we are querying against based on the environment we have set. We won't get too much into this, but it's basically a static configuration class that we access within our query methods.

1:  <mysite.com  
2:     applicationInstance="management"  
3:     defaultDomain="mysite.com"  
4:     defaultDatabase="master"  
5:     siteName=""  
6:     managementInstanceUrl="http://cm.mysite.com">  
7:     <indexes>  
8:      <index name="buckets" management="sitecore_master_index" delivery="sitecore_web_index" />  
9:      <index name="products" management="commerce_products_master_index" delivery="commerce_products_web_index" />  
10:     <index name="media" management="scene7_media_master_index" delivery="scene7_media_web_index" />  
11:    </indexes>  
12: </mysite.com>  

On a delivery server, the applicationInstance would be set to "delivery" and the defaultDatabase to "web".

Create Event Reminder

The first method that gets accessed in our repository is CreateEventReminder, as shown above in the quick snippet from the business code.

1:  public bool CreateEventReminder(ReminderViewModel reminderViewModel)  
2:      {    
3:        return Settings.ApplicationInstance == InstanceType.Management ? CreateEventReminderMaster(reminderViewModel) : PostEventToManagement(reminderViewModel);  
4:      }  

Line 3 is a ternary that determines if we are on the delivery or management instance, and either passes the model object to the CreateEventReminderMaster method if we are on the management server, or to the PostEventToManagement method if we are on the delivery server.

Creating the Item in the Master Database

The CreateEventReminderMaster method below, as it's name indicates, creates items in the master database. There is a check to determine if the item already exists in the database, before creating it. Nothing really fancy here.


1:  public bool CreateEventReminderMaster(ReminderViewModel reminderViewModel)  
2:      {  
3:        //We are on the management server  
4:        var masterService = new SitecoreService("master");  
5:        var destinationFolder = masterService.Database.GetItem(new ID(Sitecore.Configuration.Settings.GetSetting("EventReminderFolderID")));  
6:        if (destinationFolder == null)  
7:        {  
8:          return false;  
9:        }  
10:        var itemName = ItemUtil.ProposeValidItemName(reminderViewModel.Email + reminderViewModel.EventId);  
11:        if (!EventReminderExists((itemName)))  
12:        {  
13:          if (!EventReminderExistsFallback((itemName)))  
14:          {  
15:            var newUser = new Event_Reminder  
16:            {  
17:              Name = itemName,  
18:              First_Name = reminderViewModel.FirstName,  
19:              Last_Name = reminderViewModel.LastName,  
20:              Email = reminderViewModel.Email,  
21:              Communications = reminderViewModel.Consent,  
22:              Event = new Guid(reminderViewModel.EventId)  
23:            };  
24:            using (new SecurityDisabler())  
25:            {  
26:              masterService.Create(destinationFolder, newUser);  
27:            }  
28:          }  
29:        }  
30:        return true;  
31:      }  

This is an example of an item that has been created:


Posting Data From the Delivery to the Management Server

This is the magic method.

As it turns out, it's quite simple. All we are doing here is posting the model object back over to the exact same controller and action on the management server. You can see on line 4 where I am building the url to the controller's action.

On line 6, I created a clean DTO so that I didn't run into any issues when serializing my object to JSON.

Finally, I used the WebClient class' UploadData method to post my model back over to the management server as shown on line 14.


1:  public bool PostEventToManagement(ReminderViewModel reminderViewModel)  
2:      {  
3:        //This will get executed if you are on a delivery (CD) server  
4:        var controllerUrl = $"{Settings.ManagementInstanceUrl}/api/mysite/forms/SubmitReminder";  
5:        //Create a clean DTO Object to send over  
6:        var reminderDTO = new {reminderViewModel.FirstName, reminderViewModel.LastName, reminderViewModel.Email, reminderViewModel.EventId, reminderViewModel.Consent};  
7:        using (var client = new WebClient())  
8:        {  
9:          client.Headers["Content-type"] = "application/json";  
10:          // serializing the reminderViewModel object in JSON notation  
11:          var jss = new JavaScriptSerializer();  
12:          var json = jss.Serialize(reminderDTO);  
13:          //Post to controller on cm  
14:          client.UploadData(controllerUrl, "POST", Encoding.UTF8.GetBytes(json));  
15:          return true;  
16:        }  
17:      }  

Summary

With these pieces in place, we can summarize the events that follow after a user has completed their input and clicks the submit button:

Management Server Instance

If the setting indicates that we are on the management server, the object is passed to the CreateEventReminderMaster method where it creates the item in the master database.

Delivery Server Instance

If the setting indicates that we are on the delivery server, the object is passed to the PostEventToManagement method where is posts the model over to the management server where the process starts again.

Once on the management server, the object ends up in the CreateEventReminderMaster method where the item is created in the master database.

Final Thoughts

I hope that you find this concept useful as you start working on your own custom MVC forms that require you to make changes to the master database from all instances.

Please share your thoughts and comments.

Happy Sitecoring!


Thursday, July 23, 2015

FXM Experience Editor - Cleaning Up Problematic External Content

Standard

Background

I have been using Federated Experience Manager (FXM) a lot, and it has worked extremely well in almost all of our client deployments.




I recently ran into an issue with one of our client's production instance where we had an external site that wasn't able to load up in the FXM Experience Editor.  We could see the page start loading, and then after a few seconds, it would stop leaving a blank page with a little bit of script. This left us scratching our heads for quite some time.

Using Fiddler's AutoResponder feature, we were eventually able to determine
that if we disabled an Adobe Dynamic Tag Management script library, we were able to successfully load the external site in the Experience Editor.

Our problematic script:






Our client made it very clear that removing the script from the external site was not an option. So, we needed to find a way to remove the script from the external site when loading it in the Experience Editor only.

Finding the Sweet Spot

Almost certain that there had to be a pipeline that I could hook into, I armed myself with the instance's "showconfig" and my favorite decompiler (JetBrains dotPeak), I started digging around until I discovered the following pipeline:

1:  <content.experienceeditor>  
2:   <processor type="Sitecore.FXM.Client.Pipelines.ExperienceEditor.ExternalPage.GetExternalPageContentProcessor, Sitecore.FXM.Client"/>  
3:   <processor type="Sitecore.FXM.Client.Pipelines.ExperienceEditor.ExternalPage.UpdateBeaconScriptPathProcessor, Sitecore.FXM.Client"/>  
4:   <processor type="Sitecore.FXM.Client.Pipelines.ExperienceEditor.ExternalPage.InjectControlsProcessor, Sitecore.FXM.Client"/>  
5:   <processor type="Sitecore.FXM.Client.Pipelines.ExperienceEditor.ExternalPage.AddPlaceholderData, Sitecore.FXM.Client"/>  
6:  </content.experienceeditor>  
This looked very promising indeed! Next step was to crack open the GetExternalPageContentProcessor.

Voila, I found exactly what I was looking for; the point at which FXM grabs the content from the external site, and sticks it into an argument that the rest of the processors can access (lines 19 & 20):

1:  public void Process(ExternalPageExperienceEditorArgs args)  
2:    {  
3:     Assert.ArgumentNotNull((object) args, "args");  
4:     Assert.ArgumentNotNull((object) args.MatcherContextItem, "MatcherContextItem");  
5:     Assert.ArgumentNotNull((object) args.ExperienceEditorUrl, "ExperienceEditorUrl");  
6:     string externalPageUrl = this.GetExternalPageUrl(args);  
7:     if (string.IsNullOrEmpty(externalPageUrl))  
8:     {  
9:      args.AbortPipeline();  
10:     }  
11:     else  
12:     {  
13:      string experienceEditorUrl = this.GetBaseExperienceEditorUrl(args);  
14:      HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, externalPageUrl);  
15:      request.Headers.Add("FxmReferrer", (IEnumerable<string>) new string[1]  
16:      {  
17:       experienceEditorUrl  
18:      });  
19:      HttpResponseMessage httpResponseMessage = this.externalSiteWebProxy.MakeRequest(string.Format("{0}&url={{0}}", (object) experienceEditorUrl), request);  
20:      args.ExternalPageContent = httpResponseMessage.Content.ReadAsStringAsync().Result;  
21:     }  
22:    }  

FXM External Content Sanitizer Processor

Yes, the name is a mouthful!

Knowing that this type of problem was bound to pop up again, I decided to write a custom processor that would accept a series of regular expressions in configuration,  and use them to strip out any problematic content that may be causing issues when loading up the FXM Experience Editor.

Processor

1:  public class SanitizeContent : IExternalPageExperienceEditorProcessor  
2:    {  
3:      private static string _sanitizeNode = "/sitecore/fxmSanitizeExternalContent";  
4:      public void Process(ExternalPageExperienceEditorArgs args)  
5:      {  
6:        Assert.ArgumentNotNull(args, "args");  
7:        Assert.ArgumentNotNull(args.ExternalPageContent, "ExternalPageContent");  
8:        foreach (var regex in GetRegexList())  
9:        {  
10:          var currentReg = new Regex(regex);  
11:          var cleanHtml = currentReg.Replace(args.ExternalPageContent, "");  
12:          args.ExternalPageContent = cleanHtml;  
13:        }  
14:      }  
15:      /// <summary>  
16:      /// Returns list of strings containing regular expressions that have been set in configuration  
17:      /// </summary>  
18:      private static IEnumerable<string> GetRegexList()  
19:      {    
20:        var configNode = Factory.GetConfigNode(_sanitizeNode);  
21:        var regexList = new List<string>();  
22:        foreach (XmlNode childNode in configNode.ChildNodes)  
23:        {  
24:          regexList.Add(XmlUtil.GetAttribute("value", childNode));  
25:        }  
26:        return regexList;  
27:      }  
28:    }  

Configuration

You can duplicate line 4 and add as many regular expressions as you need to. In my configuration, I added a regular expression (thanks Andy Uzick for the help) to strip out any script that contained the words "adobetm".

1:  <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">  
2:   <sitecore>  
3:    <fxmSanitizeExternalContent>  
4:     <sanitizeRegex value="&lt;script[^&lt;]*(adobedtm)[\s\S]*?&lt;/script&gt;"/>  
5:    </fxmSanitizeExternalContent>  
6:    <pipelines>  
7:     <group groupName="FXM" name="FXM">  
8:      <pipelines>  
9:       <content.experienceeditor>  
10:        <processor type="MyProject.Domain.Pipelines.ExperienceEditor.ExternalPage.SanitizeContent, MyProject.Domain" patch:after="processor[@type='Sitecore.FXM.Client.Pipelines.ExperienceEditor.ExternalPage.GetExternalPageContentProcessor, Sitecore.FXM.Client']" />  
11:       </content.experienceeditor>  
12:     </pipelines>  
13:    </group>  
14:   </pipelines>  
15:  </sitecore>  
16: </configuration>  

Problem solved!

With the script removed, the external site loaded up in the FXM Experience Editor, and we were able to complete the tasks that we had originally set out to do.

I hope that this helps others that run into this same issue.




Sunday, July 19, 2015

Managing Countries and Regions within the same site (Part 2) : The Region Resolver

Standard
With the content in place, the next step was to be able to tie the specific site instances to their respective countries or regions. We wanted to keep things nice and clean, and decided to add 2 additional attributes to the site definitions within our config file:

  1. siteRegion - The item id value of the region content item
    1. 1 These items where located in the global / shared location within our site. See part 1 for more information.

  2. pageNotFound - Redirect the user to a path within our site that contains a "page not found" page if they try and access a page that didn't match their country or region. 
So, our site definition looked something like this:

 <sites>  
    <site name="MySiteUSA" patch:before="site[@name='website']"  
       virtualFolder="/"  
       physicalFolder="/"  
       rootPath="/sitecore/content"  
       startItem="/MySite"  
       database="master"  
       domain="extranet"  
       allowDebug="true"  
       cacheHtml="true"  
       htmlCacheSize="10MB"  
       enablePreview="true"  
       enableWebEdit="true"  
       enableDebugger="true"  
       disableClientData="false"  
       hostName="*MySiteusa.com"  
       siteRegion="{B4F05B9C-1A40-4B95-AC03-C2115E7CA448}"  
       pageNotFound="/Page-Not-Found"  
       />  
    <site name="MySiteCA" patch:before="site[@name='website']"  
       virtualFolder="/"  
       physicalFolder="/"  
       rootPath="/sitecore/content"  
       startItem="/MySite"  
       database="master"  
       domain="extranet"  
       allowDebug="true"  
       cacheHtml="true"  
       htmlCacheSize="10MB"  
       enablePreview="true"  
       enableWebEdit="true"  
       enableDebugger="true"  
       disableClientData="false"  
       hostName="*MySitecda.com"  
       siteRegion="{A71B1C90-107B-4740-9913-8E683B019BF7}"  
       pageNotFound="/Page-Not-Found"  
       />  
   </sites>  

Quick note:
This site definition is not meant for a production instance. You obviously want to increase the cache size and turn off things like debugging in production.

Next, we added a simple class that we would use to retrieve an object that contains our site's context as well as the above-mentioned attributes from the configuration xml via it's properties.

When looking at the code below (starting at line 44), you will notice that we are attempting to pull the region object out of cache if it exists. We added a caching layer, because we wanted to ensure that this processor would be as fast as possible. You will see this being used a lot in our processor code that follows.

1:  public class SiteRegion  
2:    {  
3:      private const string SiteNode = "/sitecore/sites";  
4:      private XmlNode CurrentSiteNode  
5:      {  
6:        get  
7:        {  
8:          XmlNode targetParamsNode = Factory.GetConfigNode(SiteNode);  
9:          var currentSiteContext = Context.Site;  
10:          foreach (XmlNode childNode in targetParamsNode.ChildNodes)  
11:          {  
12:            if (XmlUtil.GetAttribute("name", childNode)  
13:              .Equals(currentSiteContext.Name, StringComparison.InvariantCultureIgnoreCase))  
14:            {  
15:              return childNode;  
16:            }  
17:          }  
18:          return null;  
19:        }  
20:      }  
21:      public string CacheKey  
22:      {  
23:        get  
24:        {  
25:          return Context.Site.Name;  
26:        }  
27:      }  
28:      public SiteContext Site  
29:      {  
30:        get { return Context.Site; }  
31:      }  
32:      public string Region  
33:      {  
34:        get { return XmlUtil.GetAttribute("siteRegion", CurrentSiteNode); }  
35:      }  
36:      public string PageNotFoundUrl  
37:      {  
38:        get { return XmlUtil.GetAttribute("pageNotFound", CurrentSiteNode); }  
39:      }  
40:      public static string CurrentRegion  
41:      {  
42:        get  
43:        {  
44:          var siteRegion = CacheHelper.RegionCache.GetObject(Context.Site.Name) as SiteRegion;  
45:          if (siteRegion == null)  
46:          {  
47:            siteRegion = new SiteRegion();  
48:            CacheHelper.RegionCache.SetObject(Context.Site.Name, siteRegion);  
49:          }  
50:          return siteRegion.Region;  
51:        }  
52:      }  
53:    }  

Region Resolver Processor

Next, we built out the region resolver pipeline processor to check if an item was meant for a specific country / region.

Sitecore provides us with a nice SafeDictionary KeyValuePair object in their PipelineArgs that is useful for adding custom data to pass down the pipeline. This was ideal for us to pass the message along to the Region Page Not Found processor (next up) telling it whether the "page not found" page should be displayed or not (Line 26).

Line's 15 and 16 are performing the check for the context item's region field being set.

1:  public class RegionResolver : HttpRequestProcessor  
2:    {  
3:      public override void Process(HttpRequestArgs args)  
4:      {  
5:        Assert.ArgumentNotNull(args, "args");  
6:        var showRegionPage = true;  
7:        var siteRegion = CacheHelper.RegionCache.GetObject(Context.Site.Name) as SiteRegion;  
8:        if (siteRegion == null)  
9:        {  
10:          siteRegion = new SiteRegion();  
11:          CacheHelper.RegionCache.SetObject(Context.Site.Name, siteRegion);  
12:        }  
13:        if (Context.Item != null && siteRegion.Site.HostName.IsNotEmpty())  
14:        {  
15:          if (Context.Item.Fields["Region"] != null &&  
16:            !Context.Item.Fields["Region"].Value.Contains(siteRegion.Region))  
17:          {  
18:            showRegionPage = false;  
19:          }  
20:        }  
21:        if (showRegionPage)  
22:        {  
23:          return;  
24:        }  
25:        //Add entry to safe dectionary to tell region page not found processor to redirect to page not found  
26:        args.CustomData.Add("hideRegionPage", true);  
27:        var notFoundProcessor = new RegionPageNotFound();  
28:        notFoundProcessor.Process(args);  
29:      }  
30:    }  

Region Page Not Found Processor

One of things that we wanted was to have a "page not found" bit of logic that would be able to handle both our normal page not found / 404's for our sites, as well as those that were not supposed to be displayed for our country / region.

So, we wrote a processor that would be able to catch both.

Before looking at the code, there are a few things to note:
  1. On line 7, we are checking to see if our Region Resolver has told us to hide the context item via the "message" in the safe dictionary object .
  2. If you have defined custom MVC routes, you would need to check for those in the pipeline and allow them to be processed (line 31).
  3. This processor would do the job of redirecting visitors to our page not found path, set in our site definition that was mentioned above (line 47).

1:  public class RegionPageNotFound : HttpRequestProcessor  
2:    {  
3:      public override void Process(HttpRequestArgs args)  
4:      {  
5:        Assert.ArgumentNotNull(args, "args");  
6:        //Check for safe dectionary object indicating that page needs to be "hidden"  
7:        var hideRegionPage = args.CustomData.ContainsKey("hideRegionPage");  
8:        if (!hideRegionPage &&  
9:          (Context.Item != null  
10:          || Context.Site == null  
11:          || Context.Site.Name.Equals("shell", StringComparison.CurrentCultureIgnoreCase)  
12:          || Context.Site.Name.Equals("website", StringComparison.CurrentCultureIgnoreCase)  
13:          || Context.Database == null  
14:          || Context.Database.Name.Equals("core", StringComparison.CurrentCultureIgnoreCase)  
15:          || string.IsNullOrEmpty(Context.Site.VirtualFolder)  
16:          ))  
17:        {  
18:          return;  
19:        }  
20:        // The path in the requested URL.  
21:        var filePath = Context.Request.FilePath.ToLower();  
22:        if (string.IsNullOrEmpty(filePath)  
23:          || WebUtil.IsExternalUrl(filePath)  
24:          || System.IO.File.Exists(HttpContext.Current.Server.MapPath(filePath)))  
25:        {  
26:          return;  
27:        }  
28:        //Api path checks  
29:        var uri = HttpContext.Current.Request.Url.AbsoluteUri;  
30:        if (uri.Contains("sitecore/api")  
31:          || uri.Contains("api/mycustomroute"))  
32:        {  
33:          return;  
34:        }  
35:        var siteRegion = CacheHelper.RegionCache.GetObject(Context.Site.Name) as SiteRegion;  
36:        if (siteRegion == null)  
37:        {  
38:          siteRegion = new SiteRegion();  
39:          CacheHelper.RegionCache.SetObject(Context.Site.Name, siteRegion);  
40:        }  
41:        // Send the NotFound page content to the client with a 404 status code  
42:        if (!string.IsNullOrEmpty(siteRegion.Region) && !string.IsNullOrEmpty(siteRegion.PageNotFoundUrl))  
43:        {  
44:          var ctx = HttpContext.Current;  
45:          ctx.Response.TrySkipIisCustomErrors = true;  
46:          ctx.Response.StatusCode = 404;  
47:          ctx.Response.Redirect(siteRegion.PageNotFoundUrl);  
48:          ctx.ApplicationInstance.CompleteRequest();  
49:        }  
50:      }  
51:    }  

Hooking into the HttpBeginRequest Pipeline

The final piece of this puzzle, was to add our new processors to the HttpBeginRequest Pipeline, after the ItemResolver processor.

Here is what the config file looked like that would make the magic happen:

 <pipelines>  
  <httpRequestBegin>  
   <processor  
        type="MyProject.Library.Pipelines.HttpRequestBegin.RegionResolver, MyProject.Library"  
        patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']"/>  
   <processor  
        type="MyProject.Library.Pipelines.HttpRequestBegin.RegionPageNotFound, MyProject.Library"  
        patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']"/>  
  </httpRequestBegin>  
 </pipelines>  

Order is important here, because as I mentioned, we want the Region Resolver Processor to be able to tell the Region Not Found Processor whether or not the page should be displayed for the context site.

Next Up

In Part 3, I am going to demonstrate how we were able to "regionize" renderings on the sites by building a custom condition for the Sitecore Rules Engine.
 

Wednesday, June 17, 2015

How to Reset All Values for a Field using PowerShell

Standard
Today I ran into a situation where we had a Multilist with Search field for a bunch of items that had values set to items that no longer existed in our instance. This is often the case when you are in development and deleting and adding sections of the tree while testing things.

As you know, Sitecore tends to be unhappy when this happens. A common error message is the following:

Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

So, I thought it would be a good exercise to write up a quick PowerShell script that would iterate over items in my tree, and reset the values for a field that I specify.

All you need to do is set the $fieldToReset and $path variable values to suit your needs.

 $fieldToReset = "My Field Name";  
 $path = "master:\sitecore\content\MyItem";  
 $items = Get-ChildItem -Path $path -Recurse   
 $items | ForEach-Object { if ($_.fieldToReset -ne $null) {$_.fieldToReset = ""} }  

Bless you PowerShell!


Monday, June 15, 2015

Schedule Full and Incremental Content Backups using PowerShell Extensions

Standard
During my studies in University, and working for a smaller company in the past where I was involved in the hardware side of the IT world,  I discovered the power of command-line scripts when automating daily tasks such as server backups.

Looking back at late 2006, when Microsoft first introduced PowerShell,  they gave us a more advanced command-line interface where we could do some really fancy stuff. There was a new syntax we had to learn, but with enough time, it was as familiar as the good old MS-DOS prompt.



Enter Sitecore PowerShell Extensions

Having been in the Sitecore world for quite some time, I was excited to find Sitecore PowerShell Extensions on the marketplace.

As Alistair Deneys put it at the 2012 Symposium:
"There is nothing you can not do with PowerShell Console, because you're inside the Sitecore application. You can call the Sitecore API"

I have been using the module religiously for the past year, and am truly impressed by the work of the contributors.

If you know a little PowerShell, and haven't tried it before, I suggest you download and give it a bash.

This post assumes that you have the module installed. As I am running XP 8.0 Update 2, I installed version 3.0 in my instance.

The Use Case

As I mentioned in an earlier post, one of the projects that I am working on involves building out a couple new sites that have an E-Commerce back end that supports thousands of products that we are integrating into Sitecore via Commerce Connect.

One of the features that the client requested was the ability to perform batch product updates using Microsoft Excel. We ended up building an admin app where they could export and import CSV files and manipulate the content within Sitecore, using the data within these files. Our tree structure looks like this:

You may already be thinking that this sounds like trouble, and trust me, we had the same thoughts.

Wouldn't it be nice if we could have an automated daily backup strategy that would enable us to revert to a backup set relatively quickly in case something went wrong? Just like a server backup script?

PowerShell Backup Scripts

After doing some research and testing, I formulated the following scripts that create either a full or incremental package of all the items in the Master database that are located at this path in the content tree: "/sitecore/content/Data/Product-Repository/Products". You can modify this path to suite your needs.

Full Backup Script

 $packageBackupName = "Client Products Full Package Backup";  
 $sourceItems = Get-ChildItem -Path "master:\sitecore\content\Data\Product-Repository\Products"  
 $package = new-package $packageBackupName;  
 $package.Sources.Clear();  
 Write-Log "$packageBackupName started";  
 $Author = [Sitecore.Context]::User.Profile.FullName;  
 $Publisher = [Sitecore.SecurityModel.License.License]::Licensee  
 $DayOfWeek = [datetime]::Now.DayofWeek  
 $package.Metadata.Author = $Author;  
 $package.Metadata.Publisher = $Publisher;  
 $package.Metadata.Version = "";  
 $package.Metadata.Readme = "";  
 $packageSource = $sourceItems | New-ItemSource -Name "Content Source" -InstallMode Overwrite -MergeMode Merge  
 $package.Sources.Add($packageSource);  
 Export-Package -Project $package -Path "$packageBackupName-$DayOfWeek" -Zip  
 Write-Log "$packageBackupName completed"  

Incremental Backup Script

Note: This script will generate a package of items located under the target path that have been added or changed within the last 2 days. You can modify this day value by changing the "$changedDays" variable below.

 $packageBackupName = "Client Products Incremental Package Backup";  
 $changedDays = 2;  
 $sourceItems = Get-ChildItem -Recurse -Path "master:\sitecore\content\Data\Product-Repository\Products" | Where-Object  { $_."__Updated" -gt (Get-Date).AddDays(-$changedDays) -or $_."__Created" -gt (Get-Date).AddDays(-$changedDays)}  
 $package = new-package $packageBackupName;  
 $package.Sources.Clear();  
 Write-Log "$packageBackupName started";  
 $Author = [Sitecore.Context]::User.Profile.FullName;  
 $Publisher = [Sitecore.SecurityModel.License.License]::Licensee  
 $DayOfWeek = [datetime]::Now.DayofWeek  
 $package.Metadata.Author = $Author;  
 $package.Metadata.Publisher = $Publisher;  
 $package.Metadata.Version = "";  
 $package.Metadata.Readme = "";  
 $packageSource = $sourceItems | New-ItemSource -Name "Content Source" -InstallMode Overwrite -MergeMode Merge  
 $package.Sources.Add($packageSource);  
 Export-Package -Project $package -Path "$packageBackupName-$DayOfWeek" -Zip  
 Write-Log "$packageBackupName completed"  

Running Scripts and Package Default Location



Setting up Scheduled Tasks

You can certainly run these scripts on demand to create packages, but the real magic would be to let these scripts run on a schedule so that once they are set up, they would take care of themselves.

This can be achieved within a few simple steps:

1. Navigate to /sitecore/system/Modules/PowerShell/Script Library/Task Management/Tasks and create a new Powershell Script Library item, that will contain your 2 scripts. I called mine "Content Backups".

2. Within this new script library item, create 2 new PowerShell Script items, and paste in each respective PowerShell script. I called my items "Full Product Backup" and "Incremental Product Backup".



3. Navigate to "/sitecore/system/Tasks/Schedules" and create a Schedule Item to invoke the scripts housed in the Items above. To learn more about Sitecore Scheduled Tasks, please see John West‘s post discussing them).

  3.1 Set the command to "Commands/PowerShellScriptCommands"
  3.2 Set items to the location of your PowerShell Script Item for each script that you created in step
2.
  3.3 Set the schedule item to your preferred value.




I set my Full Backup to ||1|13.00:00 so that it will run once on Sunday and the interval will be 13 hours, so it will only be executed once within the day.

I set my Incremental Backup to ||126|13.00:00 so that it will run Monday - Saturday, once during the day (same as above).

Reviewing and Testing your Scheduled Tasks

Checking that you set up everything correctly and testing it out is easy. Simply open up the Task Manager within the PowerShell Toolbox:



Once launched, you can review the scheduled tasks that you set up. To test either of them, click on the item and then click the "Execute Now" button.