How to Work with Profiles in Commerce Server

This sample shows how to use key Multi-Channel Foundation concepts to:

  • Create, Read, Update and Delete operations on a user profile

  • Expose a new custom profile (Organization)

  • Create, Read, Update and Delete operations on the Organization profile

  • Create a new translator for the Organization profile

  • Create a custom operation sequence components to handle the operations on organizations

  • Customize the MetadataDefinitions.xml to include the new Organization profile

  • Customize the ChannelConfiguration.config file to include the new Organization components within the operation sequence

Note This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

Prerequisites

  • You can access the sample code by extracting the .zip archive in the samples folder you selected when you installed the Commerce Server 2009 R2 SDK Samples. In order to use these samples it is assumed that you have extracted them.

Unpackage and Configure the PUP

You must first unpackage and configure the included Commerce Server site via the PUP file SolutionStorefrontWithSampleData.pup.

The samples depend upon the data within this package.

To create the Commerce Server site

  1. In Windows Explorer, navigate to the <dir>\Pup Packages.

  2. Double-click the SolutionStorefrontWithSampleData.pup file.

  3. In the Unpack screen, select Quick Unpack, and then click Next.

  4. In the Quick Unpack screen, ensure that the SQL Server computer name is correct, enter the SQL Server login name and password or select Windows Authentication as the authentication method, and then click Next.

  5. In the Profiling System screen, accept the defaults, and then click Next.

  6. In the second Profiling System screen, accept the defaults, and then click OK.

  7. In the Commerce Server Site Packager dialog box, click OK.

  8. In the Unpacking is complete! screen, click Finish.

The Commerce Server site is unpacked to the Solution Storefront in IIS.

Building the Solution

You will have to build the ProfileAPIUsage.sln before proceeding.

To build the solution within Visual Studio

  1. In Windows Explorer, go to <dir>\Extensibility Kits\Samples\.

  2. Unzip the FoundationSamples.zip to a new directory.

  3. Browse to the Profile folder.

  4. Double-click the ProfileAPIUsage.sln file. This opens the solution in Visual Studio.

  5. Under Solution Explorer, right-click Solution <solution name>, and from the context menu select Build Solution.

Creating a Web Application

Use the following steps to add the sample application to your Commerce Server site in Internet Information Services (IIS). When you do so, you will choose an IIS application pool to assign to the Catalog Web application. To make sure that the application works correctly, the application pool you select must comply with the following:

  • The application pool must have all the necessary security privileges to access your Commerce Server site and its associated databases.

To add the application to the Commerce Server site in IIS 7.0

  1. In Internet Information Services (IIS) Manager, in the navigation tree, click Sites, right-click Default Web Site, and then click Add Application.

  2. In the Alias box, type a name for your sample.

  3. Click the Select button next to the Application pool box, select the application pool to use, and then click OK.

    Note

    The application pool must have all the necessary security privileges to access your Commerce Server site and its associated databases.

  4. In the PhysicalPath box, type the full path of the application. This is the path to which you browsed to the FoundationSamples\Profile folder.

    For example, when you extract the FoundationSamples.zip to the same folder, the application path is as follows: %commerce_server_root%Extensibility Kits\Samples\FoundationSamples\Profile.

  5. Click OK.

  6. In Internet Explorer, go to the following URL to access the Web application:

    https://localhost/<your_sample_vdir>/Default.aspx,

    where <your_sample_vdir> is the value used in step 4.

Example

The following code snippet illustrates how to create a user profile. It assumes usage of wrappers that are included in the sample.

{
            CommerceCreate<UserProfile> userCreate = new CommerceCreate<UserProfile>();
            userCreate.Model.Email = email;
            userCreate.Model.FirstName = firstName;
            userCreate.Model.LastName = lastName;
            userCreate.Model.Password = password;
            userCreate.Model.PasswordQuestion = passwordQuestion;
            userCreate.Model.PasswordAnswer = passwordAnswer;

            if (string.IsNullOrEmpty(organization))
            {
                CommerceDeleteRelationship<Organization> organizationDelete = new CommerceDeleteRelationship<Organization>(UserProfile.RelationshipName.Organization);
                userCreate.RelatedOperations.Add(organizationDelete);
            }
            else
            {
                OrganizationCommands orgCommands = new OrganizationCommands();
                List<OrganizationData> data = orgCommands.Select(organization);
                if (data != null && data.Count > 0 && !string.IsNullOrEmpty(data[0].Id))
                {
                    CommerceCreateRelationship<Organization> organizationCreate = new CommerceCreateRelationship<Organization>(UserProfile.RelationshipName.Organization);
                    organizationCreate.SearchCriteria.Model.Id = data[0].Id;
                    userCreate.RelatedOperations.Add(organizationCreate);
                }
                else
                {
                    throw new FaultException<ItemDoesNotExistFault>(new ItemDoesNotExistFault());
                }
            }

            CommerceContext.Current.OperationServiceAgent.ProcessRequest(
                CommerceContext.Current.GetCurrentRequestContext(),
                userCreate.ToRequest());
        }

The following code snippet illustrates how to query a user profile, by email address. It assumes usage of wrappers that are included in the sample.

{
            if (string.IsNullOrEmpty(emailAddress))
            {
                return new List<UserData>() { new UserData() };
            }

            // Retreive Addresses, PreferredAddress, Organization
            CommerceQueryRelatedItem<Address> addressesQuery = new CommerceQueryRelatedItem<Address>(UserProfile.RelationshipName.Addresses);
            CommerceQueryRelatedItem<Address> preferredAddressQuery = new CommerceQueryRelatedItem<Address>(UserProfile.RelationshipName.PreferredAddress);
            CommerceQueryRelatedItem<Organization> organizationQuery = new CommerceQueryRelatedItem<Organization>(UserProfile.RelationshipName.Organization);

            // Build the user query.
            CommerceQuery<UserProfile> userQuery = new CommerceQuery<UserProfile>();
            userQuery.SearchCriteria.Model.Email = emailAddress;
            userQuery.RelatedOperations.Add(addressesQuery);
            userQuery.RelatedOperations.Add(preferredAddressQuery);
            userQuery.RelatedOperations.Add(organizationQuery);

            // Execute the query.
            CommerceResponse response = CommerceContext.Current.OperationServiceAgent.ProcessRequest(
                CommerceContext.Current.GetCurrentRequestContext(),
                userQuery.ToRequest());

            List<UserData> userList = new List<UserData>();
            CommerceQueryOperationResponse queryResponse = (CommerceQueryOperationResponse)response.OperationResponses[0];
            if (queryResponse.Count > 0)
            {
                UserProfile user = queryResponse.CommerceEntities[0];
                user.Password = UserData.MaskedPassword;
                user.PasswordAnswer = UserData.MaskedPassword;
                userList.Add(UserData.Create(user));
            }
            else
            {
                userList.Add(new UserData());
            }

            return userList;
        }

The following code snippet illustrates how to create a relationship between a User and an Organization. It assumes usage of wrappers that are included in the sample.

            // User to update
            CommerceUpdate<UserProfile> userUpdate = new CommerceUpdate<UserProfile>();
            userUpdate.SearchCriteria.Model.Id = id;

            CommerceCreateRelationship<Organization> organizationCreate = 
new CommerceCreateRelationship<Organization>(UserProfile.RelationshipName.Organization);

            organizationCreate.SearchCriteria.Model.Id = orgId;
            userUpdate.RelatedOperations.Add(organizationCreate);