PieterDub

Use ModelsBuilder in Umbraco

Feb 22nd, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 KB | None | 0 0
  1. Using ZBU / Umbraco models builder (ZpqrtBnk) from scratch - Umbraco 7.4+
  2.  
  3. 1. Create new Web Application project in Visual Studio
  4.     ASP.Net Web Application | Empty template
  5. 2. Install Umbraco
  6.     NuGet: Install-Package UmbracoCms
  7.     Build solution
  8. 3. Create site in local IIS
  9.     i.e. http://localhost:84
  10. 4. Run Umbraco installer
  11.     Go to http://localhost:84 and follow installation steps | choose 'customize' and pick local SQL Server database
  12.     Create document types, templates etc. needed for site
  13. 5. Install Umbraco ModelsBuilder API
  14.     NuGet: Install-Package Umbraco.ModelsBuilder.Api
  15. 6. Install 'Umbraco ModelsBuilder Custom Tool' Visual Studio plugin
  16.     Download and run https://visualstudiogallery.msdn.microsoft.com/ef0896ab-e2eb-47fc-8fcb-79dad0f66e30
  17.     Restart Visual Studio
  18. 7. Configure ModelsBuilder in Visual Studio
  19.     Tools > Options > Umbraco > ModelsBuilder Options
  20.         Umbraco Password: the password entered when setting up Umbraco in step 2.
  21.         Umbraco Url: http://localhost:84 (don't add /umbraco)
  22.         Umbraco User: the username entered when setting up Umbraco in step 2.
  23. 8. Create a model class
  24.     Add new file 'UmbracoModels.cs' to /Models in Visual Studio project
  25.     Rightclick UmbracoModels.cs > Properties > Custom Tool: UmbracoModelsBuilder
  26.     Rightclick UmbracoModels.cs > Run Custom Tool
  27. 9.  Build code to use the generated models
  28.  
  29. namespace Project.CMS.Controllers
  30. {
  31.     // API calls: /umbraco/api/Employee/GetAllEmployees
  32.     public class EmployeeController : UmbracoApiController
  33.     {
  34.         public HttpResponseMessage GetAllEmployees()
  35.         {
  36.             UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
  37.             // Grab all 'employees' -- they're located as 'person' document types under the root
  38.             var childNodes = helper.TypedContentAtRoot().First().Children;
  39.             var employees = childNodes.Where(childNode => childNode.DocumentTypeAlias == "person");
  40.  
  41.             HttpContext.Current.Response.ContentType = "application/json";
  42.             var settings = new JsonSerializerSettings();
  43.             settings.DefaultValueHandling = DefaultValueHandling.Include;
  44.  
  45.             if (employees.Any())
  46.             {
  47.                 foreach (var employee in employees)
  48.                 {
  49.                     // Use the 'person' generated model:
  50.                     var stronglyTypedEmployee = new Person(employee);
  51.  
  52.                     // Use a DTO to create JSON serializable items
  53.                     var emp = new Employee();
  54.  
  55.                     emp.Department = stronglyTypedEmployee.Department ?? "";
  56.                     //Biography = stronglyTypedEmployee.Biography;
  57.                     if(stronglyTypedEmployee.Image != null) emp.Image = stronglyTypedEmployee.Image.GetCropUrl("Vierkant") ?? "";
  58.                     emp.Jobname = stronglyTypedEmployee.JobTitle ?? "";
  59.                     emp.Name = stronglyTypedEmployee.Name ?? "";
  60.                     emp.Residence = stronglyTypedEmployee.CityOfResidence ?? "";
  61.                     emp.Startdate = stronglyTypedEmployee.StartDate;
  62.                     emp.Video = stronglyTypedEmployee.Video ?? "";
  63.                     persons.Add(emp);
  64.                 }
  65.             }
  66.             HttpContext.Current.Response.Write(JsonConvert.SerializeObject(persons));
  67.  
  68.             return new HttpResponseMessage();
  69.         }
  70.     }
  71. }
Add Comment
Please, Sign In to add comment