Login Page in umbraco

Step I: The first step is to create a member group and some members that belong to the group.
MemberGroup
Here I have created a group External Users

Step II: Create some sample users and add them to External users group.

Step III: Now that we have some users to work with our next step is to code. Create an mvc project in visual studio and then a model as show belos


public class MemberLoginModel
{
    [Required, Display(Name = "Enter your user name")]
    public string UserName { get; set; }
    [Required, Display(Name = "Enter your password")]
    public string Password { get; set; }
    public bool RememberMe { get; set; }
}

Step IV: Now we create surface controller as shown below.


public class MemberLoginSurfaceController : SurfaceController
{
    [HttpGet]
    [ActionName("MemberLogin")]
    public ActionResult MemberLoginGet()
    {
        return PartialView("MemberLogin", new MemberLoginModel() { UserName = "Enter Username", Password = "Enter Password" });
    }

    [HttpGet]
    public ActionResult MemberLogout()
    {
        Session.Clear();
        FormsAuthentication.SignOut();
        return Redirect("/");
    }

    [HttpPost]
    [ActionName("MemberLogin")]
    public ActionResult MemberLoginGet(MemberLoginModel model)
    {
        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                Session["username"] = model.UserName;
                return Redirect("/student");
            }
            else
            {
                return RedirectToCurrentUmbracoPage();
            }
        }
        return RedirectToCurrentUmbracoPage();
    }
}

Step V: Build the project and copy the dll to bin folder in umbraco. Create a partial view as shown below


@model MvcMemberLogin.Models.MemberLoginModel
@if (Session["username"]!=null)
{
    <p>Logged in : @Session["username"]</p>
    <p>@Html.ActionLink("Log out", "MemberLogout", "MemberLoginSurface")</p>
}
else
{
    using (Html.BeginUmbracoForm("MemberLogin", "MemberLoginSurface"))
    {
        @Html.EditorFor(x => Model)
    }
}

Step VI: Use the partial view in any template as follows


@Html.Action("MemberLogin","MemberLoginSurface")
Advertisements

Adding Forms to umbraco using Surface Controller

Step I: Create an empty mvc project in visual studio
Step II: Add the required model to models folder. Here we create a model named StudentModel as shown below


public class StudentModel
{
    public string FullName { get; set; }
}

Step III: Add controller to the controllers project. Here we add the controller named StudentSurfaceController. Note that the name of the controller should be same as the name of the document type and suffixed with SurfaceController.


public class StudentSurfaceController : SurfaceController
{   
    public ActionResult RenderStudentForm()
    {
        return PartialView("StudentForm",new MVCUserControl.Models.StudentModel());
    }
    [HttpPost]
    public ActionResult HandleStudentForm(MVCUserControl.Models.StudentModel student)
    {
        if (!ModelState.IsValid)
        {
            //Not valid - so lets return the user back to the view with the data they entered still prepopulated
            return CurrentUmbracoPage();
        }
        return RedirectToCurrentUmbracoPage();
    }
}

HandleStudentForm handles the post request after form is submitted.
Step IV: Build the project and add the dll to bin folder in umbraco
Step V: Now we need to create a partial view and add it to the template. Here we create a partial view StudentPartial.cshtml and add it to student template.


@using MVCUserControl.Controllers
@using MVCUserControl.Models
@model StudentModel

@using (Html.BeginUmbracoForm("HandleStudentForm"))
{
    @Html.TextBoxFor(model => model.FullName, new { placeholder = "Your Name" })
    <input type="submit" value="Send" />
}

Step VI: Render the partial view


@{
    Html.RenderPartial("StudentPartial", new MVCUserControl.Models.StudentModel() { FullName=Model.Content.GetPropertyValue("fullName")});
}

Adding members to umbraco programatically


if (Member.GetMemberFromEmail(txtMail.Text) == null)
{

    MemberType demoMemberType = new MemberType(1136);
    Member newMember = Member.MakeNew("Nikhil Gaikwad", demoMemberType, new umbraco.BusinessLogic.User(0));

    newMember.Email = txtEmail.Text;
    newMember.Password = "Pass@123";
    newMember.LoginName = txtFirstName.Text;

    newMember.getProperty("address").Value = txtAddress.Text;
    newMember.getProperty("firstName").Value = txtFirstName.Text;
    newMember.getProperty("lastName").Value = txtLastName.Text;
    newMember.getProperty("gender").Value = 7;
    newMember.Save();
}
else
{
    //member exists
}

Fluent API

Step I: Specify the search Provider


var Searcher = ExamineManager.Instance.SearchProviderCollection["WebsiteSearcher"];

Step II: Create an instance of ISearchCriteria to build our search query


var searchCriteria = Searcher.CreateSearchCriteria(BooleanOperation.Or);

Step III: Chain up the query for this we can use AND OR NOT operator

var query = searchCriteria.Field("nodeName","hello").Or().Field("metaTitle","hello").Compile();
//var query = searchCriteria.GroupedOr(new string[] { "nodeName", "metaTitle"}, "hello").Compile();
var searchResults = Searcher.Search(query);
var noResults = searchResults.Count();

Razor syntax for search


@inherits umbraco.MacroEngines.DynamicNodeContext
@using Examine
@using Examine.SearchCriteria
@{
        var searchTerm = "Client";
        if (searchTerm == null)
         {
               <p>Please use the search box</p>
               return;
         }

        var searcher = ExamineManager.Instance.SearchProviderCollection[0];
        var searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.Or);
        var query = searchCriteria.GroupedOr(new string[] { "nodeName", "bodyText" }, searchTerm).Compile();
        var searchResults = searcher.Search(query);
        var noResults = searchResults.Count();

        You searched for @searchTerm, and found @noResults results
        <ul class="search-results">
        @foreach (var result in searchResults)
        {
            <li>
                <a href="@umbraco.library.NiceUrl(result.Id)">@result.Fields["nodeName"]</a>
            </li>
        }
        </ul>
        <a href="@umbraco.library.NiceUrl(result.Id)">@result.Fields["nodeName"]</a>
}

Displaying repetitive content on a single page in Umbraco

Following is the structure of the content folder
-Homepage
    -Articles
        -One
        -Two
        -Three


@inherits Umbraco.Web.Mvc.UmbracoTemplatePage

@* Ensure that the Current Page has children, where the property umbracoNaviHide is not True *@
@if (CurrentPage.Children.Where("Visible").Any())
{
    <div>
        @* For each child page under the root node, where the property umbracoNaviHide is not True *@
        @foreach (var item in Model.Content.Children)
        {
            foreach (var item1 in item.Properties)
            {
				if(item1.PropertyTypeAlias=="summary")
				{
					<h1>@item1.Value</h1>	
				}
				if(item1.PropertyTypeAlias=="details")
				{
					<span>@item1.Value</span>	
				}
            }
        }
    </div>
}