Email Aaron Blake now at webmaster@aaronblake.co.uk
or contact me

Telerik RadGrid events not firing in Sitecore

I recently had to use a Telerik RadGrid during the development of an admin page within a sitecore site.

The error:

Sitecore’s event handlers conflict with RadGrid, which causes sorting, filtering, editing and deleting to fail.
I kept receiving a bank page every time an event fired, as if it lost the datasource.

The solution:

The solution to this problem is to exclude the controls from Sitecore’s event handlers.
You can do this very easily by updating the following section in the Sitecore web.config:

  
    
      
        System.Web.UI.WebControls.Repeater
        System.Web.UI.WebControls.DataList
        System.Web.UI.WebControls.GridView
        Telerik.Web.UI.RadGrid
      
    

Sitecore – CryptographicException file not found

After installing a new instance of Sitecore manually, I kept receiving the following error:

CryptographicException file not found

Server Error in ‘/’ Application.
The system cannot find the file specified.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Security.Cryptography.CryptographicException: The system cannot find the file specified.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[CryptographicException: The system cannot find the file specified.
]
System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer) +7715070
System.Security.Cryptography.DSACryptoServiceProvider.ImportParameters(DSAParameters parameters) +258
System.Security.Cryptography.DSA.FromXmlString(String xmlString) +501
Sitecore.Nexus.Licensing.NexusLicenseApi.(String xml, Guid instance) +124
Sitecore.Nexus.Licensing.NexusLicenseApi.GetSnapShot(Guid instance) +683
Sitecore.SecurityModel.License.LicenseManager.GetSnapshotData(Guid instance) +47
Sitecore.SecurityModel.License.LicenseManager.UpdateSnapshot() +70
Sitecore.SecurityModel.License.LicenseManager.Initialize() +8
Sitecore.Nexus.Web.HttpModule.Application_Start() +76
Sitecore.Nexus.Web.HttpModule.Init(HttpApplication app) +435
System.Web.HttpApplication.InitModulesCommon() +65
System.Web.HttpApplication.InitModules() +43
System.Web.HttpApplication.InitInternal(HttpContext context, HttpApplicationState state, MethodInfo[] handlers) +729
System.Web.HttpApplicationFactory.GetNormalApplicationInstance(HttpContext context) +298
System.Web.HttpApplicationFactory.GetApplicationInstance(HttpContext context) +107
System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +289

Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927

This random error is actually being caused by the IIS application pool not being configured to run using a non-standard user account Identity.

The solution:

To solve the problem, set Load User Profile to true for the website application pool in IIS.

  1. Open IIS Manager
  2. Click on Application Pools on the right.
  3. Right Click on the application pool for your web site and select “Advanced Settings”
  4. In the Process Model section, set Load User Profile to true.

References:

http://www.realnero.info/2010/04/sitecore-cryptographicexception-file.html
http://sdn.sitecore.net/Products/Sitecore%20V5/Sitecore%20CMS%206/ReleaseNotes/ChangeLog.aspx

Validate Sitecore Item Programmatically

I was developing an event handler that needed to check if the fields in a Sitecore item were valid before continuing with the event.
I thought Sitecore items might have a property such as:
item.Validation.IsValid

But this isn’t the case. To check if an item is valid, I created a custom function called ValidateItemFields that works in an item event handler.

Solution:

        ///

        /// Validates all fields in item.
        /// 

        ///

        /// The item to be validated.
        /// 
        /// 
        /// True if all fields are valid.
        /// 
        private static bool ValidateItemFields(Item item)
        {
            if (((item != null) && !item.Paths.IsMasterPart) && !StandardValuesManager.IsStandardValuesHolder(item))
            {
                item.Fields.ReadAll();
                item.Fields.Sort();
                foreach (Field field in item.Fields)
                {
                    if (!string.IsNullOrEmpty(field.Validation) && !Regex.IsMatch(field.Value, field.Validation, RegexOptions.Singleline))
                    {
                        return false;
                    }
                }

                var formValue = WebUtil.GetFormValue("scValidatorsKey");
                if (!string.IsNullOrEmpty(formValue))
                {
                    var validators = ValidatorManager.GetValidators(ValidatorsMode.ValidatorBar, formValue);
                    var options = new ValidatorOptions(true);
                    ValidatorManager.Validate(validators, options);
                    var valid = ValidatorResult.Valid;
                    foreach (BaseValidator validator in validators)
                    {
                        var result = validator.Result;
                        if (validator.ItemUri != null)
                        {
                            var item1 = Client.ContentDatabase.GetItem(validator.ItemUri.ToDataUri());
                            if (((item1 != null) && StandardValuesManager.IsStandardValuesHolder(item1)) && (result > ValidatorResult.CriticalError))
                            {
                                result = ValidatorResult.CriticalError;
                            }
                        }

                        if (result > valid)
                        {
                            valid = validator.Result;
                        }

                        if (validator.IsEvaluating && (validator.MaxValidatorResult >= ValidatorResult.CriticalError))
                        {
                            return false;
                        }
                    }

                    switch (valid)
                    {
                        case ValidatorResult.CriticalError:
                            return false;

                        case ValidatorResult.FatalError:
                            return false;
                    }
                }
            }

            return true;
        }

Example:

///

        /// On Item Moving event handler for ImportArticle Items
        /// 

        ///

        /// The sender object.
        /// 
        ///

        /// The args.
        /// 0 - Item being moved
        /// 1 - Move destination
        /// 
        public void OnItemMoving(object sender, EventArgs args)
        {
            var item = Event.ExtractParameter(args, 0) as Item;
            if (item == null)
            {
                return;
            }

            // Check item type is ImportArticle
            if (!item.Template.Name.Equals(TemplateName))
            {
                return;
            }

            // If ancestor is a child of Content Node, then return
            if (item.Axes.GetAncestors().Any(ancestor => ancestor.Paths.Path.Equals(ContentItemPath)))
            {
                return;
            }

            // If item is valid, return
            if (ValidateItemFields(item))
            {
                return;
            }

            SheerResponse.Alert(ErrorMessage, new string[0]);
            ((SitecoreEventArgs)args).Result.Cancel = true;
        }

Email me through this magic form.