Поделиться через


How to Manipulate Lists

This topic shows you how to copy a list, add and remove users from a list, append and subtract members of other lists. It also shows list deletion features of the list management object model. The example code builds on the "Test Mailing List" example created in How to Create a List in the Marketing System.

To manipulate lists

  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 object.

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

  6. Open the "Test Mailing List" created in How to Create a List in the Marketing System by using the SearchClauseFactory object.

  7. Retrieve the saved list.

  8. Copy the "Test Mailing List" to a new list named "Copied List1."

  9. Map the copied list to a static list.

  10. Delete two members from the "CopiedList1" list.

  11. Add a new user to the "CopiedList1" list.

  12. Create a new list named "Appended List" and save it to the database.

    You must save new lists before you add new records, append operations, and delete operations.

  13. Append all records from "Copied List1" to "Appended List".

  14. Create a new "Subtract List" and add a name from the copied list.

  15. Subtract the member of the "Subtract List" from the "Append List".

    In the example, the record "barneyuser@fabrikam.com" will be removed from the "Append List".

  16. Delete "Copied List1" and "Subtract List".

Example

The following code example illustrates how to manipulate lists.

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

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

                // Retrieve the GUID of the "Test Mailing List" mailing list.
                SearchClauseFactory searchClauseFactory = marketingSystem.MailingLists.GetSearchClauseFactory();
                SearchClause sc = searchClauseFactory.CreateClause(ExplicitComparisonOperator.Equal, "Name", "Test Mailing List");
                DataSet dataSet = marketingSystem.MailingLists.Search(sc);
                Guid listId = (Guid)dataSet.Tables["SearchResults"].Rows[0][0];

                // Retrieve the mailing list.
                MailingList ml = marketingSystem.MailingLists.GetList(listId);

                // Copy the mailing list.
                MailingList cml = marketingSystem.MailingLists.CreateCopy("Copied List1", ml);

                // Map the copied list to a static list.
                StaticList scl = (StaticList)cml;

                // Remove users.
                scl.RemoveUser("joeuser@treyresearch.com");
                scl.RemoveUser("barneyuser@fabrikam.com");

                // Add a user.
                MailingListUser mlu = new MailingListUser("test@fabrikam.com");
                scl.AddUser(mlu);

                // Create a new appended list and save it in the database.
                StaticList al = marketingSystem.MailingLists.NewStaticList();
                al.Name = "Appended List";
                al.Save();
                // Append the copied list to the appended list.
                al.Append(scl);

                // Create a new subtract list and add one user from the copied list.
                StaticList sl = marketingSystem.MailingLists.NewStaticList();
                sl.Name = "Subtract List";
                MailingListUser slu = new MailingListUser("barneyuser@fabrikam.com");
                sl.Save();
                sl.AddUser(slu);
                // Subtract the members of the subtract list.
                al.Subtract(sl);
                //Save the subtracted list.
                sl.Save();

                // Delete the copied list and subtract list.
                marketingSystem.MailingLists.Delete(cml.Id);
                marketingSystem.MailingLists.Delete(sl.Id);
            }
            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

List and Direct Mail Management Programming Concepts