Skip to main content


Showing posts from 2013

Umbraco Razor - List Documents form Folder selected via the Media Picker

1) Create a Media Picker in your Document Type: "listFilesFromAParticularFolder"

 2) Create this Macro

@using Examine @using Examine.SearchCriteria @using UmbracoExamine @using System.Xml.XPath

@{  if (@Model.listFilesFromAParticularFolder != "" && @Model.listFilesFromAParticularFolder != null)  {                    dynamic imageFolder = Library.NodeById(@Model.listFilesFromAParticularFolder);                     <h2>Related Documents</h2>
                   foreach (var folder in imageFolder.Children)                       {                                    <p>&bull; <a href="@folder.Url">@folder.Name</a> (@folder.umbracoExtension)</p>                                    <p>@folder.shortDescriptionOfTheDocument</p>                                                        <div class="thinline"> </div>                                      }  }

 3) Insert the Macro in to …

Umbraco: Load Different Stylesheets depending on values in a Tick Box.

The purpose of this is to allow the user to influence the colours of a page by loading a different style-sheet. Using CSS, Umbraco and Razor.

STEP 1: Create a New Data Type 1)  Add a new Data Type called "Colours"

2) Make the Data Type a Checkbox List and add some Values to it for the User.


STEP 2: Add the Colours Option to the Document Type in the Backend
STEP 3: Add Seperate CSS Style Sheets with your CSS Rules For example:  colour-purple.css, colour-gree.css, color-orange.css, colour-pink.css 

STEP 4: Create Some Razor Code
...@if (@Model.pageColour == "Purple")
@Html.Raw("<link href=\"/css/colour-purple.css\" rel=\"stylesheet\" type=\"text/css\" media=\"all\" />");

@if (@Model.pageColour == "Pink")
@Html.Raw("<link href=\"/css/colour-pink.css\" rel=\"stylesheet\" type=\"text/css\" media=\"all\" />");

@if (@Model.pageColour == "…

Umbraco 4 Backend not showing CSS, Javascript or Sections after moving to Local Host

The issue below can be resolved by:

1) Rename the Folders: App_Browser and App_Data to something like, App_Browser_old and App_Data_old
2) Log in to your Umbraco Backend

If you receive errors like:
 "Values cannot be returned until Resolution is frozen" , Press the refresh button.

If you then get the message:

The website is restarting Please wait for 10s while we prepare to serve the page you have requested...

Go for a cup of tea and come back.

Umbraco 4 Backend Still not working?  In IIS --> Application Pools --> Change the Managed Pipeline to Classic instead of Integrated for the site

Umbraco Razor - List Subpages from Specific Node - byNodeId - Liist Lates News - Get Node By Id

@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.NodeFactory

@* Ensure that the Current Page has children, where the property umbracoNaviHide is not True *@
@if (Model.NodeById(1850).Children.Any())
<div class="sideBarWidget">
<h2><a style="color:#ffffff" href="@Model.NodeById(1850).Url" title="News">Scottish Medical Training<br />Latest News</a></h2>

        @* For each child page under the root node, where the property umbracoNaviHide is not True *@

         @foreach (var childPage in Model.NodeById(1850).Children.OrderBy("newsArticleDate descending").Take(4))
   <a href="@childPage.Url">@childPage.newsArticleTitle</a>  -  @childPage.newsArticleDate.ToString("dd MMM yyyy")

Umbraco Razor, Fill in a Dropdownlist or Mega drop down from DataTypes using a Function

@using Examine
@using Examine.SearchCriteria
@using UmbracoExamine
@using System.Xml.XPath

@helper fillDropDown(int thisPreValue, string thisLocation)

{XPathNodeIterator preValueRootElementIterator = umbraco.library.GetPreValues(thisPreValue);
XPathNodeIterator preValueIterator = preValueRootElementIterator.Current.SelectChildren("preValue", "");

<div class="col_4 responsive_thirds">

@Html.Raw("<li><strong><a href=\"#selectionmenu?location=" + @thisLocation + "\">" + @thisLocation + "</strong></li>")
@while (preValueIterator.MoveNext())

   @Html.Raw("<a href=\"#selectionmenu?location=" + @preValueIterator.Current.Value + "\">")



Umbraco Razor, Dealing with the Issue of state in Forms

<select name="fr" style="">
<option value="All">All Regions</option>
<option value="North" @if (region=="North"){@Html.Raw("selected")}>North</option>
<option value="East" @if (region=="East"){@Html.Raw("selected")}>East</option>
<option value="South East" @if (region=="South East"){@Html.Raw("selected")}>South East</option>
<option value="West" @if (region=="West"){@Html.Raw("selected")}>West</option>

Umbraco Razor Macro - Write out a Meta Data Title Automatically with page names

@inherits umbraco.MacroEngines.DynamicNodeContext

@if (@Model.pageTitle == "") // where pageTitle is a field name in  your doctype
if (@Model.Id != 1062) // where 1062 is the id of the home page

@Html.Raw("My Web Site Name | ")

foreach (var page in Model.Ancestors().OrderBy("Level"))
if (@page.Id != 1062)
@page.Name @Html.Raw(" | ");
if (@Model.Id != 1062)


Umbraco - Reset the Admin Password via SQL Server

To reset the admin password of a Umbraco website.

1. In Sql server, run the following script on the database:


UPDATE umbracoUser set userdisabled=0, userLogin='admin', userPassword='default' where id=0

2. In your web config file, find the "UsersMemnershipProvider" section, change the setting:


You should now be able to login using the Username: Admin and the Password: default ("the two sweetest words in the english language").

Umbraco Razor Display Links to Next, Previous and Parent page or node

The Following Razor code for Umbraco displays Next and Previous Buttons / Links and a Return to the Parent node Button / Link:

Example 1:
<umbraco:Macro runat="server" language="cshtml">
        if (@Model.Previous() != null) {
            <a href="@Model.Previous().Url">Previous Image</a>

var currentPg = Model;
<a href="@currentPg.Parent.Url">View all @currentPg.Parent.Name</a>

if (@Model.Next() != null) {
            <a href="@Model.Next().Url">Next Image</a>

Example 2: This Example shows you how to ignore pages that are hidden from the navigation in Umbraco.


if (@Model.Previous() != null) {
var node = Model.Previous();
if (node.Visible)
<a class="button green"  href="@Model.Previous().Url">@Model.Previous().PageTitle Images</a>

var currentPg = Model;
if (currentPg.Visible)
<a style="margin-left:…

Umbraco Inline Razor to refer to Stylesheets within a Template

Umbraco Inline Razor to refer to a Stylesheet Example: 
<umbraco:Macro runat="server" language="cshtml">
@{var myStyleSheet = "~/css/styles.css";}
<link rel="stylesheet"  href="@Href(myStyleSheet)" type="text/css" media="screen" />

@{var IEStyleSheet = "~/css/stylesIE.css";}
<!--[if lte IE 8]>
<link rel="stylesheet" href="@Href(myStyleSheet)" type="text/css" media="screen" />

Umbraco Inline Razor to refer to lots of Stylesheets, using the folder as the reference Example

<umbraco:Macro runat="server" language="cshtml">
@{var myStyleSheetFolder = "~/css/";}
<link href="@Href(myStyleSheetFolder)bootstrap.css" rel="stylesheet">
      <link rel="stylesheet" type="text/css" href="@Href(myStyleSheetFolder)theme.css">

A Quick Javascript Contact Form

function validateForm()
var x=document.forms["myForm"]["name"].value;
var x=document.forms["myForm"]["email"].value;

if (x==null || x=="" || y==null || y=="")
 document.getElementById('myDiv').innerHTML = '<span style="font-weight:bold;color:#800000">***Please fill in all requred fileds.</span>';
  return false;

<div id="myDiv"> </div>
<div id="comments">
<form action="thank-you.aspx" method="post" name="myForm" onsubmit="return validateForm()">
            <input type="text" name="name" id="name" value="" size="22">
            <label for="name"><small>Name (required)</small></label>
            <input type="…

UMBRACO Razor - Write Out a Sub String and Write out a Substring Encoding

The Highlighted lines below shows you how to write out a Substring from Razor in Umbraco.

1) @Html.Raw(@item.HomePageIntro.ToString().Substring(0, 290))
2)@item.HomePageIntro.ToString().Substring(0, 290)

(Example 1 will write out the HTML characters, Example 2 removes Encoding.)
Example of writing out a SubString in Umbraco using Razor @inherits umbraco.MacroEngines.DynamicNodeContext
@{ var numberOfItems = 6; }
    @foreach (var item in @Model.Children.Where("Visible").Take(numberOfItems))
        <figure><img src="images/triangle.jpg" alt=""></figure>
        <strong>@item.PageHeading </strong>
        <p>@Html.Raw(@item.HomePageIntro.ToString().Substring(0, 290))...</p>
<p class="more"><a href="@item.Url">FIND OUT MORE</a></p>

Umbraco - XSLT - Replace String, Replace String in Selection from DropDownList Mulitiple

The following code highlights the string replacement function in umbraco:

The example below places a space after each comma (basically to help with the formatting) from values selected from a multiple drop down list ("placementHospital").

The important bit is highlighted.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
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:Exsl…