Posts

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 Razor getNodes of a certain type

var getNodes = Model.AncestorOrSelf().Descendants("ShopItem"); // Programmes Nodes int numberOfNodes = getNodes.Count(); @numberOfNodes

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

Image
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.

Example:


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 Razor Get by template Id

@Html.Raw("Hello: " + @Model.NodeById(listingPage).Children.Where("template=1201").Count());

Umbraco Razor test to see if on current page

if(@node.Url == @Model.Url)
{
<li>
<a href="@node.Url"><span style="text-decoration:underline">@node.Name</span></a>                                      
</li>
}
else
{
<li>
<a href="@node.Url">@node.Name</a>                                      
</li>
}

Umbraco Razor Add Line Breaks to Textbox multiple

@if (@Model.address != "")
{

var output = (umbraco.library.ReplaceLineBreaks(@Model.address));
<p>@Html.Raw(@output)</p>
}

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

Image
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>


    <ul>          
        @* 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))
       {
            <li>
   <a href="@childPage.Url">@childPage.newsArticleTitle</a>  -  @childPage.newsArticleDate.ToString("dd MMM yyyy")
            </li>
        }
    </ul>
</div>
}

c# Umbraco Razor Display List of all Session Varioables

@inherits umbraco.MacroEngines.DynamicNodeContext
<p>Session Variables</p>

@HttpContext.Current.Items.Count:<br />


@for (int i =1; i <= @HttpContext.Current.Session.Contents.Count; i++)
{
   @HttpContext.Current.Session.Keys[i-1];
<br />

}

Umbraco Razor Media Picker - how to display a link from the media picker

var node = Library.NodeById(Model.videoFollowUpLink);
    <a href="@node.Url">@node.Name</a>

Umbraco Razor Last Updated Date and Time

@inherits umbraco.MacroEngines.DynamicNodeContext


<p><small>This page was last updated on: @Model.UpdateDate.ToString("dd.MM.yyyy") at @Model.UpdateDate.ToString("HH.mm") </small></p>



Example Output:

This page was last updated on: 01.08.2013 at 13.18

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);
preValueRootElementIterator.MoveNext();
XPathNodeIterator preValueIterator = preValueRootElementIterator.Current.SelectChildren("preValue", "");

<div class="col_4 responsive_thirds">
        <ul>

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

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

}
</ul>
</div>
}



    @fillDropDown(1083,"Scotland")
   @fi…

Umbraco Razor, Dealing with the Issue of state in Forms

Solution:
<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>
</select>

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)
{
@Model.Name
}
}

}
else
{
@Model.pageTitle
}

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:


USE  INSERT-YOUR-DATABASE-NAME-HERE
GO

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:


passwordFormat="Hashed"
to
passwordFormat="Clear"


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>
        }
    }
</umbraco:Macro>



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" />
<![endif]-->
</umbraco:Macro>



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

<script>
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;
  }
}
</script>

<div id="myDiv"> </div>
<div id="comments">
<form action="thank-you.aspx" method="post" name="myForm" onsubmit="return validateForm()">
          <p>
            <input type="text" name="name" id="name" value="" size="22">
            <label for="name"><small>Name (required)</small></label>
          </p>
          <p>
            <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))
or
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))
    {<article>
        <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>
      </article>}

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;"> ]>
<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:Exsl…