How to Create a New Expression

This topic describes how to create a new expression.

To create a new expression

  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 the example, the MarketingContext object is named marketingSystem.

  6. Create the expression XML element clause for the Body property.

    You can validate the expression XML before saving by calling the EvaluateXML method of the ExpressionEvaluator class. No error checking is performed on the syntax of this XML element when the Expression object is saved.

  7. Create the new expression. Supply the body clause as defined in step 3.

    The NewExpression method saved with property isLocal=false stores the expression in the general expression store. The NewExpression object saved with property isLocal=true requires that the expression be linked to an advertisement, discount, or direct mail. If the expression is meant to appear as a target expression in the Expressions module of the Marketing Manager, the Category property must be set to TargetCondition.

Example

The following code example illustrates how to create a new expression.

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

namespace CommerceServerMarketingExamples
{
    public class CSMarketingExamples
    {
        public void NewExpression()
        {
            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);

                XmlDocument xmlDoc = new XmlDocument();
                XmlElement clause = xmlDoc.CreateElement("CLAUSE");
                clause.SetAttribute("OPER", "equal");

                XmlElement property = xmlDoc.CreateElement("PROPERTY");
                property.SetAttribute("ID", "UserObject.GeneralInfo.first_name");
                property.SetAttribute("TYPE", "STRING");

                XmlElement immedval = xmlDoc.CreateElement("IMMED-VAL");
                immedval.SetAttribute("TYPE", "STRING");

                XmlElement value = xmlDoc.CreateElement("VALUE");
                value.InnerText = "Steve";

                immedval.AppendChild(value);
                clause.AppendChild(property);
                clause.AppendChild(immedval);

                // Create a new expression.
                Expression ex = marketingSystem.Expressions.NewExpression(false);
                ex.Description = "Test expression1";
                ex.Body = clause;
                ex.Name = "Test Expression1";
                ex.Category = "TargetCondition";
                ex.Save(true);
            }
            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.WriteLine("\n");
            }
        }
    }
}

See Also

Other Resources

Marketing Management Scenarios