Archive for September, 2015

When a content author enters decoded symbols in single line / multiline sitecore text fields, the symbols don’t get encoded on their own on the site when we output them with @Editable (Glass method – supporting page editor mode).
This also applies to links rendered using @RenderLink (Glass method)
This would mean text like &reg; and &trade; getting output in place of ® and ™ respectively.

Sitecore text fields DO support these symbols being pasted on to them directly, but sometimes it is not a choice we can make, especially if we have items created and prefilled automatically through external code / a connector to a third party system etc.

To achieve this, we create a new class inheriting from the GlassView class, and create Editable / RenderLink methods which use the respective base methods, taking care of the HtmlDecode!

In most product repositories, we come across product specification sets for each product, which usually comprise of a key value pair.
We had one such scenario in our project, and we came across the requirement to facet on various specification keys.

We have currently added a list of comma separated (key, value) pairs in the specifications computed field. The computed field needed to be declared as a string collection:

And in the code for the computed field (implementing IComputedIndexField), you will want to split this list and return it as a List.
So this

public object ComputeFieldValue(IIndexable indexable)

would return List<string>

Here’s a sample from the index:

So then we facet on this field, and filter out the facets that start with (specification key + ‘,’) for the specification key we want the facets for and remove the preceding key and ‘,’.
So if I were to facet on size specification for the below data (provided we had a separator between the specification key and value), we would get:

Large (1)
Small (3)
Medium (1)

This way, we use the same computed field to facet on any kind of specification.

When using the salesforce connector with Sitecore, we came across the need to store user profile images as well in salesforce.
Once the connector is set up (which includes setup in Web.config / Domains.config and S4S.config), any user user profile interaction automatically happens with salesforce, and fields which are mapped in web.config with corresponding sitecore fields – will automatically get mapped / updated on these interactions!

The special case we came across, was with Profile images. Our site allowed the user to upload profile images, which we needed to store in salesforce, and additionally also wanted the image to show up in sitecore.

The solution we came up with, was to create a document folder in salesforce and upload all profile images there with the contact id as the file name as a document, and then store the url in the Sitecore user portrait field.

Once the document is added in salesforce, the document url will give you the base document url of your salesforce instance, which you can use and replace the intended document id to get the right url for each new document created. This is what the S4SHelper.GetDocumentUrl(documentId) method does.

While working with Sitecore, we add many renderings in the presentation and that makes up each page that is output for the corresponding item.

We are currently working with Sitecore MVC, and we have traditionally added start and end comments in views manually (copy pasting the path of the current view) to mark the start and end of each component on the page. This helped us debug things, made the output html more readable and maintainable, and lets face it, after a month or two, we find it hard to remember which module was called what!

Recently it was pointed out that this might be a potential security threat and we are exposing details of our folder structure in the production environment, which might not be the best idea.

So to ease the process, and remove this potential threat, we created a html helper extension method which would dynamically output the view path start and end comments subject to an appsetting value being toggled to true. This appsetting key would be toggled to false in any environment where we wouldn’t want the comments to be output!

Transform File (glassv3header.tt) – for the common header and will appear only once in your generated code file – this includes the namespace using statements / base interface / class for all models.

Base Project Transform File (glassv3item.tt) – The other template file will be used in a repetitive manner for every item that code (class / model) is generated for.

In addition to this, we also use the Code Generation Template (itempaths.tt) file to generate item properties for any items we would want. The properties generated per item, is again decided by the template file itself, but out of the box, contains properties such as the item id, item path, template name and template id.

Add TemplateId to all codegen classes

One thing we noticed right off the bat, was the lack of a property for the template id in the models! We needed this for many situations, like deciding a separate html rendering based on the template id of the item etc. So we updated the t4 template (glassv3header.tt) to include the template id for every model, including the appropriate sitecore attributes on them to get them to populate as expected. (line below)

Item Name vs a Name field

The header template for the common code for your code gen file, has a property mapped to the item name ‘Name’. We came across a situation where we were working with Sitecore Commerce Connect templates, and the main product template there had a Name field. When we tried generating code for that template, we ran into conflicts! We couldn’t change the name of the field in the Commerce Connect template, so had to update the t4 template (glassv3header.tt) instead to change the ‘Name’ property to ‘ItemName’ (line below)

Error with codegen for System Language template

When we tried adding the sitecore system template ‘/sitecore/templates/System/Language’ into TDS and generating code for it, but we again ran into conflicts here! This is why we again updated the t4 template (glassv3header.tt) to explicitly specify the namespace of the Language property. (line below)

Add a new sitecore field type to enable codegen create the right custom field property types

For various reasons, we have had to add custom fields in Sitecore. For these new fields, by default glassmapper will general Object properties unless we provide a hint to the type of data this new custom field will contain.
For example, the Single Select with Search, Single Select Tree fields are link fields and contain a single guid, while Multisite Multilist with Search is a list field and contains a list of guids. So we need to map the intended property type in the GetGlassFieldType() method of glassv3item.tt template file:

In some cases, to maintain the integrity of the design, we might want to restrict the amount of text that the content author can add in the rich text field. For this purpose we created a custom validator and applied the same to rich text fields only.

For single line / multiline fields, this can be easily achieved by simply using regex patterns for validation, with rich text fields, the presence on html, requires more custom logic.

Please note – you can also disallow certain tags in the custom validation code here, like images / canvas / picture etc, if the design calls for it.

We first added a field for the maximum length, to the Template Field item (/sitecore/templates/System/Templates/Template field). which would make this field available in all template fields.

Following is the code used for the custom validation, which uses this field:

And we then created a new validation rule item in Sitecore and pointed it to this method in our assembly:

This rule then needs to be added to the rich text field item, depending upon what actions you would like to trigger the validations at, the rule will need to be added accordingly. Eg. Quick Action Bar / Validate Button / Validation Bar:

As an example, consider this field where the validations have been set at:

So exceeding the character limit (after stripping off html tags!), would give a fatal error.

In most sites, by design we have some minimum image size requirements which the content authors need to adhere by while uploading content images to maintain the integrity of the design.

In our project we wanted to add a validation in place so that the content author wouldn’t be allowed to set images smaller than what minimum requirement was set by the front end designers.

In some cases, we also needed to enforce an exact dimensions match, so we included the provision for that as well.

Here’s how we went about this:

We created a new template to use here – Base Image Validation

And added this new template as a base for the Template Field template:

So now, these fields would be available on all template fields. Even though these fields will be available in all template fields, the validation logic we hook up, will be fired only for image field types.

And we then created a new validation rule item in Sitecore and pointed it to this method in our assembly:

This rule then needs to be added to the image field item, depending upon what actions you would like to trigger the validations at, the rule will need to be added accordingly. Eg. Quick Action Bar / Validate Button / Validation Bar:

As an example, consider this field where the validations have been set at:

So uploading an image smaller than this would result in the error state:

We came across the need to be able to truncate text (html) entered in a rich text field to be able to display the same on compact modules / show article summaries etc.
There are many different approaches that can be taken here to be able to safely truncate html. But here’s the code that passed our testing and is currently live! We are parsing the html into XML to achieve this.

Please note:
This code truncates the text to the nearest word, and if the character limit is reached within the first word, the truncation is done mid word instead of returning no text at all. It will also allow you to optionally append ellipses to the truncated text.

The TruncateText() method only truncates a given string to the nearest word, while the TruncateHtml() method is responsible for converting the passed in text into XML and safely truncating the same, taking care of maintaining valid XML and hence valid HTML.

We have also wrapped any passed in text into an external XML tag , to make sure that passed in free text also can be parsed in this way.

When it came to using this in Sitecore, here’s an example of how we have put this to use!