Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • dominik 711 posts 733 karma points
    Feb 02, 2012 @ 09:25
    dominik
    0

    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

  • dominik 711 posts 733 karma points
    Feb 02, 2012 @ 10:17
    dominik
    0

    if i use properties directly in the usercontrol i am able to get these.

    How can i get public properties of the base class?

  • Rodion Novoselov 694 posts 859 karma points
    Feb 02, 2012 @ 13:37
    Rodion Novoselov
    0

    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.

  • dominik 711 posts 733 karma points
    Feb 02, 2012 @ 13:57
    dominik
    0

    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

  • Rodion Novoselov 694 posts 859 karma points
    Feb 02, 2012 @ 14:32
    Rodion Novoselov
    0

    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.

  • dominik 711 posts 733 karma points
    Feb 02, 2012 @ 16:56
    dominik
    0

    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

     

  • Rodion Novoselov 694 posts 859 karma points
    Feb 02, 2012 @ 17:18
    Rodion Novoselov
    1

    Well, 

     

    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:
                // .....
            }
        }
    }
     

    I hope it will help. 

  • dominik 711 posts 733 karma points
    Feb 02, 2012 @ 19:10
    dominik
    0

    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

  • Rodion Novoselov 694 posts 859 karma points
    Feb 02, 2012 @ 19:19
    Rodion Novoselov
    0

    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).

  • dominik 711 posts 733 karma points
    Feb 03, 2012 @ 08:00
    dominik
    0

    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

  • dominik 711 posts 733 karma points
    Feb 03, 2012 @ 08:10
    dominik
    0

    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

     

  • Rodion Novoselov 694 posts 859 karma points
    Feb 03, 2012 @ 08:11
    Rodion Novoselov
    0

    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.

  • dominik 711 posts 733 karma points
    Feb 03, 2012 @ 08:14
    dominik
    0

    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

  • Rodion Novoselov 694 posts 859 karma points
    Feb 03, 2012 @ 08:15
    Rodion Novoselov
    0

    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.

  • dominik 711 posts 733 karma points
    Feb 03, 2012 @ 08:19
    dominik
    0

    yes this should be done everytime i add or remove a new property to my usercontrol base class.

    This would be great

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft