Creating a Standard Values Provider to Pull Values From Another Item

Standard values, standard values, standard values.  Those 2 little words were pounded into my head during Sitecore training.  “Use to set default values on newly created Sitecore items.”  “Always create standard values.”  That’s great, got it.  But what happens if you want to pull those values from somewhere else?

In order to override standard values we need to change our Standard Values Provider to a custom one.  For the sake of this exercise, I will create a custom provider that will return a random guid for a particular field.

1st step.  Write our custom Standard Values Provider.  Our class needs to set a base class of StandardValuesProvider.  Once that is set up, we need to override the GetStandardValue method.  In that function is where we can set our logic for the value we want to return.  (see code example)

namespace Demo
{
    public class CustomStandardValuesProvider : StandardValuesProvider
    {
        public override string GetStandardValue(Sitecore.Data.Fields.Field field)
        {
            //check to see if this is the field we want to override the value for
            if (field.ID == ID.Parse("{1B7CCE21-BB8E-499E-A837-7AFF431FD705}"))
            {
                //we want to handle this field, return a new Guid (as a string)
                return Guid.NewGuid().ToString();
            }

            //if we fall through return the base
            return base.GetStandardValue(field);
        }
    }
}

2nd step.  Patch our new provider into the configuration.  Never modify the Sitecore config files unless you absolutely have to.  Always use patch files.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <standardValues>
      <providers>
        <add name="sitecore" type="Demo.CustomStandardValuesProvider, Demo" patch:instead="add[@type='Sitecore.Data.StandardValuesProvider, Sitecore.Kernel']"></add>
      </providers>
    </standardValues>
  </sitecore>
</configuration>

 

Real world example.

Recently while working on a project, I needed the ability to pull standard values from a common location.  In our solution, we have a common item, which is referenced by multiple other items in the tree (site items).  (Think of a product catalog with a common product item that has values that are shared across multiple site items).  We wanted one place for content authors to manage common data for these items, however make it easy for developers to access those values.  On our “site items”, we have standard values for certain fields coming from this common item.  When retrieving data for that item, we only need to concern ourselves with the site item, and don’t need to get data from those referenced items.

Advertisements

One Response to “Creating a Standard Values Provider to Pull Values From Another Item”

  1. pradeep gupta Says:

    Nice article John,

    there is a scenario, where I am patching my customStandardValueProvider after StandardValueProvider and for that I tried

    but it gives me an error: ‘patch’ is an undeclared prefix


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: