5

I have added a new sc.variable (just like dataFolder, tempFolder). We see the setting in showconfig.aspx but it returns an empty string when getting it with Sitecore.Configuration.Settings.GetSetting("mySetting").

I tried playing around with lowercase/uppercase as Sitecore.Configuration.Settings.GetSetting("dataFolder") returns empty but Sitecore.Configuration.Settings.GetSetting("DataFolder") returns the actual value; but didn't work either. Is it not possible to read custom sc.variable settings?

UPDATE: We are using sc.variable so we can read this setting from other .config files e.g. <bla value="$(mySetting)xxx /> but we also happen to need to read it programmatically.

7

All the variables from sc.variable are defined in two places For example tempFolder is defined :

 <sc.variable name="tempFolder" value="/temp" />

and

 <setting name="TempFolder" value="$(tempFolder)" /> 

The value of the TempFolder you can get using :

return Settings.GetSetting("TempFolder", "/temp") where the second parameter is default value.

So I recommend to declare your variable not like a sc.variable, but like normal setting. Or if you declare like a sc.variable declare a setting with a variable parameter.

Update: How sitecore replace this variables : On Sitecore.Configuration.ConfigReader it has a method:

protected virtual void ReplaceGlobalVariables(System.Xml.XmlNode rootNode)
{
    Assert.ArgumentNotNull(rootNode, "rootNode");
    System.Xml.XmlNodeList xmlNodeList = rootNode.SelectNodes(".//sc.variable");
    StringDictionary stringDictionary = new StringDictionary();
    foreach (System.Xml.XmlAttribute xmlAttribute in rootNode.Attributes)
    {
        string name = xmlAttribute.Name;
        string @string = StringUtil.GetString(new string[]
        {
            xmlAttribute.Value
        });
        if (name.Length > 0)
        {
            string key = "$(" + name + ")";
            stringDictionary[key] = @string;
        }
    }
    for (int i = 0; i < xmlNodeList.Count; i++)
    {
        string attribute = XmlUtil.GetAttribute("name", xmlNodeList[i]);
        string attribute2 = XmlUtil.GetAttribute("value", xmlNodeList[i]);
        if (attribute.Length > 0)
        {
            string key2 = "$(" + attribute + ")";
            stringDictionary[key2] = attribute2;
        }
    }
    if (stringDictionary.Count == 0)
    {
        return;
    }
    this.ReplaceGlobalVariables(rootNode, stringDictionary);
}
|improve this answer|||||
  • 2
    You don't say it directly, but you do imply that sc.variable must have a <setting>. Just to clarify, sc.variable does not require a <setting> and can, in fact, be used in all config attribute and element values. – Zachary Kniebel Jul 17 '17 at 12:56
  • We are using sc.variable so we can read the setting in other config files. This is not possible with <setting>, right? – josedbaez Jul 17 '17 at 15:20
6

While the answer given by Sitecore Climber is spot on, I'd like to clarify a bit on the difference between sc.variable and a setting.

If you look at how sc.variable is used throughout the (original) Sitecore.config, it is almost always in places that would differ by environment. So the config defines a sc.variable for dataFolder for instance, and then an actual setting for it later like <setting name="dataFolder" value="$(dataFolder" />.

The reason for this is, that you can then isolate all the environment specific settings in your solution, and have a simple patch for them. All sc.variable declarations would be at the top of the config file, so they could be easily found and replaced.

I don't often see this used any more however. Sitecore later introduced config patching and I think, generally, people just use different layers of patch config files to achieve the same result; making the use of sc.variable largely obsolete.

Further reading:

|improve this answer|||||
  • I actually try to use sc.variable for as many instance-specific values as I can in my solutions. In my App_Config/Include folder I have two child folders zz.Variables and zzz.Patches. It's great for some things, but patching is where the magic happens. Remember that you don't have to just use them in settings. You can use them anywhere. – Zachary Kniebel Jul 17 '17 at 12:54
  • We are using sc.variable so we can read from other config files. Is this possible with <setting>? – josedbaez Jul 17 '17 at 15:20
  • Sure. But as pointed out, you cannot resolve an sc.variable directly. You have to put it into a <setting> element and then read it using Configuration.GetSetting as shown above. – Mark Cassidy Jul 17 '17 at 15:23
  • Cool, that's what we ended up doing. Was trying to figure out if I was missing something. Thanks – josedbaez Jul 17 '17 at 15:26
2

Here is how I used Sitecore PowerShell Extensions to read a specific variable value.

$config = [Sitecore.Configuration.Factory]::GetConfiguration()
$speSourceFolder = $config.sitecore."sc.variable" | 
    Where-Object { $_.Name -eq "speSourceFolder" } | 
    Select-Object -First 1 -Expand value

enter image description here

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.