<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>$name &#8211; blog.boro2g .co.uk</title>
	<atom:link href="https://blog.boro2g.co.uk/tag/name/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.boro2g.co.uk</link>
	<description>Some ideas about coding, dev and all things online.</description>
	<lastBuildDate>Mon, 12 Mar 2012 12:06:58 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.8</generator>
	<item>
		<title>$name in your Sitecore content</title>
		<link>https://blog.boro2g.co.uk/name-in-your-sitecore-content/</link>
					<comments>https://blog.boro2g.co.uk/name-in-your-sitecore-content/#comments</comments>
		
		<dc:creator><![CDATA[boro]]></dc:creator>
		<pubDate>Mon, 12 Mar 2012 12:05:33 +0000</pubDate>
				<category><![CDATA[Sitecore]]></category>
		<category><![CDATA[$name]]></category>
		<category><![CDATA[sitecore]]></category>
		<guid isPermaLink="false">http://blog.boro2g.co.uk/?p=166</guid>

					<description><![CDATA[<p>One issue that seems to regularly occur in Sitecore builds is pages showing $name in the content. Its the sort of problem that seems to creep into projects the further through you get. Why does it happen? When you setup Sitecore templates they are typically built up from several other templates eg: A content page [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/name-in-your-sitecore-content/">$name in your Sitecore content</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>One issue that seems to regularly occur in Sitecore builds is pages showing $name in the content. Its the sort of problem that seems to creep into projects the further through you get.</p>
<p><strong>Why does it happen?</strong></p>
<p>When you setup Sitecore templates they are typically built up from several other templates eg:</p>
<ul>
<li>A content page is comprised from:</li>
<ul>
<li><em>Page Title</em> field section</li>
<li><em>Meta Data</em> field section</li>
<li>etc</li>
</ul>
</ul>
<p>Down the line you decide you want to add <em>&#8216;Page Content &#8211; (Header and Body fields)&#8217;</em> to all pages so you setup a new Field Section template along with corresponding fields and <em>__standard values</em>. So that new pages automatically push the item name to the Header field, you set the <em>__standard values</em> to contain $name.</p>
<p>All this gets published and you then come to check the front end of the site &#8211; pages created before adding the Page Content field section now have the new header and body fields but the header field shows $name <img src="https://s.w.org/images/core/emoji/15.0.3/72x72/1f641.png" alt="🙁" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>The reason being these fields are inheriting their value from <em>__standard values</em> &#8211; variables such as $name are processed when items are created. See <a title="http://adeneys.wordpress.com/2009/12/29/custom-tokens-and-nvelocity-for-item-creation/" href="http://adeneys.wordpress.com/2009/12/29/custom-tokens-and-nvelocity-for-item-creation/" target="_blank">http://adeneys.wordpress.com/2009/12/29/custom-tokens-and-nvelocity-for-item-creation/</a> for info on how to create custom replacement tokens. There is also more information on <a title="http://learnsitecore.cmsuniverse.net/en/Developers/Articles/2010/08/Standard-values-in-sitecore.aspx" href="http://learnsitecore.cmsuniverse.net/en/Developers/Articles/2010/08/Standard-values-in-sitecore.aspx" target="_blank">http://learnsitecore.cmsuniverse.net/en/Developers/Articles/2010/08/Standard-values-in-sitecore.aspx</a></p>
<p><strong>How to solve the problem?</strong></p>
<p>When items are created they are processed by a set of pipelines. An example of this is <em>expandInitialFieldValue </em>pipeline<em>.</em> Out the box, this makes use of  the <em>MasterVariablesReplacer. </em></p>
<p>You now have a couple options, either you can call the <em>MasterVariablesReplacer</em> for the problematic items or you could manually script the same functionality. The following code demonstrates the manual approach:</p><pre class="crayon-plain-tag">&amp;lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; %&amp;gt;

&amp;lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&amp;gt;

&amp;lt;%@ Import Namespace=&quot;Sitecore.Data.Items&quot; %&amp;gt;
&amp;lt;%@ Import Namespace=&quot;Sitecore.Data.Fields&quot; %&amp;gt;
&amp;lt;%@ Import Namespace=&quot;Sitecore.Data&quot; %&amp;gt;

&amp;lt;script runat=&quot;server&quot;&amp;gt;    

    public void FixNames_Click(object sender, EventArgs e)
    {
        //note, this doesn't error check the root guid is valid item
        Item root = Database.GetDatabase(&quot;master&quot;).GetItem(new ID(new Guid(RootIdTextBox.Text)));
        int fixes = 0;

        using (new Sitecore.SecurityModel.SecurityDisabler())
        {
            fixes = RecurseAndFix(root);
        }

        ResultsLiteral.Text = String.Format(&quot;&amp;lt;hr /&amp;gt;Fixed {0} cases of '$name'&quot;, fixes);
    }

    private int RecurseAndFix(Item root)
    {
        int fixes = 0;

        if (root.Template != null)
        {
            foreach (TemplateFieldItem field in root.Template.Fields)
            {
                Field existingField = root.Fields[field.ID];

                if (existingField != null)
                {
                    bool fixMe = false;

                    if (existingField.Value.Equals(&quot;$name&quot;))
                    {
                        fixMe = true;
                    }

                    if (existingField.ContainsStandardValue &amp;amp;&amp;amp; existingField.GetStandardValue().Equals(&quot;$name&quot;))
                    {
                        fixMe = true;
                    }

                    if (fixMe)
                    {
                        using (new EditContext(root))
                        {
                            existingField.Value = root.Name;
                            fixes++;
                        }
                    }
                }
                else
                {
                    if (root.Template.StandardValues[field.ID].Equals(&quot;$name&quot;))
                    {
                        using (new EditContext(root))
                        {
                            root[field.ID] = root.Name;
                            fixes++;
                        }
                    }
                }
            }
        }

        foreach (Item child in root.Children)
        {
            fixes += RecurseAndFix(child);
        }

        return fixes;
    }

&amp;lt;/script&amp;gt;

&amp;lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &amp;gt;
&amp;lt;head id=&quot;Head1&quot; runat=&quot;server&quot;&amp;gt;
    &amp;lt;title&amp;gt;'$name' Fixer &amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;form id=&quot;form1&quot; runat=&quot;server&quot;&amp;gt;
        &amp;lt;p&amp;gt;'$name' Fixer &amp;lt;/p&amp;gt;
        &amp;lt;p&amp;gt;
            Running the code on this page searches through all the content in the tree and replaces
            any '$name' values with the item's name.
        &amp;lt;/p&amp;gt;
        &amp;lt;h2&amp;gt;Note: Be careful, only run on content nodes and children and not templates!&amp;lt;/h2&amp;gt;
        &amp;lt;hr /&amp;gt;
        &amp;lt;div&amp;gt;
            Root id: &amp;lt;asp:TextBox runat=&quot;server&quot; ID=&quot;RootIdTextBox&quot; Width=&quot;600&quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;
            &amp;lt;asp:Button runat=&quot;server&quot; OnClick=&quot;FixNames_Click&quot; Text=&quot;Fix '$name'&quot; /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;asp:Literal runat=&quot;server&quot; ID=&quot;ResultsLiteral&quot; /&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;</pre><p>Its worth noting this can introduce some interesting challenges if Language Fallback is used on your site. The script above works fine for content items on a single language site. If you need similar functionality when language fallback is in place it may actually be meaningful to set $name = &#8221; for non-primary languages. This will ensure the fallback will occur correctly, rather than finding $name and thinking it is valid content.</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/name-in-your-sitecore-content/">$name in your Sitecore content</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.boro2g.co.uk/name-in-your-sitecore-content/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
