Tim Geyssens, Umbraco developer - Umbraco .net cms Tips, tricks, examples, screencasts and more

Web Name: Tim Geyssens, Umbraco developer - Umbraco .net cms Tips, tricks, examples, screencasts and more

WebSite: http://www.nibble.be

ID:199261

Keywords:

developer,net,cms,

Description:

UI-O-Matic is great for quickly adding a CRUD section to your Umbraco backoffice but what if you also want to add a form to your frontend based on the same poco ? Meet MCFly: One of the most thrilling new features in UI-O-Matic v2 is the ability to plug in your own reposities to handle the crud (thanks to Matt for the idea and implementation). So by default UI-O-Matic works agains the database using PetaPoco but with this feature we can pretty much plug in any third party API.ExampleLet’s say we want to be able to create Github issues from inside the Umbraco UI, well that can be done in a couple of easy steps.Define the Model 1 using System.ComponentModel.DataAnnotations; 2 using UIOMatic.Attributes; 3 using UIOMatic.Example.GitHubIssues.Repo; 4 using Umbraco.Core.Persistence.DatabaseAnnotations; 6 namespace UIOMatic.Example.GitHubIssues.Models 8 [UIOMatic( issues , Issues , Issue , 9 RepositoryType = typeof(GitHubIssuesRepo),10 ShowOnSummaryDashboard = true)]11 public class Issue13 [PrimaryKeyColumn]14 public int Id { get; set; }16 [Required]17 [UIOMaticField(IsNameField = true)]18 [UIOMaticListViewField]19 public string Title { get; set; }21 [UIOMaticField(View = UIOMatic.Constants.FieldEditors.Textarea)]22 public string Body { get; set; }24 public override string ToString()26 return Title;The model consists of a couple of properties of course if we wish that UI-O-Matic picks these up we need to mark them with the correct attributes. One thing that we also need to do is mark 1 property with the primarykeycolumn attribute , this is the property used to identify the object.As you see on the UIOMatic attribute we setup the RepositoryType to be something custom instead of just the default.Create the RepositoryNext we need to create that custom repo type. We can inherit from the Abstract class class AbstractUIOMaticRepository TEntity, TId and override it’s methods, so that’s where we’ll plug in the third party api… 1 using Octokit; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using UIOMatic.Data; 6 using UIOMatic.Models; 8 namespace UIOMatic.Example.GitHubIssues.Repo 10 public class GitHubIssuesRepo : AbstractUIOMaticRepository Models.Issue, int 11 { 12 private const string RepoOwner = TimGeyssens ; 13 private const string RepoName = UIOMatic ; 15 private GitHubClient Client 16 { 17 get 18 { 20 var client = new GitHubClient(new ProductHeaderValue( my-cool-app )); 21 var basicAuth = new Credentials( USERNAME , PASSWORD ); 22 client.Credentials = basicAuth; 23 return client; 24 } 25 } 27 public override Models.Issue Create(Models.Issue entity) 28 { 29 var createIssue = new NewIssue(entity.Title); 30 createIssue.Body = entity.Body; 32 var issue = Client.Issue.Create(RepoOwner, RepoName, createIssue).Result; 34 entity.Id = issue.Number; 36 return entity; 37 } 40 public override void Delete(int[] ids) 41 { 43 foreach (var id in ids) 44 { 45 var issue = Client.Issue.Get(RepoOwner, RepoName, id).Result; 47 var update = issue.ToUpdate(); 49 update.State = ItemState.Closed; 51 var updatedIssue = Client.Issue.Update(RepoOwner, RepoName, issue.Number, update).Result; 52 } 53 } 56 public override Models.Issue Get(int id) 57 { 59 var issue = Client.Issue.Get(RepoOwner, RepoName, id).Result; 61 return new Models.Issue 62 { 64 Id = issue.Number, 65 Title = issue.Title, 66 Body = issue.Body 67 }; 68 } 71 public override IEnumerable Models.Issue GetAll(string sortColumn = , string sortOrder = ) 72 { 73 var retVal = new List Models.Issue (); 75 var issues = Client.Issue.GetAllForRepository(RepoOwner, RepoName).Result; 77 foreach(var issue in issues) 78 { 80 retVal.Add(new Models.Issue 81 { 82 Id = issue.Number, 83 Title = issue.Title 84 }); 86 } 87 return retVal; 88 } 90 public override UIOMaticPagedResult Models.Issue GetPaged(int pageNumber, int itemsPerPage, string searchTerm = , IDictionary string, string filters = null, string sortColumn = , string sortOrder = ) 91 { 92 //used in the ListView but we aren t using that one currently 93 throw new NotImplementedException(); 94 } 96 public override long GetTotalRecordCount() 97 { 98 return GetAll().Count(); 99 }101 public override Models.Issue Update(Models.Issue entity)102 {104 var issue = Client.Issue.Get(RepoOwner, RepoName, entity.Id).Result.ToUpdate();106 issue.Title = entity.Title;107 issue.Body = entity.Body;109 var updatedIssue = Client.Issue.Update(RepoOwner, RepoName, entity.Id, issue).Result;111 return entity;112 }115 }116 }So as you see nothing groundbreaking in here, we are just using octokit.net to do the heavy lifting and using its API to do the CRUD operations.ResultWe are now able to perform crud operations against the Github repo from within the Umbraco backoffice.VideoConclusionSo as you can see the sky is the limit with UI-O-Matic v2! In collaboration with Matt Brailsford I’m am happy to announce the v2 release of UI-O-Matic. In addition to the beta we’ve also added the following:List View ActionsList View FiltersConfig settingsDashboardInstallationSimply install on your Umbraco instance with NugetInstall-Package Nibble.Umbraco.UIOMaticExampleIf you have the following db table1 CREATE TABLE [People] (2 [Id] int IDENTITY (1,1) NOT NULL3 , [FirstName] nvarchar(255) NOT NULL4 , [LastName] nvarchar(255) NOT NULL5 , [Picture] nvarchar(255) NOT NULL 1 [UIOMatic( people , People , Person , FolderIcon = icon-users , ItemIcon = icon-user )] 2 [TableName( People )] 3 public class Person 5 [PrimaryKeyColumn(AutoIncrement = true)] 6 public int Id { get; set; } 8 [Required] 9 [UIOMaticField(Name = First name , Description = Enter the persons first name )]10 public string FirstName { get; set; }12 [Required]13 [UIOMaticField(Name = Last name ,Description = Enter the persons last name )]14 public string LastName { get; set; }16 [UIOMaticField(Name = Picture ,Description = Select a picture , View = UIOMatic.Constants.FieldEditors.File)]17 public string Picture { get; set; }19 public override string ToString()21 return FirstName + + LastName; In the contact entries example you’ll see that we use a relative date filter to output the created date in the format of … minutes ago, hours ago,…days agoWith the following config on the standard label list view field1 [UIOMaticListViewField(Config = { format : {{value|relativeDate}} } )]2 [UIOMaticField(View = UIOMatic.Constants.FieldEditors.DateTime)]3 public DateTime Created { get; set; }Show meSo let me extend the contact entries example and truncate the message if it’s to large.I’ve downloaded this truncate filter from github and added that to my project, also added a package manifest and a script where I do the include, so I get the following structure in my AppPlugins folderImport looks like1 var app = angular.module( umbraco );3 //These are my Angular modules that I want to inject/require5 app.requires.push( truncate );So with that in place I can now use the new filter1 [SpecialDbType(SpecialDbTypes.NTEXT)]2 [Required]3 [UIOMaticListViewField(Config = { format : {{value|characters:25}} } )]4 [UIOMaticField(View = UIOMatic.Constants.FieldEditors.Textarea)]5 public string Message { get; set; }And now we get a truncated message (tailed with …)btw RC is out! https://www.nuget.org/packages/Nibble.Umbraco.UIOMatic/2.0.0-rc (last chance to test before final hits the shelves) Something that we’ve just added to UI-O-Matic v2 is the ability to plug in additional actions. So by default you get CRUD but now you will also be able to plug in third party ones!ExampleLet’s say you want to provide export functionality to your clients, here is how do do that (this is currently run on the latest source so still subject to change)Defining the actionStep 1 is that we’ll need to define our action, each action consist of a name, alias and a view it will open1 [UIOMaticAction( export , Export , ~/App_Plugins/UIOMaticAddons/export.html )]2 public class ExportAction { }Next we add the our model (if we take the previous example we can just add the new Action by adding it to the ListActions parameter of our UIOMatic attribute)1 [TableName( ContactEntries )]2 [PrimaryKey( Id , autoIncrement = true)]3 [UIOMatic( contactentries , Contact Entries , Contact Entry ,4 FolderIcon = icon-users ,5 SortColumn = Created , SortOrder = desc ,6 RenderType = UIOMaticRenderType.List,7 ListActions = new[]{ typeof(ExportAction)})]8 public class ContactEntryThe Angular View and ControllerAs you see we pointed the new action to ~/App_Plugins/UIOMaticAddons/export.html so we now have to build that dialog1 div ng-controller= uioMaticAddons.ExportController 2 p ng-show= loading Generating Export /p 3 a href= {{file}} ng-hide= loading target= _blank Download export here /a 4 /div 1 angular.module( umbraco ).controller( uioMaticAddons.ExportController ,2 function ($scope, uioMaticExportResource) {3 $scope.loading = true;4 uioMaticExportResource.getExport($scope.dialogData.typeAlias).then(function (response) {5 $scope.file = response.data;6 $scope.loading = false;7 });8 });As you can see here we just call a resource and provide it with the typeAlias (that is provided to us in the dialog data).The resource will return the file name of the csv export.AngularJS Resource 1 angular.module( umbraco.resources ) 2 .factory( uioMaticExportResource , function ($http, umbRequestHelper) { 3 return { 4 getExport: function (typeAlias) { 5 return umbRequestHelper.resourcePromise( 6 $http.get(Umbraco.Sys.ServerVariables.uioMaticAddons.ecBaseUrl + GetExport?typeAlias= +typeAlias), 7 Failed to generate export 8 );11 });this one basicly calls our .net controller.NET Controller 1 using CsvHelper; 2 using System; 3 using System.IO; 4 using Umbraco.Core.IO; 5 using Umbraco.Web.Editors; 7 namespace TestUIOMatic2Take2.Controllers 9 public class ExportController: UmbracoAuthorizedJsonController11 public object GetExport(string typeAlias)13 var guid = Guid.NewGuid();15 using (var textWriter = File.CreateText(IOHelper.MapPath(@ ~\App_Plugins\UIOMaticAddons\Exports\ + guid + .csv )))17 using (var csv = new CsvWriter(textWriter))19 var os = new UIOMatic.Services.PetaPocoObjectService();21 var data = os.GetAll(UIOMatic.Helper.GetUIOMaticTypeByAlias(typeAlias));23 csv.Configuration.Delimiter = ; ;24 csv.Configuration.HasHeaderRecord = true;26 csv.WriteRecords(data);30 return new { data = ../App_Plugins/UIOMaticAddons/Exports/ + guid.ToString() + .csv };This uses the great and simple CsvHelper package to easily generate a csv file out of our data and then stores it on disk and returns the file location.ResultI now got an additional export action I can apply to any model I wish!FutureOnce v2 final is out, I’ll make a nuget package out of this (and probably make some tweaks since this is just a proof of concept at the moment) so you can easiliy install it and extend your UI-O-Matic models As promised I would share some real world examples of using UI-O-Matic. One thing I tend to use if for is as a quick entries viewer for custom made forms on your Umbraco site.PrerequisitesSo of course we first need to setup a form on our frontend, for that we’ll need a model, view and (surface)controller. If you arent’ familiar with how to set that up check out the chapter on Umbraco.tv http://umbraco.tv/videos/umbraco-v7/developer/fundamentals/surface-controllers/introduction/ or I’ve also seen this video recently https://www.youtube.com/watch?v=3V0A1AYJbys explaining the process.Model 1 [TableName( ContactEntries )] 2 [PrimaryKey( Id , autoIncrement = true)] 3 public class ContactEntry 5 [PrimaryKeyColumn(AutoIncrement = true)] 6 public int Id { get; set; } 8 [Required] 9 public string Name { get; set; }11 [Required]12 [EmailAddress]13 public string Email { get; set; }15 [SpecialDbType(SpecialDbTypes.NTEXT)]16 [Required]17 public string Message { get; set; }19 public DateTime Created { get; set; } View 1 @if (TempData[ success ] != null) 3 p Thank you for your message! /p 5 else 7 using (Html.BeginUmbracoForm ContactController ( HandlePost )) 9 div 10 @Html.LabelFor(m = m.Name)11 @Html.TextBoxFor(x = x.Name)12 @Html.ValidationMessageFor(m = m.Name)13 /div 15 div 16 @Html.LabelFor(m = m.Email)17 @Html.TextBoxFor(x = x.Email)18 @Html.ValidationMessageFor(m = m.Email)19 /div 21 div 22 @Html.LabelFor(m = m.Message)23 @Html.TextAreaFor(x = x.Message)24 @Html.ValidationMessageFor(m = m.Message)25 /div 27 div 28 button type= submit Submit /button 29 /div Controller 1 public class ContactController : SurfaceController 3 [ChildActionOnly] 4 public ActionResult Render() 6 return PartialView( ContactEntry ,new Models.ContactEntry()); 9 [HttpPost]10 public ActionResult HandlePost(Models.ContactEntry model)12 if (!ModelState.IsValid)13 return CurrentUmbracoPage();15 //add to db16 model.Created = DateTime.Now;17 var db = ApplicationContext.DatabaseContext.Database;18 db.Insert(model);20 //send email, do other things 22 TempData[ Success ] = true;23 return RedirectToCurrentUmbracoPage(); Of course you’ll also need to make sure your db table exist, that can be done with this piece of code 1 public class App: ApplicationEventHandler 3 protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) 5 var ctx = applicationContext.DatabaseContext; 6 var db = new DatabaseSchemaHelper(ctx.Database, applicationContext.ProfilingLogger.Logger, ctx.SqlSyntax); 8 //Check if the DB table does NOT exist 9 if (!db.TableExist( ContactEntries ))11 //Create DB table - and set overwrite to false12 db.CreateTable(false, typeof(ContactEntry));So far this has nothing to do with UI-O-Matic it’s just standard Umbraco, MVC and PetaPoco (to insert a db entry).But of course we now want to show these entries to the client…Adding UI-O-Matic V2 into the mixThe only thing we have to do now is install UI-O-Matic v2 (at this moment it’s in beta and you can get it from Nuget or build the latest source from github). In this example I’ve build the latest source and installed that but all these features will make it into the final release. 1 [TableName( ContactEntries )] 2 [PrimaryKey( Id , autoIncrement = true)] 3 [UIOMatic( contactentries , Contact Entries , Contact Entry , 4 FolderIcon = icon-users , ItemIcon = icon-user , 5 SortColumn = Created , SortOrder = desc , 6 RenderType = UIOMaticRenderType.List)] 7 public class ContactEntry 9 [PrimaryKeyColumn(AutoIncrement = true)]10 public int Id { get; set; }12 [Required]13 [UIOMaticListViewField]14 [UIOMaticField]15 public string Name { get; set; }17 [Required]18 [EmailAddress]19 [UIOMaticListViewField]20 [UIOMaticField]21 public string Email { get; set; }23 [SpecialDbType(SpecialDbTypes.NTEXT)]24 [Required]25 [UIOMaticListViewField]26 [UIOMaticField(View = UIOMatic.Constants.FieldEditors.Textarea)]27 public string Message { get; set; }29 [UIOMaticListViewField(Config = { format : {{value|relativeDate}} } )]30 [UIOMaticField(View = UIOMatic.Constants.FieldEditors.DateTime)]31 public DateTime Created { get; set; }So first we simply decorate the class with the UIOMatic attribute, providing a unique alias, a singular name and a plural name for our object and some optional parameters like which icon to use and that we would like to render in a list instead of the treeThen we can mark the items we want in the actual editor (marked with UIOMaticField, can also specify a different editor then textstring as you can see in the example)And mark the ones we want in the list view (marked with UIOMaticListViewField)ResultAs you see simply by decorating our model with some additional attributes we now get an entries viewer/editor! Kapow! Two weeks ago I spotted this message on my Github timelineand I must say I was pretty excited to see the great Matt Brailsford forking the UI-O-Matic repo. In his fork he has taken the project to the next level,  housekeeping and improving the codebase and implementing quite a few new features.What has changed?The project has undergone major refactoring, a lot has changed, so it is not possible to upgrade from v1 to v2 without having to make a couple of changes. To outline the main differences:No IUIOMatic interface and custom validation method but simply using standard .net data annotationsUIOMatic field property is now explicit, so if you want it to show up in the UI you need to mark your propertiesExplicit UIOMaticListViewField attribute (use this if you want properties to show up in the listview)Consistency in the js, no more uppercase properties, this has an impact if you created custom property views, use property.value instead of property.ValueWhat’s new?Core project (there are now 2 nuget packages one for the core and 1 for the ui)Folder SupportList view views (so you can now choose how to render a property in the list view, think image as an example)Filter option on the list view (with the new UIOMaticListViewFilter attribute)UIOMaticRepository (so devs can supply a custom repository object)Map field typeNew LogoInstallationYou can install the beta via Nuget:Install-Package Nibble.Umbraco.UIOMatic –PreDocumentationWe are currently working at updating the docs (and they should be ready when the final hits the shelves), you should be able to get started with what is currently available at http://uiomatic.readthedocs.io/en/stable/Reporting bugsYou can either submit them on the forum https://our.umbraco.org/projects/developer-tools/ui-o-matic/computer-says-no/ or directly on the issue tracker https://github.com/TimGeyssens/UIOMatic/issuesAgain credits for this release go out to Matt! H5YR! UI-O-Matic provides an simply way to validate your model by implementing the interface IUIOMatic and the member Validate() there you simply return a list of exceptions you wish to display to your editor, so you have full control over the validation logic… (as in the example shown here where it checks wether a value for the FirstName prop and LastName prop have been provider)But something I have been toying with is the idea to use a .net standard instead which are data annotations. So you can simply decorate your properties with those (like [Required])And luckily .net provides a way to manually validate those so we can actually use them allready, you’ll just have to write the validation code in the validate method and this end up with a mode like this 1 [UIOMatic( PollAnswers , icon-poll , icon-poll , RenderType = UIOMatic.Enums.UIOMaticRenderType.List)] 2 [TableName( PollAnswers )] 3 public class PollAnswer : IUIOMaticModel 5 [UIOMaticIgnoreField] 6 [PrimaryKeyColumn(AutoIncrement = true)] 7 public int Id { get; set; } 9 [Required]10 public string Answer { get; set; }12 [Required]13 [StringLength(6, MinimumLength = 3)]14 [RegularExpression(@ (\S)+ , ErrorMessage = White space is not allowed in the Poll Identifier field )]15 public string PollIdentifier { get; set; }17 public IEnumerable Exception Validate()19 var context = new ValidationContext(this, serviceProvider: null, items: null);20 var results = new List ValidationResult ();22 Validator.TryValidateObject(this, context, results,true);24 return results.Select(r = new Exception(r.ErrorMessage)); A year ago I introduced an Umbraco addon called UI-O-Matic which allows you to build up an integrated UI for your custom db tables in minutes (if you haven’t seen it I would recommend to check it out since it’s a real time saver!)But of course all the CRUD action happens in the backoffice and I thought it would be nice to allow you to easily genereate forms based on your UI-O-Matic models that you can use in the front end of your site.So I’m glad to present you a couple of Umbraco Forms addons that will make it super easy to link and submit data from your custom db tables.Prevalue source typeThe UI-O-Matic prevalue source type allows you to hook list field types (checkboxlist, dropdownlist, radiobuttonlist) to your Models (so to your db)Simply select the type of object (your available models decorated with the UIOMatic attribute) the sort column and order and that’s it… it will use the primary key column as prevalue id and the ToString() method on the model as the valueWorkflow typeThe UI-O-Matic workflow type allows you to extend the functionality of a form in this case it will allow you to submit your record to a database table. If you have ever used the save as Umbraco document workflow type it will look very similar, first select the type of object then you can map the different properties on that object to record fields or setup a static value.Data source typeAnd finally there is also a UI-O-Matic datasource type that will give you the option to generate a form based on a Model.Simply select the type of object and then follow the wizard.BonusA small bonus that you’ll also find in the project is an attribute that can be used to decorate your UI-O-Matic model so that it creates the assiciated table for you [CreateTable]Where can I get it?You can simply install it from Nuget. Of course this depends on Umbraco Forms and UI-O-Matic but Nuget should take care of the dependencies if you haven t already installed them.PM Install-Package Nibble.Umbraco.UIOMaticLovesFormsWhat’s next?I’ll try to do some follow up posts with real world examples. It must have been one of the first project I created for Umbraco (timestamp says it was released December 14, 2008 http://www.nibble.be/?p=53) But now at last it’s available as a Nuget package https://www.nuget.org/packages/Nibble.Umbraco.ConfigTree/Simply run the command Install-Package Nibble.Umbraco.ConfigTree in the package manager console of Visual Studio and you’ll have a new tree where you can edit all files in the /config folder of Umbraco.

TAGS:developer net cms 

<<< Thank you for your visit >>>

Websites to related :
RELAX NG home page

  has both an XML syntax and a compact non-XML syntaxdoes not change the information set of an XML documentsupports XML namespacestreats attributes unif

New Forest Holiday Park - Shamba

  Ideally situated to explore Hampshire and Dorset, Shamba Holidays offers touring and camping facilities in pleasant countryside close to the New Fores

Kalapa Media

  Currency Note:Orders shipped to the US will be charged in US dollars. Orders shipped to addresses outside the US will be charged in Canadian dollars.T

Restavracija Shambala | Dežela

  Dežela azijskih okusovnanash2021-08-19T13:49:41+02:00 Rezerviraj mizoShambala je več kot azijska restavracijaRazlog za to se verjetno skriva v dej

Home - Shambala

  In 2020, with no Shambala festival, we’ve been busy developing an ambitious five-year vision that includes environmental action, social justice and i

Muebles de madera natural y deco

  Muebles de madera natural y decoraci n | Banak Importa Utilizamos cookies propias y de terceros con finalidades anal ticas y para mostrarle publicidad

Educating, Counseling and Healin

  Ecopsychology: Educating, Counseling and Healing with Nature in Action. Heartfelt, Holistic Ecotherapy.  .  Project NatureConnect.Reasonably stop ou

Activist Trauma Support | home

  Activist Trauma Support 2004 - 2014Hello, and thank you for visiting the Activist Trauma Support Website. As of 2014 we no longer operate as a group,

Panic-And-Anxiety-Attacks.com

  Panic attacks and general anxiety can consume your life, robbing you of your confidence and joy.If you suffer from Palpitationsa pounding heart, or an

LEGENDS OF THE EAST ANGLIAN LAND

  the biggest collection anywhere of East Anglian legends and encounters with the ghostly hound Black Shuck 'standing stone', 'mark stone', glacial

ads

Hot Websites