Skip to content

Instantly share code, notes, and snippets.

@csharpforevermore
Last active August 25, 2020 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csharpforevermore/510b9bac44944bccaf28511f11451fcd to your computer and use it in GitHub Desktop.
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 …
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;
}
}
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"
}
};
}
}
{
  "dashboards": [
    {
      "name": "My Form",
      "alias": "myForm",
      "view": "~/App_Plugins/MyContentAPp/myForm.html",
      "icon": "icon-edit",
      "show": [ "+content/*" ]
    }
}
@csharpforevermore
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment