Last active
August 25, 2020 01:21
-
-
Save csharpforevermore/510b9bac44944bccaf28511f11451fcd to your computer and use it in GitHub Desktop.
Create a Content App in Umbraco using C# rather than package.manifest. For documentation see the website (https://bit.ly/3jbH1zE), the PDF (https://bit.ly/34t2Iqz) and the YouTube video (https://bit.ly/2EsUELE). You have the code-first or configuration-first approaches. The configuration-first approach uses the package.manifest file. Code-first …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Http.Filters; | |
using Umbraco.Core.Models.ContentEditing; | |
using Umbraco.Web.Editors; | |
using Umbraco.Web.Models.ContentEditing; | |
public class MyContentAppFactory | |
{ | |
// Works in v7 | |
public void Initialize() => | |
// on startup bind to this event | |
EditorModelEventManager.SendingContentModel += EditorModelEventManager_SendingContentModel1; | |
// Event handler | |
private void EditorModelEventManager_SendingContentModel1( | |
HttpActionExecutedContext sender, | |
EditorModelEventArgs<ContentItemDisplay> e) | |
{ | |
var apps = e.Model.ContentApps.ToList(); // get current apps list | |
// TODO: I could add/remove/modify content app based on server side logic | |
// Example - add a custom content app before the "Info" app | |
apps.Insert(apps.Count - 1, new ContentApp | |
{ | |
Alias = "dynamicApp", | |
Name = "Dynamic App", | |
Icon = "icon-flash", | |
View = "../App_Plugins/MyContentApp/dynamic.html", | |
// TODO: I could change the model based on server side logic | |
ViewModel = new | |
{ | |
name = "Hello", | |
description = "I am a dynamic content app" | |
} | |
}); | |
//Re-assign | |
e.Model.ContentApps = apps; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using Umbraco.Core.Models; | |
using Umbraco.Core.Models.ContentEditing; | |
using Umbraco.Core.Models.Membership; | |
public class MyContentAppFactory : IContentAppFactory | |
{ | |
public ContentApp GetContentAppFor(object source, IEnumerable<IReadOnlyUserGroup> userGroups) | |
{ | |
// I have access to the current IContent | |
if (!(source is IContent content)) | |
return null; | |
// TODO: I could add/remove/modify apps based on server side logic | |
return new ContentApp | |
{ | |
Alias = "myForm", | |
Name = "My Form", | |
Icon = "icon-edit", | |
View = "../App_Plugins/MyContentApp/myForm.html", | |
// TODO: I could change the model based on server side logic | |
ViewModel = new | |
{ | |
name = "Hello", | |
description = "I am a dynamic content app" | |
} | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dashboards": [ | |
{ | |
"name": "My Form", | |
"alias": "myForm", | |
"view": "~/App_Plugins/MyContentAPp/myForm.html", | |
"icon": "icon-edit", | |
"show": [ "+content/*" ] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added Umbraco v7 and Umbraco v8 implementations of Content App using code-first approach with C#, and the configuration-first approach using the package.manifest