This post has been migrated from www.experimentsincode.com, we apologise if some of the images or content is missing

Team Development for Sitecore Version 5 introduces the ability to add validators that allow you to check the structure and integrity of a TDS solution. TDS comes with a set of useful validators out of the box but if you need something specific it is possible to create your own custom validators, in this blog post we will look at how we can do this. First start Visual Studio and create a Class Library project. Once the project is created we need to add references to the following assemblies:
  • HedgehogDevelopment.SitecoreCommon.Data.dll
  • HedgehogDevelopment.SitecoreCommon.Data.Parser.dll
  • HedgehogDevelopment.SitecoreProject.Tasks.dll
These can be found in C:\Program Files (x86)\MSBuild\HedgehogDevelopment\SitecoreProject\v9.0. Next you need to open the AssemblyInfo.cs file and add the following to end of the file:
[assembly: ContainsValidators()]
This is used by TDS to see if your assembly contains any validators. With this setup we are now ready to create the actual validator. I am going to create a validator that ensures that items do not contain any spaces in their names. First I create my NoSpaceValidator class and inherit from HedgehogDevelopment.SitecoreProject.Tasks.ProjectAnalysis.UserConfigurableValidator. The UserConfigurableValidator allows a user to specify additional properties in the Validation Tab, you can inherit from Validator if you don't need additional properties:
My class looks like this:
    public class NoSpaceValidator : UserConfigurableValidator
    {
        public override ValidatorSettings GetDefaultSettings()
        {
        }
        public override IEnumerable<Problem> Validate(Dictionary<Guid, HedgehogDevelopment.SitecoreProject.Tasks.SitecoreDeployInfo> projectItems, System.Xml.Linq.XDocument scprojDocument)
        {
        }
    }
I next need to add an attribute to my class to inform TDS that this is a validator it can use, this attribute also contains the information that I want to be displayed on the Validator Tab:
    [ValidatorAttribute("CUS001", Status.Warn, Description = "Sitecore items must not have spaces in names", Name="No Spaces in Names")]
    public class NoSpaceValidator : UserConfigurableValidator
    {
    }
On the validation tab we can see my attribute information being outputted:
This is the basics setup, next I can specify some default values that will appear in the additional properties list when someone first selects it, this is done in the GetDefaultSettings method:
    [ValidatorAttribute("CUS001", Status.Warn, Description = "Sitecore items must not have spaces in names", Name="No Spaces in Names")]
    public class NoSpaceValidator : UserConfigurableValidator
    {
        public override ValidatorSettings GetDefaultSettings()
        {
            return new ValidatorSettings()
            {
                Properties = new List<string>()
                {
                    "/sitecore",
                }
            };
        }

        public override IEnumerable<Problem> Validate(Dictionary<Guid, HedgehogDevelopment.SitecoreProject.Tasks.SitecoreDeployInfo> projectItems, System.Xml.Linq.XDocument scprojDocument)
        {

        }
    }
Finally I need to create my validation logic in the validate method, I have commented the code to make it clear what is happening:
        public override IEnumerable<Problem> Validate(Dictionary<Guid, HedgehogDevelopment.SitecoreProject.Tasks.SitecoreDeployInfo> projectItems, System.Xml.Linq.XDocument scprojDocument)
        {
            //loop through each item in the TDS project
            foreach (var item in projectItems)
            {
                //check that the item path starts with a value specified in the Additional Properties list
                //otherwise we just ignore the item
                if (Settings.Properties.Any(
                        x => item.Value.Item.SitecoreItemPath.StartsWith(x, StringComparison.InvariantCultureIgnoreCase)))
                {

                    //get the name of the item from the Sitecore Item Path
                    var name = item.Value.Item.SitecoreItemPath.Split('/').Last();
                    //do out check for a space
                    if (name.Contains(" "))
                    {
                        //when a problem is found get the position of it in the TDS project file
                        ProblemLocation position = GetItemPosition(scprojDocument, item.Value.Item);

                        //create a report which will be displayed in the Visual Studio Error List
                        Problem report = new Problem(this, position)
                        {
                            Message = string.Format("A space in item name {0}", item.Value.ParsedItem.Path)
                        };

                        yield return report;
                    }
                }
            }
        }
Our validator now has all the code that it needs to work, all we need to do now is drop the DLL it into the folder C:\Program Files (x86)\MSBuild\HedgehogDevelopment\SitecoreProject\v9.0. In our TDS project we will see that the validator now appears (after a VS restart):
And when we compile the project we will start to receive warnings and errors about our TDS project:
Validators are very quick and easy to setup but should allow you to make much more scalable and robust TDS solutions.
comments powered by Disqus