We are using a base class for our usercontrols. This base class includes about 20 poperties.
If i create a new c# usercontrols and try to get all of the properties into umbraco it does not find any of these.
I can not add all properties for each usercontrol manually. Is there a way how i can get parameters from my usercontrol baseclass into the umbraco macro parameter section?
Hi. There's a good reason for such a behaviour. How on earth the macro property finder would distinguish between your own properties inherited from the base class and other public properties inherited from the UserControl one and so on up along the whole hierarchy. I'm not sure if there's any better workaround but by now I can see only one straightforward way - to declare your property as virtual and override them in the derived control so that an overriding implementation calls the base one.
I'm not certain, but the only option that comes to my mind is to write some code to handle the events of the umbraco.cms.businesslogic.macro.Macro class so that to automate the process of adding common properties.
using umbraco.BusinessLogic; using umbraco.cms.businesslogic; using umbraco.cms.businesslogic.macro;
public class AutoMacroPropertyApplication: ApplicationBase { public AutoMacroPropertyApplication() { Macro.New += Macro_New; }
private void Macro_New(Macro sender, NewEventArgs e) { // we will use "*" in the name to signal that we want automatic properties to be added // actuallly any condition based on the name pattern can be used here if (sender.Name.StartsWith("*")) { // remove the signal character sender.Name = sender.Name.Substring(1); sender.Alias = sender.Alias.Substring(1);
// add the macro property MacroProperty.MakeNew( sender, true, // visible "myAutoProperty", // alias "My Auto Property", // name // prop type aliases can be observed from the dbo.cmsMacroPropertyType table: new MacroPropertyType("text") ).Save(); // here we can add any additional properties: // ..... } } }
Nope. It should go to a separate .cs file inside the App_Code folder, either you can build it as a separate assembly and put it into ~/bin/.
As for "*" - it asks for some explanation - when you create a new macro there's no way to determine automatically if you need to add the set of those predefined parameters or not. So I put this check to distinguish that. I.e. if you call "Macros" -> "Create" and type something like "*My Macro" in the "Name" textbox then the macro is created with those properties added and the "*" removed from the name as well (see comments to the code). If you type just "My Macro" then the macro is created usual way. (Probably there could be a better solution but by than it was the one that came to my mind instantly as the simplest).
So if i add a new property to my usercontrol Base class i have also to add the property to the new class "AutoMacroPropertyApplication"? also all my "custom" usercontrols are starting with "ucMacroName" so i can look for "uc" instead of "*". Is this correct?
So if i add a new property to my usercontrol Base class i have also to add the property to the new class "AutoMacroPropertyApplication"?
Yeah, this code supposes it, however you can elaborate it so to make it enumerate those properties automatically for you (by reflection) - you can yet investigate the source code of umbraco to learn how it does it.
all my "custom" usercontrols are starting with "ucMacroName" so i can look for "uc" instead of "*". Is this correct?
Exactly. The only point is that inside the New event only the name of a new macro is yet available - so you can use there any mark to distinguish the macros of interest (like a specific prefix or suffix or name pattern, etc). I used a star "*" prefix mainly for demonstration purposes.
I just tried and it is working. But only if I add a new macro. If i add a new property to the base class all existing usercontrols (starting with prefix) should get this property automatically. Otherwise i have to update about 50 usercontrols :-)
Get macro properties from usercontrol base class
Hello,
We are using a base class for our usercontrols. This base class includes about 20 poperties.
If i create a new c# usercontrols and try to get all of the properties into umbraco it does not find any of these.
I can not add all properties for each usercontrol manually. Is there a way how i can get parameters from my usercontrol baseclass into the umbraco macro parameter section?
Thanks
if i use properties directly in the usercontrol i am able to get these.
How can i get public properties of the base class?
Hi. There's a good reason for such a behaviour. How on earth the macro property finder would distinguish between your own properties inherited from the base class and other public properties inherited from the UserControl one and so on up along the whole hierarchy. I'm not sure if there's any better workaround but by now I can see only one straightforward way - to declare your property as virtual and override them in the derived control so that an overriding implementation calls the base one.
Thanks Rodion,
My problem is thate we have about 40 usercontrols which should all have the same properties (from usercontrol baseclass).
If i now add a new usercontrol I have to add my 20 macro parameters manually?
is there any way to just use properties from base class with a specific prefix ? For example all properties with prefix "custom" are shown?
If i set the properties in each usercontrol and i want to add a new property to my base class i have to add it to each of my usercontrol manually.
Thanks
I'm not certain, but the only option that comes to my mind is to write some code to handle the events of the umbraco.cms.businesslogic.macro.Macro class so that to automate the process of adding common properties.
Thanks Rodion,
Can you please tell me how this can be done. I am new to umbraco and asp.net so it would be great if you can explain it a little bit more detailed.
Thanks a lot
Well,
I hope it will help.
Thanks,
Where i have to write this code?
In my user control base class or in each of my usercontrols?
So each parameter which starts with an * will be parsed to the umbraco macro propertie window?
I think this would be great
Nope. It should go to a separate .cs file inside the App_Code folder, either you can build it as a separate assembly and put it into ~/bin/.
As for "*" - it asks for some explanation - when you create a new macro there's no way to determine automatically if you need to add the set of those predefined parameters or not. So I put this check to distinguish that. I.e. if you call "Macros" -> "Create" and type something like "*My Macro" in the "Name" textbox then the macro is created with those properties added and the "*" removed from the name as well (see comments to the code). If you type just "My Macro" then the macro is created usual way. (Probably there could be a better solution but by than it was the one that came to my mind instantly as the simplest).
Hi Rodion,
So if i add a new property to my usercontrol Base class i have also to add the property to the new class "AutoMacroPropertyApplication"?
also all my "custom" usercontrols are starting with "ucMacroName" so i can look for "uc" instead of "*". Is this correct?
Thanks for all your help
Hi Rodion I think i got it working but it only works if i create a new macro.
If i add now a new property to my base class how can i update the properties of all macros in the umbraco backend?
Until now i have to go each own macro and add thsi new property manually but it should be done automatically
THanks
So if i add a new property to my usercontrol Base class i have also to add the property to the new class "AutoMacroPropertyApplication"?
Yeah, this code supposes it, however you can elaborate it so to make it enumerate those properties automatically for you (by reflection) - you can yet investigate the source code of umbraco to learn how it does it.
all my "custom" usercontrols are starting with "ucMacroName" so i can look for "uc" instead of "*". Is this correct?
Exactly. The only point is that inside the New event only the name of a new macro is yet available - so you can use there any mark to distinguish the macros of interest (like a specific prefix or suffix or name pattern, etc). I used a star "*" prefix mainly for demonstration purposes.
Thanks Rodion,
I just tried and it is working. But only if I add a new macro. If i add a new property to the base class all existing usercontrols (starting with prefix) should get this property automatically. Otherwise i have to update about 50 usercontrols :-)
Will this also work?
Thanks
If you think it's worth it you could write a small utility to add properties to already existing macros so that not to do it by hand.
yes this should be done everytime i add or remove a new property to my usercontrol base class.
This would be great
Thanks
is working on a reply...