Umbraco package use of package.xml file

I am trying to create a new Umbraco Package (in version 13) with simple contact form that can be wrapped in a macro and used on landing pages.
Completed a sample package using Creating a Package | Umbraco CMS
However there is no mention of how to include code files such as C# Controllers and Views.

What does package.xml file is used for?

My project has following files

  • ~/MessagingService.cs (handles email sending)
  • ~/Controllers/ContactFormController.cs (inherits SurfaceController and process form submission)
  • ~/Views/Index.cshtml (display email form)
  • ~/Views/MacroPartials/ContactFormWrapper.cshtml (Macro to wrap up email form)
  • ~/App_Plugins/SimpleContactForm/* (all files as in documentation Creating a Package | Umbraco CMS to give introduction of the package)

Thanks for any help

Hi Miflun,

The package.xml is mostly used to bring over umbraco items like document types, media types, etc…
If you want to also have your nuget package include things like c# files and views, you’ll want to include that as part of a library project.

Going back to the documentation in Creating a Package | Umbraco CMS. The step called “Generate an empty package using a template” generates that c# library for you. Here you’ll be able to add your controllers along with your views. For the views, you’ll have to do something special in order to package them, you can read more about that here: Umbraco 10 - Razor Class Library Packages Pt1 - DEV Community.

Following all the steps in the documentation should leave you with a nuget package which can include c# code and following the above article you’ll be able to also bring the views with you.

Good luck!

1 Like

Thanks for your feedback. I did manage to pack my library. Can see that the code in the Services and Controller is working fine. (Manage to make it work by adding missing View and Macro in the project that is using my umbraco package, and also had to register my service Dependency Injection in Program.cs)
(I will get my code onto a public repository soon)

I was expecting that I could easily get my Macro with Marco Partial view and Macro parameters into the package. I can see this in the package.xml file. As mentioned earlier the documentation Creating a Package | Umbraco CMS do mention about generating package.xml and then abruptly moves onto “Creating a NuGet package” without specifying how to use this package.xml

I have expolred article Macro Parameter Editors | Umbraco CMS, though it didn’t helped much, will try to create a sample macro and see if it works.

I have given a read to article Umbraco 10 - Razor Class Library Packages Pt1 - DEV Community that you have mentioned, I have to go through it again to properly understand it.

I am now left with

  • Get Macro and Macro Partial View in package
  • Find a way to add custom views
  • Register Services through Dependecy Injection when package is installed

Thanks

I think you are able to import your package.xml by using the ImportPackage.FromEmbeddedResource<CustomPackageMigration>().Do(); specified in this section Creating a Package | Umbraco CMS. Though I have never really tried it, so I don’t know how well it works

Hi #Patrickdemooij9,

Thanks for pointing to custom migration. I have not used it as I need to upskill myself on using Razor Class Library. However, I found a way using Automatic Package Migration and Package.xml file.

I have created Git Repository with full source code GitHub - miflun/UmbracoContactForm: This repository demonstrate Umbraco Package Simple Contact Form with Macro and a NuGet package Miflun.SimpleContactForm (currently under prerelease)

FYI, just be aware of the slight annoyance of package.xml and templates.. in that really you want your cshtml delivered via standard nuget files.. but the package.xml migration also insists on the template.cshtml being in the source of the xml.. and as that runs post nuget delivering you views it overwrites.
Also whilst you can use the package.xml approach to subsequently update doctypes (and I think i does merge new props if your doctype has been changed by the end user) for datatypes if there is an existing datatype (not sure if that’s by key or id.. this is a very old process) then it simply exits the update in the automatic package migration.

There is also an issue for anyone using usync/umb deploy.. as updating doctypes/datatypes via the automatic package migration doesn’t issue any notifications (by design)… so if you have usync set to import on startup say.. it immediately reverts any doctype/datatype changes made as updates via the APM as it runs post migrations.. and it has no knowledge of the changes made..
You need to hook into the INotificationHandler<MigrationPlansExecutedNotification> and do any resaving..

conversation on discord Discord
or here if you aren’t on discord
Package developers.. I see I can use Umbraco #package-development

The-Starter-Kit/src/Umbraco.SampleSite/Migrations/PostMigrationNotificationHandler.cs at v14/dev · umbraco/The-Starter-Kit

Depending on how much creation is going on.. you might want to do this all via the umbraco services.. and there’s a great article (for older version but still easy enough to port) on scrift…

Working with Content Types and Data Types Programmatically by Anders Bjerner | Issue 97 of Skrift Magazine

Hi @mistyn8, Thanks for taking time to go through this topic and adding valueable guidence. I totally agree with you. In fact, Umbraco documentation Good practice and defaults | Umbraco CMS do mention about using Razor Class Library (RCL) for creating the Views. As suggested, once I have some time I will try to implement it using code and get rid of Package.xml dependency.