EPiServer 10 – Restrict any Block Types on XHtmlString property using the Validation Attribute

I couldn’t find a similar solution online as I believe there is an emerging pattern where we are now instructing clients to drag EPiServer Blocks into Rich Text editors.

Requirement:

As an Editor I want to limit certain blocks types from being dragged into the rich text editor

Solution #1:

Apply this attribute to any of your xhtml properties to enable to validation functionality

[Display(Name = "Text", GroupName = SystemTabNames.Content)]
[XHtmlStringAllowedBlockTypes(new[] { typeof(TextBlock), typeof(ImageBlock) })]
public virtual XhtmlString RichText { get; set; }

 

Implement the following attribute where your other validation attributes reside.

 public class XHtmlStringAllowedBlockTypes : ValidationAttribute
{
private readonly Type[] allowedTypes;
public XHtmlStringAllowedBlockTypes(Type[] allowedTypes)
{
this.allowedTypes = allowedTypes;
}

protected override ValidationResult IsValid(object value, ValidationContext context)
{
var contentData = context.ObjectInstance as IContentData;

if (contentData != null && contentData.Property[context.MemberName].Value is XhtmlString)
{
var richTextProperty = (XhtmlString)contentData.Property[context.MemberName].Value;

foreach (ContentFragment fragment in richTextProperty.Fragments.Where(x => x is ContentFragment))
{
var content = ServiceLocator.Current.GetInstance<IContentRepository>().Get<IContentData>(fragment.ContentLink);

foreach (var allowedType in allowedTypes)
{
if (allowedType.IsInstanceOfType(content))
{
return new ValidationResult(string.Format("You cannot add {0} to {1}", content.GetType(), context.MemberName));
}
}
}
}

return ValidationResult.Success;
}
} 

 

References:

http://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/9/Content/Properties/Property-types/Writing-custom-attributes/

http://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/9/Content/Validation/

EPiServer 7.5 – Exception – More than one content model is assigned to the guid

When running an EPiServer site and you get the exception “More than one content model is assigned to the guid”. This is usually because you’ve changed a namespace.

There will be a reference to the old namespace cached somewhere so do the following:

1) Clear your bin folder

2) Clear the ASP.NET Temporary files folder under the framework version your project is using

New Marie Curie ‘In Your Area’ Search Functionality

For the past two weeks here at Marie Curie we’ve been working on new functionality for searching for Marie Curie services across the United Kingdom. It has just today finally gone live and now we can serve our supporters with better information about the key services and events in their area.

‘In Your Area’

new_inyourarea

On launch we have included the following:

  • Fundraising Groups
  • Fundraising Offices
  • Marie Curie Shops
  • Marie Curie Hospices
  • Events we run

Results are ordered by distance from the location you have entered and can be navigate by either the result list of the result map. Clicking on a result item on one result section, highlights it’s twin in the other.

The page is also fully responsive allowing you to view the search results and map as separate views.

new_inyourarea_mobile_listnew_inyourarea_mobile_map

Click here to go to the live search

Property Injection & Action Filters in EPiServer 7 CMS

I spent a bit of time putting in some custom implementation to support Property Injection on to my Action Filters before realizing that EPiServer 7 already provides this functionality for me. Doh.

In order to take advantage all you need to do is set your DI controlled interface to the generic type property of the Injected class. This will lazily set the interface when the property is used…awesome.

public class CustomFilter : ActionFilterAttribute
{
public Injected<IService> Service { get; set; }
}

EPiServer 7 CMS – Import and Exporting Content between Environments

At Marie Curie we have several environments that sit across our infrastructure. Occasionally we get a requirement to shift content from one environment to another. This could be because we’ve auto generated pages via scheduled job or that QA have created a string of pages and want it replicated in another environment without having to key them all in again. Here are the basic steps required to do this.

Export Data

The EPiServer admin section exposes two tools which allow you to do this, Import Data and Export Data. On your source environment navigate to the Export Data tool.

EPiServerAdmin Import Export Data

Content items, content types even property definitions can be exported. What we are interested in is Export Content Items. Tick this box then hit the lookup button. This will make a pop up window appear with your website’s tree structure. Choose the page you are interested. Hit Select then hit Export.

EPiServerAdmin Import Export Data

The export data screen will show in real time the number of items that have been exported and your browser will automatically down a zip file.

EPiServerAdmin Import Export Data

Import Data

Switch to your target environment and once again head to the admin section but this time click on Import Data. here you can select the zip file you just exported and destination of the content. When you are ready click Begin Import.

EPiServerAdmin Import Export Data

Done! You will see that all the content items have been successfully imported.

EPiServerAdmin Import Export Data

I would not recommend this method as part of a permanent solution. It should be used for more one off circumstances. Remember that the target and source environment should have matching content types and property definitions otherwise you may not get all the data transferred correctly. Enjoy!