Handle dynamic data in umbraco
- public class OrganizationController : UmbracoApiController
{
// GET: Surface
//public ActionResult Index()
//{
// return View();
//}List<OrganizationalModel>
[HttpGet]
public BaseModel Get(OrganizationalModel model)
{
List<OrganizationalModel> orgList = new List<OrganizationalModel>();
var rootNode = Umbraco.TypedContentAtRoot().FirstOrDefault();
var organizationList = rootNode.Children.Where(i => i.DocumentTypeAlias == "organization");
foreach (var child in organizationList)
{
var orgObj = new OrganizationalModel { organizationName = child.Name };
orgList.Add(orgObj);
}
return new BaseModel { Data = orgList };
}
[HttpPost]
[ActionName("CreateOrganisation")]
public JsonResult CreateOrganisation(OrganizationalModel model)
{
var homeNode = Umbraco.TypedContentAtRoot().FirstOrDefault();
//this is use for create dynamic content
var ContentService = Services.ContentService;
if (homeNode != null)
{
//first to create organization using Content Service
var organization = ContentService.CreateContent(model.organizationName, homeNode.Id, "organization");
//now set all values
organization.SetValue("organizationName", model.organizationName);
//now save and publish parent Content
ContentService.SaveAndPublishWithStatus(organization);
var EmailList = ContentService.CreateContent("EmailList", organization.Id, "emailList");
ContentService.SaveAndPublishWithStatus(EmailList);
var welcome = ContentService.CreateContent("Welcome", EmailList.Id, "email");
welcome.SetValue("subject", "This is testing Subject");
welcome.SetValue("body", "This is testing body");
ContentService.SaveAndPublishWithStatus(welcome);
var Thanks = ContentService.CreateContent("Thanks", EmailList.Id, "email");
Thanks.SetValue("subject", "This is testing Subject");
Thanks.SetValue("body", "This is testing body");
ContentService.SaveAndPublishWithStatus(Thanks);
}
else
{
return new JsonResult();
}
return new JsonResult();
}
[HttpGet]
[ActionName("GetEmailTemplatesById")]
public List<Email> GetEmailTemplatesById(string organizationId)
{
List<OrganizationalModel> orgList = new List<OrganizationalModel>();
var rootNode = Umbraco.TypedContentAtRoot().FirstOrDefault();
var organization= Umbraco.TypedContent(organizationId);
//var organization = rootNode.Children.FirstOrDefault(i => i.DocumentTypeAlias == "organization" && i.Id == Convert.ToInt32(organizationId));
var EmailList = organization.Children().FirstOrDefault();
var EmailsTemplates = EmailList.Children();
var returnEmails = new List<Email>();
foreach (var template in EmailsTemplates)
{
var email = new Email
{
Title = template.GetPropertyValue("subject").ToString(),
Description = template.GetProperty("body").Value.ToString()
};
returnEmails.Add(email);
}
return returnEmails;
}
}
}
No comments:
Post a Comment