How to configure default catalog and channel name of commerce server into sharepoint channel configuration list programmatically



Commerce server feature channel configuration "ChannelConfigurationFeature" creates the "Channel Configuration" list with content type named "ChannelStringContentType" once activated on sharepoint site.

It is important to note that if that content type or that list already exists, the activation process will first delete them and then recreate them. The list will have empty values.

In this list we configure the channel and default catalog name of commerce server which is used in application to get commerce server data from specific catalog.

Once you update the default catalog name and channel with required data, event receiver attached to the list add the entry in sharepoint property bag in key-Value pair format. And commerce server searches the sharepoint property bag for getting default catalog and channel value

Sharepoint property bag stores the default catalog name and channel value in key-value pair format as -

microsoft.commerce.settings.Channel - channel set to "Default" and it is case sensitive
microsoft.commerce.settings.DefaultCatalog - Name of the catalog

Problem

I have created one feature to update the channel configuration list programmatically by creating one feature receiver to avoid manual configuration. But application gives an error as mention below

Code under Feature Activated event is as follows-

using (SPWeb web = properties.Feature.Parent as SPWeb)
{
web.AllowUnsafeUpdates = true;
SPList ConfigurationList = web.Lists["Channel Configuration"];

  if (ConfigurationList != null)
     {

       SPListItem item = ConfigurationList.GetItemById(1);
            if (item["Value"] == null)
            {

item["Value"] = properties.Feature.Properties["DefaultValue"].Value;
item.Update();
}
      SPListItem item2 = ConfigurationList.GetItemById(2);

     if (item2["Value"] == null)
      {
      item2["Value"] = properties.Feature.Properties["CatalogValue"].Value;
      item2.Update();
      }

      ConfigurationList.Update()
}


DefaultValue and CatalogValue are the properties in feature.xml file

Error I got is as

Server Error in '/' Application.

The operation service has encountered an error while processing the request.  The error details have been logged by the service.

while processing the request. The error details have been logged by the service.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ServiceModel.FaultException`1[[Microsoft.Commerce.Contracts.Faults.GeneralOperationFault, Microsoft.Commerce.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: The operation service has encountered an error while processing the request. The error details have been logged by the service.

Source Error:
 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[FaultException`1: The operation service has encountered an error while processing the request.  The error details have been logged by the service.]

   Microsoft.Commerce.Broker.OperationService.ProcessRequest(CommerceRequest request) +329

   Microsoft.Commerce.Common.OperationServiceAgent.ProcessRequest(CommerceRequestContext requestContext, CommerceRequest request) +39

   Microsoft.Commerce.Portal.Common.SiteContext.ProcessRequest(CommerceRequest request) +40

   TietoEnator.DI.SharePoint.CommerceServer.CatalogHelper.GetCategories(String rootCategoryId, Boolean isRecursive) +679

   TietoEnator.DI.SharePoint.Web.UI.WebControls.CleanTwoLevelMenu.GetSitemapData() +521

   TietoEnator.DI.SharePoint.Web.UI.WebControls.CleanTwoLevelMenu.Render(HtmlTextWriter writer) +155

   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27

   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99

   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25

   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134

   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19

Solution

Reason is, it doesn't update the configured values in sharepoint property bag.

So programmatically we can add the key-values in sharepoint property bag as on Feature Activated event

// Remove default channel string from sharepoint property bag
 
if
(web.AllProperties.ContainsKey("microsoft.commerce.settings.Channel"))
{

web.AllProperties.Remove("microsoft.commerce.settings.Channel");                          
web.Properties["microsoft.commerce.settings.Channel"] = null;
web.Update();
web.Properties.Update();

}

// Add default Channel key-value into Property bag

 web.Properties["microsoft.commerce.settings.Channel"] = properties.Feature.Properties["DefaultValue"].Value;
web.AllProperties["microsoft.commerce.settings.Channel"] = properties.Feature.Properties["DefaultValue"].Value;

// Remove default catalog key-value from sharepoint Property bag if alredy exist

If
(web.AllProperties.ContainsKey("microsoft.commerce.settings.DefaultCatalog"))
{
web.AllProperties.Remove("microsoft.commerce.settings.DefaultCatalog");                      
web.Properties["microsoft.commerce.settings.DefaultCatalog"] = null;
web.Update();
web.Properties.Update();

}

// Add default catalog key-value into sharepoint property bag
web.Properties["microsoft.commerce.settings.DefaultCatalog"] = properties.Feature.Properties["CatalogValue"].Value;

web.AllProperties["microsoft.commerce.settings.DefaultCatalog"] = properties.Feature.Properties["CatalogValue"].Value;

Here we are adding property key-value in web.Properties and in web.AllProperties because Web.properties - add the key-value in sharepoint property bag but all in small case and channel value id case sensitive.

Web.AllProperties - add the key-value in sharepoint property bag as the case specified in value