Commerce Foundation CommerceRequestContext

Each commerce request has a CommerceRequestContext. At a minimum, the CommerceRequestContext specifies the following properties: channel, request identifier, user identifier, user locale, and user interface locale. See Commerce Foundation Presentation.

However, using the CommercePropertyCollection property bag, you can add properties to the Solution Storefront SiteContext or Commerce Foundation CommerceRequestContext. When you add properties to the Solution Storefront SiteContext using the CommercePropertyCollection property bag, these properties are copied to the Commerce Foundation CommerceRequestContext before the commerce request is sent to the Commerce Foundation.

The Commerce Foundation API selects discounts and ads based on targeting information, such as user ID or product ID. By adding properties to the CommercePropertyCollection property bag, you can extend the targeting of advertisements and discounts to include other properties. You may also use the CommercePropertyCollection property bag to pass additional information to an operation sequence.

Note

The channel between the presentation and application tiers may not necessarily be encrypted in your implementation. Please consider this when passing any personally identifiable information or other sensitive data within the Commerce Foundation request context.

Accessing the Collection from a SharePoint SiteContext

The Microsoft.Commerce.Portal.Common assembly contains the SiteContext object for a SharePoint site. This object, which exists for the life cycle of the page request, presents a CommercePropertyCollection that allows for adding arbitrary properties, such as targeting properties. When a request is issued to the Commerce Foundation, these properties are copied into the Commerce Foundation request RequestContext.Properties collection.

Targeting Context Collection on Request Context

A CommercePropertyCollection is available though the request context. This collection holds all properties including the existing properties of the CommerceRequestContext. The properties can be set or added to the CommercePropertyCollection using the Add method, the SetPropertyValue method, or a square bracket indexer. The properties may be accessed using the GetPropertyValue method or the square bracket indexer.

Setting Properties

The properties of the SiteContext and CommerceRequestContext can be set using the SetPropertyValue method, the Add method, or the square bracket indexer.

Using SetPropertyValue

CommerceRequestContext extensibleContext =...
extensibleContext.SetPropertyValue("CurrentCategory", "Boots");

Using Add

CommerceRequestContext extensibleContext =...
extensibleContext.Properties.Add("CurrentCategory", "Boots");

Using square brackets

CommerceRequestContext extensibleContext =...
extensibleContext.Properties["CurrentCategory"] = "Boots";

Accessing Properties

In an operation sequence component, the properties of the CommerceRequestContext can be accessed using the GetPropertyValue method or the square bracket indexer.

Using GetPropertyValue

CommerceRequestContext requestContext = CommerceOperationContext.CurrentInstance.RequestContext;
            
if(requestContext.Properties.ContainsProperty("CurrentCategory") &&                !string.IsNullOrEmpty(requestContext.GetPropertyValue("CurrentContext") as string))
{
    currentCategory = requestContext.GetPropertyValue("CurrentContext") as string;
}

Using square bracket accessor

CommerceRequestContext requestContext = CommerceOperationContext.CurrentInstance.RequestContext;

if (requestContext.Properties.ContainsProperty("CurrentCategory") &&
                requestContext.GetPropertyValue("CurrentCategory") != null)
{
   property = requestContext.Properties["CurrentCategory"];
}

See Also

Other Resources

Developing with the Multi-Channel Commerce Foundation

Commerce Foundation Objects and Models

Commerce Foundation Presentation