Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Using ZBU / Umbraco models builder (ZpqrtBnk) from scratch - Umbraco 7.4+
- 1. Create new Web Application project in Visual Studio
- ASP.Net Web Application | Empty template
- 2. Install Umbraco
- NuGet: Install-Package UmbracoCms
- Build solution
- 3. Create site in local IIS
- i.e. http://localhost:84
- 4. Run Umbraco installer
- Go to http://localhost:84 and follow installation steps | choose 'customize' and pick local SQL Server database
- Create document types, templates etc. needed for site
- 5. Install Umbraco ModelsBuilder API
- NuGet: Install-Package Umbraco.ModelsBuilder.Api
- 6. Install 'Umbraco ModelsBuilder Custom Tool' Visual Studio plugin
- Download and run https://visualstudiogallery.msdn.microsoft.com/ef0896ab-e2eb-47fc-8fcb-79dad0f66e30
- Restart Visual Studio
- 7. Configure ModelsBuilder in Visual Studio
- Tools > Options > Umbraco > ModelsBuilder Options
- Umbraco Password: the password entered when setting up Umbraco in step 2.
- Umbraco Url: http://localhost:84 (don't add /umbraco)
- Umbraco User: the username entered when setting up Umbraco in step 2.
- 8. Create a model class
- Add new file 'UmbracoModels.cs' to /Models in Visual Studio project
- Rightclick UmbracoModels.cs > Properties > Custom Tool: UmbracoModelsBuilder
- Rightclick UmbracoModels.cs > Run Custom Tool
- 9. Build code to use the generated models
- namespace Project.CMS.Controllers
- {
- // API calls: /umbraco/api/Employee/GetAllEmployees
- public class EmployeeController : UmbracoApiController
- {
- public HttpResponseMessage GetAllEmployees()
- {
- UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
- // Grab all 'employees' -- they're located as 'person' document types under the root
- var childNodes = helper.TypedContentAtRoot().First().Children;
- var employees = childNodes.Where(childNode => childNode.DocumentTypeAlias == "person");
- HttpContext.Current.Response.ContentType = "application/json";
- var settings = new JsonSerializerSettings();
- settings.DefaultValueHandling = DefaultValueHandling.Include;
- if (employees.Any())
- {
- foreach (var employee in employees)
- {
- // Use the 'person' generated model:
- var stronglyTypedEmployee = new Person(employee);
- // Use a DTO to create JSON serializable items
- var emp = new Employee();
- emp.Department = stronglyTypedEmployee.Department ?? "";
- //Biography = stronglyTypedEmployee.Biography;
- if(stronglyTypedEmployee.Image != null) emp.Image = stronglyTypedEmployee.Image.GetCropUrl("Vierkant") ?? "";
- emp.Jobname = stronglyTypedEmployee.JobTitle ?? "";
- emp.Name = stronglyTypedEmployee.Name ?? "";
- emp.Residence = stronglyTypedEmployee.CityOfResidence ?? "";
- emp.Startdate = stronglyTypedEmployee.StartDate;
- emp.Video = stronglyTypedEmployee.Video ?? "";
- persons.Add(emp);
- }
- }
- HttpContext.Current.Response.Write(JsonConvert.SerializeObject(persons));
- return new HttpResponseMessage();
- }
- }
- }
Add Comment
Please, Sign In to add comment