Commerce Server 2009 Product Query via IronPython
I again lifted some code from a blog entry titled "Using Commerce Server 2009 outside of a Web Application". Rather than trying to do something with the Order sub-system, I decided to amuse myself by porting this product query to IronPython.
Porting it was very straight forward as long as you are familiar with Python and know how to port the IronPython specific things (importing the clr, adding references, IronPython syntax for .NET generics, etc.). The trick is knowing how to bring in the app.config and the ChannelConfiguration.xml, and MetadataDefinitions.xml). I confess I stole this info too from this posting on the IronPython mailing list.. In a nutshell since ipy.exe, IronPython, is the host, you supply your app.config through ipy.exe.config and just place the other files in the same directory as your executable.
1 import clr
2 import System.AppDomain
3
4 clr.AddReference("Microsoft.Commerce.Application.Common")
5 clr.AddReference("Microsoft.Commerce.Broker")
6 clr.AddReference("Microsoft.Commerce.Common")
7 clr.AddReference("Microsoft.Commerce.Contracts")
8 clr.AddReference("Microsoft.Commerce.Providers")
9
10 from System.Threading import Thread
11 from Microsoft.Commerce.Contracts.Messages import CommerceRequestContext
12 from System import Guid
13 from Microsoft.Commerce.Common import OperationServiceAgent
14 from Microsoft.Commerce.Common.MessageBuilders import CommerceQuery
15 from Microsoft.Commerce.Contracts import CommerceEntity
16
17 rq = CommerceRequestContext()
18 rq.UserId = Guid.NewGuid().ToString("B")
19 rq.Channel = "Default"
20 rq.UserLocale = Thread.CurrentThread.CurrentCulture.Name
21 rq.UserUILocale = Thread.CurrentThread.CurrentUICulture.Name
22 rq.RequestId = Guid.NewGuid().ToString("B")
23
24 query = CommerceQuery[CommerceEntity]("Product")
25 query.Model.Properties.Add("Id")
26 query.Model.Properties.Add("DisplayName")
27 query.Model.Properties.Add("Description")
28 query.Model.Properties.Add("ListPrice")
29 query.SearchCriteria.Model.Id = "AW099-15"
30 query.SearchCriteria.Model.Properties["CatalogId"] = "Adventure Works Catalog"
31
32 osa = OperationServiceAgent()
33 responses = osa.ProcessRequest(rq, query.ToRequest())
34 response1 = responses.OperationResponses[0]
35 product = response1.CommerceEntities[0]
36
37 print product.Properties["Id"]
38 print product.Properties["DisplayName"]
39 print product.Properties["Description"]
40 print product.Properties["ListPrice"]
The output is... of course
C:\Program Files\IronPython 2.0.2>ipy cs2009.py AW099-15 Venus Hinged crampons for snow and moderate ice, step-in bindings, one size fits men's 4-12. 99.0000 C:\Program Files\IronPython 2.0.2>
Compiling the CS2009 Extensibilty Kit (aka the source code for "SharePoint Commerce Services")
A couple of tips:
When you extract/unzip, unzip to a location other than where the kit zip file is found (C:\Program Files\Microsoft Commerce Server 2007\Microsoft Commerce Server 2009\Sdk\Samples, otherwise the path becomes too long for the Visual Studio to handle. Try something like C:\src\CommerceSharePointExtensibilityKit.
If (when) you run into the compiler error, see the Extensibility Kit Will Not Build After Extraction in the release notes for the fix.