Get File Path

Get File Path

File Paths

  • Use the MapPath method to assign the physical file path of an item to a string
  • Sitecore compares case when evaluating item paths
  • Start file paths provided to the MapPath() method with a "/" or "~/" 
  • If the file path is blank, contains a backslash (\) or contains the string "://", the current path is returned instead

Controls featured in this example:

  • Sitecore Web Control
  • XSLT Rendering

Classes featured in this example:

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

File Path C# Web Control Code

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

string aspMapPath1 = System.Web.HttpContext.Current.Server.MapPath("/images/buttons/search.gif");
string aspMapPath2 = System.Web.HttpContext.Current.Server.MapPath("~/images/buttons/search.gif");
           
string apiMapPath1 = Sitecore.IO.FileUtil.MapPath("/images/buttons/search.gif");
string apiMapPath2 = Sitecore.IO.FileUtil.MapPath("~/images/buttons/search.gif");

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("{0}", "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 : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "Using Asp.Net");
output.RenderEndTag(); //P

if(!string.IsNullOrEmpty(aspMapPath1))
    output.Write("{0}&#58;&#160;{1}", @"/images/buttons/search.gif", "<b>" + aspMapPath1 + "</b><br/>");

if (!string.IsNullOrEmpty(aspMapPath2))
    output.Write("{0}&#58;&#160;{1}", @"~/images/buttons/search.gif", "<b>" + aspMapPath2 + "</b>");
           
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "Using Sitecore API");
output.RenderEndTag(); //P

if (!string.IsNullOrEmpty(apiMapPath1))
    output.Write("{0}&#58;&#160;{1}", @"/images/buttons/search.gif", "<b>" + apiMapPath1 + "</b><br/>");

if (!string.IsNullOrEmpty(apiMapPath2))
    output.Write("{0}&#58;&#160;{1}", @"~/images/buttons/search.gif", "<b>" + apiMapPath2 + "</b>");

output.RenderEndTag(); //Div


File Path C# Web Control Output

Using Asp.Net

/images/buttons/search.gif: C:\inetpub\wwwroot\TIC66Live\Website\images\buttons\search.gif
~/images/buttons/search.gif: C:\inetpub\wwwroot\TIC66Live\Website\images\buttons\search.gif

Using Sitecore API

/images/buttons/search.gif: C:\inetpub\wwwroot\TIC66Live\Website\images\buttons\search.gif
~/images/buttons/search.gif: C:\inetpub\wwwroot\TIC66Live\Website\images\buttons\search.gif

Sources