Friday, June 11, 2010

Using Commerce.Scriptor pipeline component in Microsoft Commerce Server 2007/09

Commerce.Scriptor pipeline component is used to inject small functionality into the pipeline. This component is generally used to verify the values and perform some arithematic operations on the values in orderform object. By using scriptor component we can perform small operations like applying certain tax % for all users, change price of the product based on some key in product info.

This component is not used where we need to access information from other sources like getting data from database or accessing other modules in commerce server using API.

We can pass key/Value pairs to the component using its interface. We can access these key/value pairs from 'config' parameter of MSCSExecute method.

Here are the few links from mdsn to configure and use scriptor component

http://msdn.microsoft.com/en-us/library/ms946865(CS.70).aspx
http://msdn.microsoft.com/en-us/library/ms944284(CS.70).aspx

Examples for using Scriptor component -

Applying value in 'DiscountPrice' property as product price if it is not null:

Requirement:

For every product there is a property called 'DiscountPrice'. If that property is not null then the value in that property should be considered as product price.

What to do:

Create a pipe line component which replaces the values for dictionary keys _cy_iadjust_regularprice, cy_placed_price if that is not null. To know more about the dictionary keys in order form got to http://msdn.microsoft.com/en-us/library/ms962766(CS.70).aspx.

Where to add the pipeline component:

In basket pipeline 'Query catalog info' queries the for catalog info from catalog module and creates the dictionary keys for all product properties like '_product_*' (* being name of the property). In 'Item price' stage after the 'requiredItemPriceCy' we have to add the scriptor component because in the 'requiredItemPriceCy' component values for the above said dictionary keys are calculated. we have to replace the calculated values.

(To know more about the commerce server pipeline components in all pipelines goto http://msdn.microsoft.com/en-us/library/ms916292(CS.70).aspx ).

How to add the pipeline component:
  1. Open basket pipeline of the application. 
  2. Go to Item price stage and right-click on requiredItemPriceCy component and insert component -> after. 
  3. Select stages as current stage and select commerce scriptor component and click ok. 
  4. Go to component properties and scriptor tab. 
  5. Check the internal radio and click on edit. 
  6. It will open editor window where we need to copy the code in VB script. 
  7. Copy the below code in the editor and click ok. 
  8. Restart IIS. 
Coding in scriptor component:

There are three functions to implement using scriptor component
  1. MSCSExecute 
  2. MSCSOpen 
  3. MSCSClose
We have good documentation on these on above said MSDN links.

The only method that is compulsory and required is MSCSExecute. This method is used to implement the desired functionality. This method have four parameters namely
  1. config - Contains key/value pairs entered on the pipeline interface. 
  2. orderform - Contains key/value pairs of orderform (or basket). 
  3. context - Contains key/value pairs of context 
  4. flags
We have good documentation on these on http://msdn.microsoft.com/en-us/library/ms944284(CS.70).aspx.

How does Code Works:

orderform parameter is of type dictionary. Iterate through each Item and look for the key( key need not be present for every item as it depends on product definition). Read the value and if it is not null then change the values of the price keys to this value.
function MSCSExecute(config, orderform, context, flags)
   Dim x
   for each item in orderform.Value("Items")
     for each key in item
     if key="_product_DiscountPrice" then
      x = item.Value("_product_DiscountPrice")
      if IsNull(x) then
      else
         if IsNumeric(x) then
           item.Value("_cy_iadjust_regularprice") = x
           item.Value("cy_placed_price") = x
        end if
      end if
      Exit for
     end if
   next
  next
 MSCSExecute = 1
end function

sub MSCSOpen(config)
end sub

sub MSCSClose()
end sub

How to register Microsoft Commerce Server 2007/09 pipeline components in 64-bit servers

To know how to develop the pipelines check the below link:



After developing the pipelines by following above post there will be a problem in registering the pipelines for 64 bit server. so follow the below procedure to register:
  1. Sign the class library for strong name.
  2. You will get two files <path>.dll and <path>.tlb file in the bin after you build the library.
  3. Open Command prompt and change directory to C:\Windows\Microsoft.NET\Framework64\v2.0.50727
  4. Run the below command in the command prompt --> Regasm.exe .dll /tlb:.tlb
  5. Open pipereg.exe (Follow max akbar post http://blogs.msdn.com/b/maxakbar/archive/2006/09/02/736999.aspx to know how to use this tool). Choose to Register and export the data.
  6. Open the exported data (For ex: pipecomp.reg) and execute it (Double click). This will change the registry values.
  7. Check for the dll in the assembly (GAC). If not add to it.
  8. Restart IIS.
  9. Open the pipeline editor for 64 bit. Try to add the custom pipeline component to the stage you assigned while registering it. If your component is visible and able to add to pipeline then registering is done and successful.

How to create new pipeline component in Microsoft Commerce Server

For developing order processing pipeline one should have good knowledge on
  1. Order processing pipelines and its default components.
  2. Dictionary keys used to get various properties and dictionary objects in it. (Ex: orderform has collection of Lineitem objects)
  3. Debugging pipelines
1. Read the documentation for good knowledge on order processing pipelines and its components.
Here are few links that help you

2. Commerce uses dictionary keys to map the properties in the .Net object model to COM orderform object.
For example orderform (or basket ) has property called lineitemcollection this collection is saved as Isimplelist in Orderform dictionary object with dictionary key as ‘Items’. To retrieve this Isimplelist use below line of code
ISimpleList items =(ISimplelist)orderform["Items"];
This mapping of .net class and COM objects is done using pipelinemappings.xml which we can find website physical directory. Apart from these keys there are various keys added at each stage in the pipeline. To know the keys and values at each stage choose any of the below two ways -
  • Enable logging of each pipeline and analyse it (How to enable logging – Go to web.config file and look for pipelines tag for corresponding pipeline change the ’loggingEnabled’ attribute value to true. Log will be placed at folder name log in the pipelines folder of application physical directory).
  • You can display the contents of the orderform by following this link ( http://msdn.microsoft.com/en-us/library/dd451239(CS.90).aspx). DumpOrder.vbs can be found at C:\Program Files (x86)\Microsoft Commerce Server 2007\Sdk\Samples\DumpOrder.
For list of all dictionary keys in orderform for developing pipelines go to below link: http://msdn.microsoft.com/en-US/library/dd442188(v=CS.90).aspx

3. Debugging a pipeline is very important while we developing. Download orderform Visualizer tool and install it. To know how to use and where to download find below links


Below are max akbar links that guide to create a pipeline component