Razor Engine in SharePoint 2013

In this blog I will show you how to leverage the Razor Engine in SharePoint 2013. I will be using Razor Engine to send emails from SharePoint 2013. Since SharePoint API’s have been upgraded to .NET framework 4.0, we can leverage some of the great frameworks like Razor Engine in SharePoint 2013.

Email functionality is the most common functionality that we develop in every SharePoint project. Before this we used to create XLST and HTML as an email template. Now we will use Razor View as template. If you have already worked with Razor view then you know how easy and neat the Razor Views are.

Let’s get started.

1)      Create a new SharePoint 15 Visual Web Part project in Visual Studio 2011. Let’s call it SharePopint13RazorWP.

2)      Create a class EmailModel.cs. Define properties that need for your email template as shown in the below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharePoint13RazorWP
{
public class EmailModel
{
string _firstName;
string _lastName;
string _emailAddress;
string _subject;

public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}

public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}

public string EmailAddress
{
get { return _emailAddress; }
set { _emailAddress = value; }
}

public string Subject
{
get { return _subject; }
set { _subject = value; }
}

public string TemplatePath
{
get { return _subject; }
set { _subject = value; }
}
}
}

3)      Download RazorEngine from codeplex(http://razorengine.codeplex.com) and add reference to project for RazorEngine.dll and System.Web.Razor.dll

4)   We will now create a Razor View .cshtml, Save it as WelcomeCustomer.cshtml and upload it to document library.  The place holders in the Razor View will be something like “@Model.LastName”. These are same properties as defined in the EmailModel.cs earlier.

Hello @Model.LastName @Model.FirstName

Welcome to our website.

Thank you for registering.

Thanks,
Administrator

NOTE:  You can also put Razor View content in the List column of type “Multiple Lines of text”

5)      In the webpart class file add event handler for button click and add the code as shown below.

protected void btnSendEmail_Click(object sender, EventArgs e)
{
EmailModel model = new EmailModel()
{
EmailAddress = “naveen.gopisetty@neudesic.com”,
FirstName = “naveen”,
LastName = “gopisetty”,
Subject = “with razor view”,
};
string siteURL = SPContext.Current.Site.Url;
RazorEmail email = new RazorEmail();
email.FromEmail = “naveen.gopisetty@gmail.com”;
email.ToEmail = “naveen.gopisetty@neudesic.com”;
email.Subject = “Testing Razor with SharePoint”;

//Calling Document Library. Pass Document Library Name and TemplateName in document library.
email.SendEmail(model, siteURL, RazorEmail.SourceType.DocumentLibrary, “WelcomeCustomer”, “ETemplates”);

//Calling LIST. Pass List Name,List Column Name,List Item Index
email.SendEmail(model, siteURL, RazorEmail.SourceType.List, null, null, “CL”, “EmailTemplate”, 0);
}

6) Now deploy the solution to SharePoint.

7) Configure the Email settings for SharePoint. Goto SharePoint Central Admin ->System Settings -> Confiure outogoing e-mail settings -> in the Outbound SMTP Server mention “smtp.gmail.com”. I am using gmail smtp settings.

Add gmail SMTP setting in the web.config as well, as it requires authentication.

<system.net>
<mailSettings>
<smtp deliveryMethod=”Network” from=”naveen.gopisetty@gmail.com”>
<network defaultCredentials=”false” enableSsl=”true” host=”smtp.gmail.com” userName=”naveen.gopisetty@gmail.com” password=”xxxxx!” port=”587″ />
</smtp>
</mailSettings>
<defaultProxy />
</system.net>

8) Add the webpart to the Home page and click on “Send Email” button and see the magic.

This is just an example as to show the use of Razor View in SharePoint 2013. I will improving the code and adding more features like adding attachments etc..

Source is hosted in the bitbucket : https://bitbucket.org/naveengopisetty/razor-engine-in-sharepoint-2013/src

About Naveen

I am Naveen Gopisetty.I work on different technologies within the Microsoft technology stack including SharePoint, Azure, MVC and more. I also love open source technologies as well. I tend to share my knowledge which I've learnt during my development with these posts. Hope it will useful to some of you. View all posts by Naveen

2 responses to “Razor Engine in SharePoint 2013

  • Shafaqat Ali

    Hi

    If document library has multiple email templates files? You have not used file name anywhere.

    • Naveen

      @shafaqatali

      1) If document library has multiple email templates files?
      My Response:- You need to call it SendEmail method for each template file assuming that you will have one template per business case.

      2) You have not used file name anywhere.
      Response: WelcomeCustomer is the file name. While writing this sample code I added another file name. Now is replaced with correct file name. Check out the below code. Updated the blog also.

      email.SendEmail(model, siteURL, RazorEmail.SourceType.DocumentLibrary, “WelcomeCustomer”, “ETemplates”);

      NOTE: This an just sample code on the thought how to utilize the Razor Engine in SharePoint. Not yet a full blown library.

      Code is hosted on bitbucket. You can play with it.

      Thanks!

Leave a comment