Posts

Umbraco 8 - Calculate number of days past / between two dates

  DateTime parsedDate = DateTime.Parse(item.Value<DateTime>("testimonailDate").ToString("dd MMMM yyyy")); var daysPast = (DateTime.Today - parsedDate).TotalDays;  @if(daysPast == 1)                                                 {                                                     <span class="days-ago">@daysPast  day ago</span>                                                 }                                                 else             ...

Umbraco 9 - Strip HTML and Truncate text output

Image
Umbraco 9 - Strip HTML and Truncate text/html output.  The code below can be used for truncating/shortening text and striping out HTML tags in Umbraco 9 to display shorter text useful for landing pages. Instructions:  Replace "bodyText" with the reference to the property you would like to truncate. Code: Example output:

Umbraco 9 - Left hand sub page navigation code snippet.

Image
Umbraco 9 - Left hand sub page navigation code C# RAZOR snippet.  The code below can be used for left hand navigation in Umbraco 9 to create left hand/sub page navigation.  Instructions:  Create a partial _leftHandNavigation  Reference the partial in your Umbraco template, like this: @Html.Partial("_leftHandNavigation") Code: Example output: https://www.buymeacoffee.com/markdevelopment/umbraco-side-bar-left-hand-navigation

Umbraco 8 Convert Line Breaks for TextArea Example

 Umbraco 8  Convert Line Breaks for TextArea Example @Html.ReplaceLineBreaksForHtml( @Model.Value("lineOne") .ToString() )

Umbraco 8 Pagination

Umbraco 8 Pagination / Paging: @inherits Umbraco.Web.Mvc.UmbracoViewPage @{     Layout = "master.cshtml";         var pageSize = 8;     if(Model.HasValue("numberOfItemsPerPage")){     pageSize = Model.Value<int>("numberOfItemsPerPage");}         var page = 1; int.TryParse(Request.QueryString["page"], out page);     @* This line assumes you have a property called lastUpdated*@     var items = Model.Children().OrderByDescending(x => x.Value("lastUpdated"));     @* Remove the line above and use this line to keep it simple *@     @* var items = Model.Children()); *@             var totalPages = (int)Math.Ceiling((double)items.Count() / (double)pageSize);     if (page > totalPages)     {       ...

Umrbaco 8 Date Formatting

Format a Date in Umbraco 8 @(Model.Value<DateTime>("publicationDate").ToString("dd MMMM yyyy")) or in a foreach loop: @(item.Value<DateTime>("publicationDate").ToString("dd MMMM yyyy"))

Umbraco 8 Umbraco Navi Hide - Hide Page in foreach

Example of using umbracoNaviHide in Umbraco 8: @foreach (var item in @Model.AncestorOrSelf(3).Children() .Where(x => x.IsVisible())) { }

Umbraco 8 OrderBy a collection by a Custom Field Such as a Date

This is a snippet for Umbraco 8  to Order a collection by a Custom Field Such as a Date. @{     var selection = Umbraco.Content(Model.Id)     .ChildrenOfType("publication")     .Where(x => x.IsVisible())     .OrderBy( x => x.Value("publicationDate")); } @foreach (var item in selection.Take(1)) {      <h2><a href="@item.Url" title="@item.Name">@item.Name</a></h2> }

Umbraco 8 Umbraco Strip and Umbraco Truncate - usefull for landing pages

Umbraco 8: <p>@Html.StripHtml(@Html.Truncate(@item.Value("bodyText").ToString(), 65))   (Thanks to A.S for the above)   Umbraco 7:    <p>@Umbraco.StripHtml(@Umbraco.Truncate(@Umbraco.Content(1067).bodyText, 300))</p>

Umbraco 8 Metadata Snippet, keywords, meta title.

Umbraco 8 Metadata Snippet, keywords, meta title, meta description: The following is a snippet that may help with generating metadata. I created this as a partial and added it to the Main Template. If the metadata fields are not filled in on the backend by the editor, they are generated from the Page Name and the Name of the Website (applicationName). ---- In the example the backend meta fields are called: "metaTitle"   (textstring) "metaDescription"   (textstring) "metaKeywords" (tags) ---- @inherits Umbraco.Web.Mvc.UmbracoViewPage @{     var applicationName = "My Website Name"; } @if(Model.HasValue("metaTitle")){     <title>@Model.Value("metaTitle")</title> }  else {     if(Model.Id == Umbraco.ContentAtRoot().FirstOrDefault().Id)     {         <title>@applicationName</title>     }     else  ...

Umbraco 7 Umbraco Strip and Umbraco Truncate - usefull for landing pages

<p>@Umbraco.StripHtml(@Umbraco.Truncate(@Umbraco.Content(1067).bodyText, 300))</p>

Umbraco 7 Get Image Crop from Media ID for specific image,simple version razor sniper

Image
In Umbraco 7 to get a image crop from a image media item in the media folder, suppply the Media id number and the name of the image crop:  <img src="@Umbraco.Media( 1297 ).GetCropUrl("umbracoFile"," i mageCropName ")" class="img-responsive">

Umbraco 7 Get Crops from Multi Media Picker for Image Gallery

Working in 7.9.2: @inherits Umbraco.Web.Mvc.UmbracoTemplatePage @if(CurrentPage.HasValue("galleryImages"))                     {                         <div class="row">                                                     @{                             char[] splitChar = { ',' };                             string[] ids = CurrentPage.galleryImages.ToString().Split(splitChar, StringSplitOptions.RemoveEmptyEntries); ...

Umbraco 7 Get Media images by ID when using the Multi Media Picker to display a Gallery

Umbraco 7 Get Media images by ID when using the Multi Media Picker This is usefull when creating a gallery of images. 1) In your Doctype add the Multiple Media Picker 2) Umbraco 7 Razor code snippet partial: @inherits Umbraco.Web.Mvc.UmbracoTemplatePage @helper RemoveExtensions(string stringIn) {  var thisTitle = stringIn;                                         thisTitle = thisTitle.Replace(".jpg", "");                                         thisTitle = thisTitle.Replace(".png", "");                                       ...

Convert an SDF file to a MDF - File useful for Umbraco 7

How to convert a SDF Compact Database file to a full blown SQL Server MDF file. PART ONE (change the SDF file to MDF file): a) Install SQL Server Compact Toolbox runtime b) Start SQL Server Compact Toolbox runtime c) Select Add and Browse to the SDF file that you want to convert d) Select Sctipt Database Scheme and Data (this will produce a script) PART TWO (import the database in to SQL Server Management Studio) a) In SQL Server Management Studio create a Blank Database b) Right Click on the new database and select New Query c) Paste in the script and press execute: this will create all the Umbraco tables. Now you just need to update your umbraco config file to point to the new database.

Umbraco 7 Redirect to Another Node - Redirect to Parent Node

Umbraco 7 Redirect to Another Node - Redirect to Parent Node Response.Redirect(@CurrentPage.Parent().Url);

Umbraco 7 Tag and Sorting Tags in to Alphabetical Order Using a Dictionary

Umbraco 7 Tag and Sorting Tags in to Alphabetical Order Using a Dictionary: <ul> @{     Dictionary<string, int> tagsList =             new Dictionary<string, int>(); }  @foreach (var c in CurrentPage.Children())     {         if(c.HasValue("tags"))         {                    foreach (var item in c.GetPropertyValue<string[]>("tags"))            {                 if (!tagsList.ContainsKey(item))                 {                 ...

Umbraco 7 Convert Line Breaks in Text Area when Outputing brs

 @Html.Raw(@umbraco.library.ReplaceLineBreaks(@CurrentPage.buildAddress))

Umbraco 7 Year Now Razor Snippet

How to display the current year in Razor and in Umbraco 7  @DateTime.Now.Year For example: <p>Copyright &copy; @DateTime.Now.Year </p> I use this on a lot of my umbraco websites for keeping the copyright up to date.  You can see an example of this on the footer of my most recent Umbraco website design at: http://www.markdevelopment.co.uk/portfolio/abercromby-heating-and-plumbing-services/

Umbraco 7 HTML Site Map Partial View

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ var home = CurrentPage.Site(); } @if (home.Children.Any()) {     @* Get the first page in the children *@     var naviLevel = home.Children.First().Level;     var listChildPages = "false";     @* Add in level for a CSS hook *@     <ul style="margin-top:0px;paddin:0px;font-size:16px;">                 @* For each child page under the home node *@         <li><a href="/" title="@CurrentPage._siteTitle">Home</a></li>         @foreach (var childPage in home.Children.Where("Visible"))         {             if (childPage.Children.Any() && listChildPages =="true"  && childPage.id != 111...