Tag Archives: C#

Traverse all fields for Sitecore item without loading all values

When working with clones in Sitecore you sometimes need to call ReadAll() to get hold of every field. This gives the penalty of loading all the items field values one by one.

Here is an extensionmethod to get all TemplateFields for the item.

    using Sitecore.Data.Items;
    using Sitecore.Data.Managers;
    using Sitecore.Data.Templates;

    public static class SitecoreItemExtensions
    {
        public static TemplateField[] GetTemplateFields(this Item item)
        {
            Template template = TemplateManager.GetTemplate(item);
            if (template == null)
            {
                return new TemplateField[] { };
            }

            return template.GetFields();
        }
    }

Then just loop through this collection and lazy load only the fields you want, like this:

    foreach (var field in Sitecore.Context.Item.GetTemplateFields())
    {
        // logic goes here
    }

Beware of using Field.HasValue in Sitecore

After calling Field.HasValue or Field.GetValue(false, x) the two properties ContainsStandardValue and InheritsValueFromOtherItem is unconditionally reset to false.

The problem is the first two rows in the public function Field.GetValue(…)

    public string GetValue(bool allowStandardValue, bool allowDefaultValue)
    {
        this.containsStandardValue = 0;
        this.inheritsValueFromOriginalItem = 0;
        // ...
    }

This generates problems when working with items that receives their values from the StandardValues or Clones of items.

The same problem has already been spotted in Sitecore 6.4 by Sean Kearney

http://seankearney.com/post/Field-ContainsStandardValue-in-Sitecore-is-Buggy.aspx

I just verified the problem still exist in Sitecore 7.0. I reported the problem with issue number #396060.

Simple workaround, avoid calling HasValue:

    // field.Value calls GetValue(true, false) internally which is safe
    var fieldValue = field.Value;
    var hasValue = fieldValue != null;

Cloned items in Sitecore missing fields

While looping through the item.Fields collection for a cloned item only the Sitecore standard fields showed up.

After quite a bit of googling I finally found this:

http://sitecoreblog.alexshyba.com/2009/01/friday-gotcha-sitecoredataitemsitemfiel.html

Turns out you need to call Fields.ReadAll() to get a complete collection of fields and values for the Item.

 

Deserialize object from urlencoded data in C#

Ever needed to deserialize application/x-www-form-urlencoded POST-data or querystring-data into C# objects?

Simple usage

    public class Foo
    {
        public string Bar { get; set; }
        public string Baz { get; set; }
        public string Qux { get; set; }
    }
    var serializer = new ParamSerializer();
    var foo = serializer.Deserialize<Foo>("bar=text1&baz=text2&qux=text3");

You can also use it directly on a NameValueCollection, such as Request.Form or Request.QueryString

    var foo2 = serializer.Deserialize<Foo>(Request.Form);
    var foo3 = serializer.Deserialize<Foo>(Request.QueryString);

Sourcecode on GitHub:

https://github.com/unger/SimpleHttpHandler/blob/master/SimpleHttpHandler/RequestHelpers/ParamSerializer.cs

The underlying implementation is a C# port of the Javascript library jQuery.deparam:

https://github.com/chrissrogers/jquery-deparam/blob/master/jquery-deparam.js

Bundling and minification without querystring versioning

When using Microsoft.AspNet.Web.Optimization you get an annoying ?v=[hash] at the end of the bundle urls.

To get around this problem I created an extension for this available on Nuget and GitHub:

http://www.nuget.org/packages/Bundling.Extensions/

https://github.com/unger/Bundling.Extensions

Why is version revving in querystring a bad thing?

http://stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring