How to get the information you need from the object sender parameter in any method

   —   
This article describes how go through the object sender parameter to get the information you need. This example describes how to get the Shopping Cart object in a custom macro inserted into an invoice.
You will need a copy of Visual Studio (the Express version is sufficient) installed and the project needs to be set up for debugging. Please place a break point into the class implementing your custom macro. You can use the default one as described in our documentation. You can place a breakpoint in the sample class in the method MacroResolver_OnResolveCustomMacro. You can see that the method has two parameters. I this example we will be using only the first one. Now run the web site or project in the debugging mode. You have to place a custom macro in your invoice so the macro gets hit, for example you can use the macro below:

{#mymacro#}

The macro doesn’t have to exist; you need only to hit the breakpoint. Save the invoice and place an order so the macro gets resolved. Your breakpoint should get hit when finishing the order. Now let’s debug the object sender parameter and let’s find our shopping cart associated with the macro call. Please hover over the object sender object and expand the plus sign:

CMSSolution_WebDeveloper-(Debugging)-Visual-Web-Developer-2008-Express-Edition_2012-02-27_11-35-17.png

You can see that there are different objects and base classes in the object structure. Now is the most important class for us the CMS.GlobalHelper.MacroResolver class. We will use it later. Now find by scrolling down or clicking the small black arrow in the bottom the SourceData property:

CMSSolution_WebDeveloper-(Debugging)-Visual-Web-Developer-2008-Express-Edition_2012-02-27_11-37-25.png

By expanding it we found the object passed down by the macro. It’s the ShoppingCartInfo we will need to use in our code. The blue highlighted items are classes we will need to use to cast the undefined object to a system specific object. The green highlighted items are used to reference the ShoppingCartInfo object within the sender object. Here is the complete code:

((CMS.Ecommerce.ShoppingCartInfo)((CMS.GlobalHelper.MacroResolver)sender).SourceData[0])

This object can now be used as a ShoppingCartInfo object and you can access all its initialized properties:

CMSSolution_WebDeveloper-(Debugging)-Visual-Web-Developer-2008-Express-Edition_2012-02-27_11-44-33.png

This approach can be used also for other methods where the object sender parameter is passed through. Just make sure the object is initialized. Here is the corresponding order in Kentico:

CMS-Desk-E-commerce-New-order-Mozilla-Firefox_2012-02-27_12-25-34.png
 
-bp-


See also: http://devnet.kentico.com/docs/6_0/devguide/index.html?types_of_macros.htm
http://www.dotnetperls.com/debugging
http://devnet.kentico.com/docs/6_0/ecommerceguide/invoice.htm


Applies to: Kentico CMS 6.0
Share this article on   LinkedIn