Map File Paths

Map File Paths

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Configuration.Settings
  • Sitecore.Data.Items.Item
  • Sitecore.IO.FileUtil
  • System.Web.HttpContext

Map File Paths Configuration

  • <configuration>
    • <sitecore>
      • <settings>
        • <sc.variable name="dataFolder" value="C:\Inetpub\wwwroot\TIC2\Data\" />

Map File Paths C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
if (item == null) return;

string dataFolder = Sitecore.Configuration.Settings.DataFolder;
if (string.IsNullOrEmpty(dataFolder)) return;

string fileName = "readme.txt";
string filePath = Sitecore.IO.FileUtil.MapDataFilePath(fileName);
string fileExists = Sitecore.IO.FileUtil.Exists(filePath).ToString();
if (string.IsNullOrEmpty(filePath)) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//using &#58; for colon and &#63; for question mark and &#160; for space
output.Write("{0}&#58;&#160;{1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}&#58;&#160;{1}", "Data Folder", dataFolder);
output.RenderEndTag(); //P
 
output.Write("{0}&#58;&#160;{1}", "File Name", "<b>" + fileName + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "File Path", "<b>" + filePath + "</b><br/>");
output.Write("{0}&#63;&#160;{1}", "File Exists", "<b>" + fileExists + "</b>");

output.RenderEndTag(); //Div


Map File Paths C# Web Control Output

Data Folder: C:\Inetpub\wwwroot\TIC66Live\Data\

File Name: readme.txt
File Path: C:\Inetpub\wwwroot\TIC66Live\Data\readme.txt
File Exists? False

Notes

  • A readme.txt file was added to the Data folder on this site
  • The MapDataFilePath method maps specified file names to the C:\inetpub\wwwroot\WebsiteRoot\Data folder by default, not the C:\inetpub\wwwroot\WebsiteRoot\Website\data folder