Skip to main content

Umbraco - Using GetPreValueAsString to Return the String of a Field and not its value

If you have a field in Umbraco that is getting returned as a number instead of the name, see the highlighted text below.  1 - In a Macro . 2 - In a Template.

1.  IN A XSLT MACRO:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxml="urn:schemas-microsoft-com:xslt"
  xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:PS.XSLTsearch="urn:PS.XSLTsearch"
  exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets PS.XSLTsearch ">
<xsl:output method="xml" omit-xml-declaration="yes" />
<xsl:param name="currentPage"/>
<!-- Don't change this but create a 'number' element in your -->
<!-- macro with the alias of 'numberOfItems' -->
  <xsl:variable name="recordsPerPage" select="20" />
  <xsl:variable name="newsPages" select="$currentPage/* [@isDoc]"/>
  <xsl:variable name="pageNumber" >
    <xsl:choose>
      <xsl:when test="umbraco.library:RequestQueryString('page') &lt;=0 or string(umbraco.library:RequestQueryString('page')) = '' or string(umbraco.library:RequestQueryString('page')) = 'NaN'">0</xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="umbraco.library:RequestQueryString('page')"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="numberOfItems" select="count($newsPages)"/>
 
<xsl:template match="/">
<!-- The fun starts here -->
 
  <xsl:if test="$numberOfItems &gt; 0">
    <div id="search-results" class="listing search-listing">
    <xsl:variable name="thisDiscipline" select="$currentPage/discipline"/>
      <xsl:if test="$thisDiscipline !=''"><h1><xsl:value-of select="string(umbraco.library:GetPreValueAsString($thisDiscipline))"/></h1> </xsl:if>
      <xsl:if test="$thisDiscipline =''"><h1>&nbsp;</h1> </xsl:if>
      <xsl:for-each select="$newsPages">
     
 
        <xsl:sort select="publicationDate" order="descending"/>
        <xsl:if test="position() > $recordsPerPage * number($pageNumber) and  
position() &lt;= number($recordsPerPage * number($pageNumber) +  
$recordsPerPage )">

    <div class="int">
      
               <div class="item">
                    <div class="image">
                        <img height="85" width="85"
                             alt=""
                             src="/media/content/types/publication.gif" />
                    </div>
                    <div class="description">                  
      <p class="title">
           <a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a>
      </p>           
       <div class="metadata">
                                    <span><strong>Theme</strong>: <xsl:value-of select="theme"/></span>                             
                                    <span><strong>Career stage</strong>: <xsl:value-of select="careerStage"/></span>             
        <span><strong>Format</strong>: <xsl:value-of select="format"/></span>                         
                                    <!--<span><strong>ISBN</strong>: <xsl:value-of select="iSBN"/></span>-->
                        </div>                 
<p><xsl:value-of select="documentOverview"/></p>               
                   </div><!--Description -->   
   
    <div class="cta"> <a href="{umbraco.library:NiceUrl(@id)}">Find Out More</a>
</div>
               
    </div>
  </div>
 
               
        </xsl:if>
      </xsl:for-each>
  </div>         
    <xsl:if test="$numberOfItems > $recordsPerPage">
      <p id="pager">
        <xsl:call-template name="pageNumbers">
          <xsl:with-param name="pageIndex" select="1"/>
        </xsl:call-template>
      </p>
    </xsl:if>
     
  </xsl:if>
</xsl:template>

  <!-- individual links to each page of results for the search -->
  <xsl:template name="pageNumbers">
    <xsl:param name="pageIndex"/>
    <xsl:if test="$pageIndex != 1"> | </xsl:if>
    <xsl:choose>
      <xsl:when test="$pageIndex - 1 = $pageNumber">
        <!-- current page -->
        <strong>
        <xsl:value-of select="$pageIndex"/>
        </strong>
      </xsl:when>
      <xsl:otherwise>
        <!-- other pages -->
        <a>
          <xsl:attribute name="href">
            ?page=<xsl:value-of select="$pageIndex - 1"/>
          </xsl:attribute>
          <xsl:value-of select="$pageIndex"/>
        </a>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:if test="$pageIndex * $recordsPerPage &lt; $numberOfItems">
      <xsl:call-template name="pageNumbers">
        <xsl:with-param name="pageIndex" select="$pageIndex + 1"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>


2. IN A TEMPLATE:

<%@ Master Language="C#" MasterPageFile="~/masterpages/Page.master" AutoEventWireup="true" %>
<asp:Content ContentPlaceHolderID="NHSNESPageContentPlaceholder" runat="server">

 <!-- LEADER -->
  <umbraco:Macro Alias="LeaderNormalPage" runat="server"></umbraco:Macro>
 
  <div id='content'>     
      <div class='page'>
       
        <!-- SIDE NAVIGATION -->
        <div class='column-left left-nav'>
        <umbraco:Macro Alias="SideNavigation" runat="server" />
        </div>
       
        <div class='column-middle page-content'>
       
          <h1><umbraco:Item ID="Item1" field="pageName" runat="server" /></h1>
    
         
          <div class="metadata">
            <span><strong>Discipline:</strong>
           <umbraco:Item runat="server" field="discipline" xslt="umbraco.library:GetPreValueAsString({0})" />
            </span>
              <span><strong>Office:</strong> <umbraco:Item field="office" runat="server"></umbraco:Item>
            </span>
              <span><strong>Location:</strong> <umbraco:Item field="location" runat="server"></umbraco:Item>
          </span>
             
              <span><strong>Overview:</strong> <umbraco:Item field="jobOverview" useIfEmpty="jobOverview" runat="server"></umbraco:Item>You will provide operational project administration support to a range of uni- and multi-disciplinary projects within the Nursing, Midwifery & Allied Health Professions Directorate.
            </span>
              <span><strong>Closing date:</strong> <umbraco:Item field="closingDate" useIfEmpty="closingDate" formatAsDate="true" runat="server"></umbraco:Item>
            </span></div>
               
         
         
          <p class="subtitle"><umbraco:Item field="pageIntroduction" runat="server" /></p>
          <umbraco:Item field="mainContent" runat="server" />
<umbraco:Macro Alias="NESPublicationDisclaimer" runat="server"></umbraco:Macro>
       
  
          <umbraco:Macro Alias="relatedLinks" runat="server" />
        </div>
       
        <div class='column-right'>
          <!-- CURRENT INITIATIVES -->
          <umbraco:Macro ID="Macro4" Alias="FeaturedInitiativesBox" runat="server" />
          <!-- CONTACT FINDER -->
          <umbraco:Macro ID="Macro5" Alias="ContactFinderBox" runat="server" />
          <!-- RELATED RESOURCES -->
          <umbraco:Macro ID="Macro6" Alias="RelatedResources" runat="server" />
        </div>
      </div>
      <div class='pre-footer'>
      <a href='#nav'>Back to top</a>
      </div>
   
</div>
</asp:Content>

Comments

Popular posts from this blog

Create a .NET Contact Form that Gets the Last Url Visited in C# Can also be Used in Umbraco

This is a Simple Contact From done in C# .Net and also can be used in Umbraco.  Paste the code in to Visual Web Developer for a better view. The important bits are coment in the second file.  One of the Commented out lines highlights how to get the mailto address from an Umbraco field. The code Below includes the myContactForm.ascx.cs codebehind file and the myContactForm.ascx file. 1) The myContactForm.ascx file:  <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactFormforPublications.ascx.cs" Inherits="usercontrols_ContactFormPublications" %> <div class="contact-form" id="contactForm" runat="server">     <h3>Request a Publication</h3>     <div class='int'>         <form runat="server">         <asp:Panel DefaultButton="contactFormSubmit" runat="server">         <fieldset>             <div class='

Umbraco 7 - C# Razor - Passing a parameter variable from a Template to a Partial View.

Passing a parameter variable from a Template to a Partial View Example - Passing a Page Id to a Partial View. In the example below we are passing 3 page Ids (1090. 1091, 1092) to a partial view.  The Partial View will display the details for each of these nodes on a home page.  See final output below. 1) In your Template: @Html.Partial("HomePageLinksToLandingPages", new ViewDataDictionary{{ "pageId", 1090}}) @Html.Partial("HomePageLinksToLandingPages", new ViewDataDictionary{{ "pageId", 1091}}) @Html.Partial("HomePageLinksToLandingPages", new ViewDataDictionary{{ "pageId", 1092}}) 2) The Partial View: @inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ var thisPageId = 1090; var stringPage = @ViewData["pageId"]; thisPageId = Convert.ToInt32(stringPage); } <div class="col-md-4"> <arti