Wednesday 17 August 2016

controllers by awais

Post comments by Awais


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestGp.Models;
using Umbraco.Web.Mvc;

namespace TestGp.Controllers
{
   public class BlogPostController : SurfaceController
   {
       // GET: BlogPost
       public ActionResult Index()
       {
           return View();
       }

       [HttpPost]
       public JsonResult PostComment(BlogComment blogComment)
       {
           try
           {
               if (blogComment.BlogId > 0 && !String.IsNullOrEmpty(blogComment.Email) && !String.IsNullOrEmpty(blogComment.Message))
               {
                   var service = Services.ContentService;
                   var blogPost = Umbraco.TypedContent(blogComment.BlogId);
                   int count = blogPost.Children.Count();
                   var comment = service.CreateContent("Comment " + (count + 1), blogComment.BlogId, "blogComment");
                   comment.SetValue("email", blogComment.Email);
                   comment.SetValue("message", blogComment.Message);
                   service.SaveAndPublishWithStatus(comment);
               }
               return Json(new { success = true }, JsonRequestBehavior.AllowGet);
           }
           catch (Exception exp)
           {
               return Json(new { success = false, message = exp.Message }, JsonRequestBehavior.AllowGet);
           }
       }
   }
}




 =====================================

Create
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);
           }



Get

using Umbraco.Core;
using Umbraco.Core.Services;
using System.Web.Mvc;
using System.Linq;
using JuneExam.Models;
using System.Collections.Generic;

namespace JuneExam.Controllers
{
   public class OrganisationController : Umbraco.Web.WebApi.UmbracoApiController
   {
       // GET: Organisation
       [HttpGet]
       public List<Email> GetOrganizationTemplates(string orgId)
       {
           int Id;
           int.TryParse(orgId, out Id);

           if (Id > 0)
           {
               var service = Services.ContentService;
               var home = Umbraco.TypedContentAtRoot().FirstOrDefault();

               if (home != null)
               {
                   var organizationList = home.Children.FirstOrDefault(c => c.DocumentTypeAlias == "organizationList");
                   if (organizationList != null)
                   {
                       var organization = organizationList.Children
                           .Where(e => e.DocumentTypeAlias == "organization" && e.GetProperty("orgId").Value.ToString() == Id.ToString())
                           .FirstOrDefault();
                       if (organization != null)
                       {
                           var emailTemplates = organization.Children.FirstOrDefault(l => l.DocumentTypeAlias == "emailTemplates");
                           if (emailTemplates != null)
                           {
                               var emails = new List<Email>();

                               foreach (var e in emailTemplates.Children)
                               {
                                   var emaiTempalte = new Email
                                   {
                                       Subject = e.GetProperty("subject").Value.ToString(),
                                       Body = e.GetProperty("body").Value.ToString()
                                   };
                                   emails.Add(emaiTempalte);
                               }
                               return emails;
                           }
                       }
                   }
               }
           }
           return null;           
       }
   }
}




============================
ajax request

<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>
__________________________________
<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>
 

No comments:

Post a Comment