How to Manipulate Expressions

When Commerce Server Core Systems saves a new expression to the Expression Store, a unique expressionId is created. You can use this ID to access the expression. This topic describes how to retrieve the expressionId by using the name of the expression. After you retrieve the expressionId, you rename the expression and change the description. Finally, you delete the expression from the Expression Store.

To manipulate expressions

  1. In Visual Studio, create a new Commerce Server Core Systems Web application.

  2. Add the Microsoft.CommerceServer.CrossTierTypes and Microsoft.CommerceServer.Marketing.CrossTierTypes references to the application.

  3. Add a using directive for the Microsoft.CommerceServer and Microsoft.CommerceServer.Marketing namespaces.

  4. Define the Marketing Web Service URL.

  5. Create the MarketingContext.

    For more information, see How to Connect to the Marketing System. In this example, the MarketingContext object is named marketingSystem.

  6. Translate the name of the expression, "Test Expression1", created in How to Create a New Expression into its expression ID.

  7. Rename the expression to "Test Expression2" and change the expression description.

  8. After you finish using the expression, delete it.

    If the expression has a non-zero reference count (one or more other expressions depend on this expression), the method will fail and the expression will not be deleted.

Example

The following code example illustrates how to manipulate expressions.

using Microsoft.CommerceServer;
using Microsoft.CommerceServer.Marketing;

namespace CommerceServerMarketingExamples
{
    public class CSMarketingExamples
    {
        public void ManipulateExpressions()
        {
            try
            {
                // Set the URL to the Web service.
                string marketingWebServiceUrl = @"https://localhost/MarketingWebService/MarketingWebService.asmx";
                // Create an instance of the Marketing System agent.
                MarketingServiceAgent ma = new MarketingServiceAgent(marketingWebServiceUrl);
                MarketingContext marketingSystem = MarketingContext.Create(ma);

                // Look up the ID of "Test Expression1".
                SearchClauseFactory searchClauseFactory = marketingSystem.Expressions.GetSearchClauseFactory();
                SearchClause sc = searchClauseFactory.CreateClause(ExplicitComparisonOperator.Equal, "Name", "Test Expression1");
                DataSet dataSet = marketingSystem.Expressions.Search(sc);
                int ExpressionID = (int)dataSet.Tables["SearchResults"].Rows[0][0];

                // Retrieve the Expression record and rename it to "Test Expression2".
                Expression ex = marketingSystem.Expressions.GetExpression(ExpressionID);
                ex.Name = "Test Expression2";
                ex.Description = "Description for Test Expression2";
                //Overwrite is unnecessary.
                ex.Save(false);   

                // Delete "Test Expression2".
                marketingSystem.Expressions.Delete(ExpressionID);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}\r\nMessage: {1}", ex.GetType(), ex.Message);
                if (ex.InnerException != null)
                    Console.WriteLine("\r\nInner Exception: {0}\r\nMessage: {1}", ex.InnerException.GetType(), ex.InnerException.Message);
                Console.ReadLine();
            }
        }
    }
}

See Also

Other Resources

Marketing Management Scenarios