Wednesday 17 August 2016

Organization view to send request for creating content dynam

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
    var homeNode = Umbraco.TypedContentAtRoot().FirstOrDefault();
    var organizationList = homeNode != null
        ? homeNode.Descendants().Where(o => o.DocumentTypeAlias == "organizationList").FirstOrDefault()
        : null;
}
@using JuneExam.Controllers;
<div>
    <h2>Create Organization</h2>
    <p>Here you can create Organization With Email Templates, Thanks!</p>

    <section id="recent-works">
        <div class="container">
            @using (Html.BeginUmbracoForm<OrganizationController>("CreateOrganization"))
            {
                <div>
                    <label>Org Id *</label>
                    <input name="OrgId" id="OrgId" type="text">
                </div>
                <div>
                    <label>Organization Name *</label>
                    <input name="Name" id="Name" type="text">
                </div>
                <div>
                    <button type="submit" name="Create" class="btn btn-primary btn-lg" required="required">Create</button>
                </div>
            }
        </div>
    </section>


    <section id="recent-works" style="margin-top:20px">
        <h2>These ore Organization List</h2>
        @{
            if (organizationList != null && organizationList.Children.Count() > 0)
            {
                foreach (var org in organizationList.Children)
                {
                    var Id = org.GetProperty("orgId").Value.ToString();
                    <a href="http://umb.june-exam.com/umbraco/api/Organisation/GetOrganizationTemplates?orgId=@Id" target="_blank"><i class="fa fa-eye"></i> @org.Name</a>
                    <br>
                }
            }
        }
    </section>
</div>
=======================

Controller


------------------------------
using JuneExam.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JuneExam.Controllers
{
    public class OrganizationController : Umbraco.Web.Mvc.SurfaceController
    {
        // GET: Organization
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult CreateOrganization(Organization newOrganization)
        {
            var service = Services.ContentService;
            var homeNode = Umbraco.TypedContentAtRoot().FirstOrDefault();

            if(newOrganization.OrgId > 0 && !string.IsNullOrEmpty(newOrganization.Name))
            {
                // Get Organization List
                int orgListId;
                var orglist = homeNode.Children.FirstOrDefault(l=>l.DocumentTypeAlias == "organizationList");
                if (orglist == null)
                {
                    var orgListContent = service.CreateContent("Organization List", homeNode.Id, "organizationList");
                    service.SaveAndPublishWithStatus(orgListContent);
                    orgListId = orgListContent.Id;
                }
                else
                    orgListId = orglist.Id;

                // Create Organization
                var newOrgContent = service.CreateContent(newOrganization.Name, orgListId, "organization");
                newOrgContent.SetValue("orgId", newOrganization.OrgId);
                newOrgContent.SetValue("orgName", newOrganization.Name);
                service.SaveAndPublishWithStatus(newOrgContent);

                // Create Email Templates
                var emailListContent = service.CreateContent("Email templates", newOrgContent.Id, "emailTemplates");
                service.SaveAndPublishWithStatus(emailListContent);

                // Create Email Template in Email Templates
                var newEmailContent1 = service.CreateContent("Welcome Email", emailListContent.Id, "email");
                newEmailContent1.SetValue("templateNo", "1");
                newEmailContent1.SetValue("subject", "Welcome Email");
                newEmailContent1.SetValue("body", "Welcome Email Body");
                service.SaveAndPublishWithStatus(newEmailContent1);

                var newEmailContent2 = service.CreateContent("Forget Email", emailListContent.Id, "email");
                newEmailContent2.SetValue("templateNo", "2");
                newEmailContent2.SetValue("subject", "Forget Email");
                newEmailContent2.SetValue("body", "Forget Email Body");
                service.SaveAndPublishWithStatus(newEmailContent2);
            }
            return CurrentUmbracoPage();
        }
    }
}

No comments:

Post a Comment