Sample Umbraco Razor Macros

 

How To See What Umbraco Node Field Properties Are Available

How to see what field are available at @Model

Get A Node Via Razor

At lot of blood sweat and tears went into this Macro, may it help someone one day.

This gets details of a blog post or news item.

Get Media In C#

Getting media in C#

 

  
public class MediaFunctions
  {
    public static string GetURL(string mediaId)
    {
      string url = "";

      if (!string.IsNullOrEmpty(mediaId))
      {
        int Id = Convert.ToInt32(mediaId);
        var media = new umbraco.cms.businesslogic.media.Media(Id);
        if (media != null)
        {
          var file = media.getProperty("umbracoFile");
          url = (string)file.Value;
        }
      }

      return url;
    }
  }
  

Getting a list of News items depending on which page of News we are viewing.

Getting media out of News

 

 @{
   var start_record = 0;
   var theBlogImage = "";
 }

 @if(!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["starting_record"]))
 {
   start_record = Convert.ToInt32(HttpContext.Current.Request.QueryString["starting_record"]);
 }
 
 @foreach(var blogpost in @Model.Children.Where("NodeTypeAlias ==  \"BlogPost\"").OrderBy("dateCreated  descending").Skip(start_record).Take(10))
 {
     @if(@blogpost.blogImage.ToString().Length > 0)
     {
      theBlogImage = @Model.MediaById(System.Convert.ToInt32(@blogpost.blogImage.ToString())).umbracoFile;
     }
   
     @Blog.FormatBlogPost(@blogpost.postBody.ToString(), @Convert.ToInt32(@Parameter.MaxWords), @blogpost.Id)
  }
 

Connect to the database via an Umbraco Connection String

  
ActivaDataContext dataContext = new ActivaDataContext(umbraco.GlobalSettings.DbDSN);

Inline Macro To Get Form Fields and Submit To C#

  
@{
    var id = Request.QueryString["id"].ToString();
    var id_int = Convert.ToInt32(id);
    
    var name = Request.Form["ctl00$ctl00$ContentPlaceHolderDefault$txtName"];
    var email = Request.Form["ctl00$ctl00$ContentPlaceHolderDefault$txtEmail"];
    var comment = Request.Form["ctl00$ctl00$ContentPlaceHolderDefault$txtComment"];
    
    if(name != null)
    {
      @ActiviaExtensions.Comments.SaveComment(@id_int, @name, @email, @comment);
    }
  
    @ActiviaExtensions.Comments.DisplayComments(@id_int);
  
  }

Access FAQs That Are A Child Of Content

This is done in a inline Macro:

 

 
@foreach(var faq in @Model.Children.Where("NodeTypeAlias == \"FrequentlyAskedQuestion\""))
 {
    @faq.question
  @faq.answer

 }

By the way, to get to the next content type

 
var next = @Model.Next();
var previous = @Model.Previous();

@umbraco.library.NiceUrl(@next.Id)

Save A Comment In C# dll

 

public static HtmlString SaveComment(int blogId, string name, string email, string comment)
    {
      ZoesBlogComment zcomment = new ZoesBlogComment();

      zcomment.BlogPostId = blogId;
      zcomment.Name = Microsoft.Security.Application.AntiXss.HtmlEncode(name);
      zcomment.Email = Microsoft.Security.Application.AntiXss.HtmlEncode(email);
      zcomment.Comment = Microsoft.Security.Application.AntiXss.HtmlEncode(comment);
      zcomment.Approved = true;
      zcomment.DateSubmitted = DateTime.Now;

      ActivaDataContext dataContext = new ActivaDataContext(umbraco.GlobalSettings.DbDSN);
      dataContext.ZoesBlogComments.InsertOnSubmit(zcomment);
      dataContext.SubmitChanges();
      //get the email address to send the comment to
      umbraco.NodeFactory.Node miscellaneous = new umbraco.NodeFactory.Node(1161);
      var emailAddress = miscellaneous.GetProperty("emailToSendBlogComments");

      var serverEmail = miscellaneous.GetProperty("serverEmail");
//or umbraco.UmbracoSettings.NotificationEmailSender to get the from email address in umbracoSettings.config
      string blogURL = umbraco.library.NiceUrl(blogId);
      blogURL = "http://www.activia.com.au" + blogURL;

      string line = "The following comment has been made to the post at " + blogURL + "";
      line += "
Name: " + name;
      line += "
Email: " + email;
      line += "
Comment: " + comment;
      umbraco.library.SendMail(serverEmail.ToString(), emailAddress.ToString(), "New comment for Zoe's Blog at www.activia.com.au" + blogURL.ToString(), line, true);
      return new HtmlString("");
    }
Share