Showing posts with label Orchard. Show all posts
Showing posts with label Orchard. Show all posts

Thursday, April 18, 2019

How to send email in Orchard CMS


Introduction


In this article, you will learn how to send email in Orchard CMS using it's out of box services.

Steps to send email in Orchard CMS

  • Create Email wrapper template
  • Create Email Template
  • Inject required orchard services in the Controller or Custom Service
  • Specify email template to use and pass data
  • Sending email using MessageService
To demonstrate the scenario, we take the example of sending a challenge email to verify the user email address. In Orchard. Users module, you will find all this code to send an email using orchard services.

Steps 1 - Create Email wrapper template


It applies default Body alteration for SmtpChannel which create the body of HTML template. create a new file "Template.User.Wrapper.cshtml" under Views folder.
@* Override this template to alter the email messages sent by the Orchard.Users module *@
@Display.PlaceChildContent(Source: Model)

Steps 2 - Create Email Template


Now create the content of the email. It applies default Body alteration for SmtpChannel which create the body of HTML template. create a new file "Template.User.Validated.cshtml" under Views folder. It is the same razor view page where we can pass the data and place on the place holders. e.g. Model.ContactEmail is the address of the contact person of the website.
@T("Thank you for registering with {0}.<br /><br /><br /><b>Final Step</b><br />To verify that you own this e-mail address, please click the following link:<br /><a href=\"{1}\">{1}</a><br /><br /><b>Troubleshooting:</b><br />If clicking on the link above does not work, try the following:<br /><br />Select and copy the entire link.<br />Open a browser window and paste the link in the address bar.<br />Click <b>Go</b> or, on your keyboard, press <b>Enter</b> or <b>Return</b>.", Model.RegisteredWebsite, Model.ChallengeUrl)
@if (!String.IsNullOrWhiteSpace(Model.ContactEmail)) {
    @T("<br /><br />If you continue to have access problems or want to report other issues, please <a href=\"mailto:{0}\">Contact Us</a>.",Model.ContactEmail)
}

Steps 3 - Inject required orchard services in the Controller or Custom Service


To render the email template, we need to use a few orchard services. In Orchard CMS every view object is a shape so we need to create a shape using our created template views. We need to inject IShapeService and IShapeFactory to create the shape for the email content. IMessageService required to send the email through the SMTP channel.
namespace Orchard.Users.Services {
    public class UserService : IUserService {
        private readonly IMessageService _messageService;
        private readonly IShapeFactory _shapeFactory;
        private readonly IShapeDisplay _shapeDisplay;
        public UserService(
        IMessageService messageService, 
        IShapeFactory shapeFactory,
        IShapeDisplay shapeDisplay) {

            _messageService = messageService;
            _shapeFactory = shapeFactory;
            _shapeDisplay = shapeDisplay;
        }
        ...

Steps 4 - Specify email template to use and pass data


At this step, we specify the email and wrapper template to create the Shape for HTML content. In below line of code ShapeFactory.Create method takes the first parameter for the email template then we add email wrapper template in the template metadata. Along with this, we pass the model data using the anonymous object.
var template = _shapeFactory.Create("Template_User_Validated", Arguments.From(new {
    RegisteredWebsite = site.As<RegistrationSettingsPart>().ValidateEmailRegisteredWebsite,
    ContactEmail = site.As<RegistrationSettingsPart>().ValidateEmailContactEMail,
    ChallengeUrl = url
}));
template.Metadata.Wrappers.Add("Template_User_Wrapper");

Steps 5 - Sending email using MessageService


Now specify the mail message Subject, Body and Recipients of the email message. You can also specify attachments also. Just add the "Attachments" key and List as value to the parameters dictionary. Attachment should be the file path to attach with the email message.
var parameters = new Dictionary<string, object> {
                    {"Subject", T("Verification E-Mail").Text},
                    {"Body", _shapeDisplay.Display(template)},
                    {"Recipients", user.Email}
                };

        _messageService.Send("Email", parameters);
Below is the complete code for method which sends the challenge email:
public void SendChallengeEmail(IUser user, Func<string, string> createUrl) {
   string nonce = CreateNonce(user, DelayToValidate);
   string url = createUrl(nonce);

   if (user != null) {
       var site = _siteService.GetSiteSettings();

       var template = _shapeFactory.Create("Template_User_Validated", Arguments.From(new {
           RegisteredWebsite = site.As<RegistrationSettingsPart>().ValidateEmailRegisteredWebsite,
           ContactEmail = site.As<RegistrationSettingsPart>().ValidateEmailContactEMail,
           ChallengeUrl = url
       }));
       template.Metadata.Wrappers.Add("Template_User_Wrapper");
       
       var parameters = new Dictionary<string, object> {
                   {"Subject", T("Verification E-Mail").Text},
                   {"Body", _shapeDisplay.Display(template)},
                   {"Recipients", user.Email}
               };

       _messageService.Send("Email", parameters);
   }
}

Conclusion

I guess that's all to send an email in Orchard CMS.

Thursday, December 22, 2016

How are the use of layers in Orchard CMS?

What is Layer?

A layer is a group of widgets (with their specific configuration, which includes their positioning -zone name and ordering-) that is activated by a specific rule.

For example, the TheHomePage layer is activated by a rule that specifically selects the home page. The Default layer is always active no matter what page is displayed.

The Authenticated layer is only active when users have identified themselves. When more than one layer is active on any given page, all the widgets from all those layers get displayed at the same time. Orchard orders them based on their position string.

What is the use of Layer?

Layers change the layout of your new page without affecting the rest of the site you can create a new layer for a specific pages by specifying layer rule. These help to render some content on a specific location of the page except other pages does not change.

See the below example:

Adding a New Page to Your Site

  1. In the Orchard Dashboard, under New, select Page.
  2. Enter a title for the page. When you enter a title for the page and save it (for example, "Download"), the permalink (URL) for the page will be filled in automatically ("download"). You can edit this link if you prefer a different URL.
  3. Enter some text for the content page body.

    clip_image001
  4. In the Tags field, add comma-separated tags such as "download" and "Orchard" so that you can search and filter using those tags later.
  5. Check Show on main menu and enter the menu text ("Downloads") to use in the site's main menu.
  6. Select Publish Now to make the updates to the page visible immediately. You can also save the page as a draft (to edit later before publishing), or you can choose to publish the page at a specific date and time.

    clip_image002
  7. Select Your Site in the upper-left side of the Dashboard to view the modified home page with the new menu. Clik Downloads and you will see your new page.

Adding New Layer for a Page

To change the layout of your new page without affecting the rest of the site you can create a new layer, that will be applied only to the Downloads page. Then you can place some widgets on that layer and they will be visible only in the Downloads page.

  1. Go to the Dashboard and select Widgets. Then click add a new layer to add a new layer for this page which will allow you to customize the layout for the new page at a later point in time.

    clip_image003
  2. Write a name for the layer, a description, and a layer rule: url"~/download". This will instruct the Orchard System to show the widgets in this layer only when the url of the browser is pointing to "download". Select Save.

    clip_image004

Adding a New HTML Widget

  1. To check that your layer rule is working you can add a widget to it. Ensure that Current Layer is Download. Click Add in AsideFirst.

    clip_image005
  2. Add a new Html Widget.
    clip_image006
  3. Write a title and a body for it. Save it.

    clip_image007
  4. Select Your Site in the upper-left side of the Dashboard. Navigate to Downloads. You should see the custom layout.
    clip_image008

Now you will see that ‘Downloads’ page has different layout rather than other pages because it uses different layer except other pages use different.

Tuesday, July 12, 2016

Configuring Orchard CMS blog in Open Live Writer

After using Windows Live Writer, now I have start using Open Live Writer to do remote blogging. Right now WLW is not able to connect with few blogging platforms like blogger.com and also Microsoft has stopped putting efforts on the amazing tool for the bloggers.
OLW is an open source tool which is developed by the contribution of the developer community. Lots of features of this tool is under development. It has the main advantage that there are many tools available as compare to the HTML editor on the remote blog. You can work offline, which might be good if you’re in a situation where you don’t have a decent internet connection or not able to connect to internet for days. It will let you write blog posts and save them to draft for later publishing.
In this article, you will go through the steps to configure Open Live Writer for the Orchard CMS. This provide support for remote bloging through XmlRcp. Both WLW and OLW are able to connect with the Orchard CMS blog using this feature.
Orchard provides this XML-RPC feature out of the box and You have to enable it in the Modules section inside your Orchard CMS Orchard admin panel.
Prerequisites:
  1. You have setup Orchard CMS on your web hosting or local IIS and created a blog on website.
  2. Installed OLW on your computer.
If you have done pre work then go to the Administration Dashboard of Orchard and click the Modules. There you will find Remote Blog Publishing feature under the Content Publishing section. It is dependent on XmlRpc so if you enable it then XmlRpc feature will automatically enabled.
clip_image002
To enable Remote Blog Publishing click the Enable link. After clicking the enable link you will see both of them enabled at once since they are dependent features. After you click Enable, if everything is Ok then you will see following message:
clip_image004
After enabling these modules you are ready to configure Orchard blog in Open Live Writer. Now, launch Live Writer from your Start menu in Windows.
In Blog Accounts section, click on 'Add blog account'
clip_image006


When you choose “Add blog account” then you will see below prompt to select the blog service. Choose “Other blog service” from the available options and click Next.
clip_image007
Type the URL to your Orchard blog, along with the admin user name and password that you defined when you set-up Orchard for the first time.
clip_image008
Click Next. Open Live Writer will connect to your blog in order to read the XML-RPC capabilities that Orchard supports and download the current Theme (for previewing posts before publishing). If you are prompted to create a temporary post during this step, select "Yes" in the dialog.
clip_image009
After that it will complete downloading supporting files and template for making blog post writing intuitive.
clip_image010
Once it finishes, you will have window where you can specify the name of your blog you have just configured in Open Live Writer, click Finish.
clip_image011
Now you are ready to write blog post for your Orchard CMS blog.






















Friday, September 11, 2015

Orchard CMS Learning stuff.

Recently I try to learn about the CMS which are developed in Microsoft technologies as there are lots of mature CMS available like joomla, Druple etc. These are based on the PHP and mostly hosted on apache(Linux based web server). Though I am working on .net stack so I decided to start with some open source CMS developed in ASP.net technology and I decided to start learning with Orchard.

What is Orchard:
Orchard is a free, open source, community-focused Content Management System built on the ASP.NET MVC platform.

Orchard is built on a modern architecture that puts extensibility up-front, as its number one concern. All components in Orchard can be replaced or extended. Content is built from easily composable building blocks. Modules extend the system in a very decoupled fashion, where a commenting module for example can as easily apply to pages, blog posts, photos or products. A rich UI composition system completes the picture and ensures that you can get the exact presentation that you need for your content.

Here are few learning resources that I want to share with others also:

Happy learning..