Code to Create and Set Profile Properties

Use the following code to create and set profile properties. The code should be used within a Commerce Server project. For more information about creating a Commerce Server project, see Creating a Commerce Project.

Ee798058.note(en-US,CS.20).gifNote

  • The error messages presented in this code sample should be modified to meet your security needs. Error messages should inform the user of the error, but reveal as little information about the environment or internal error as possible. This code sample is intended to showcase different Profiling System APIs, and should not be construed as the recommended secure way to write Profiling System code.
<%@ Page language="c#" %>
<%@ Import Namespace="Microsoft.CommerceServer.Runtime" %>
<%@ Import Namespace="Microsoft.CommerceServer.Runtime.Profiles" %>
<html>
   <script language="C#" >
      
      // Web-form scoped variables.
      ProfileContext profileContext = CommerceContext.Current.ProfileSystem;
   
      void Page_Load(Object sender, EventArgs e)
      {
         if (!IsPostBack)
         {
            LogonnameValidator.ErrorMessage = "* Invalid logon name *";
            PasswordValidator.ErrorMessage = "* Invalid password *";
            FirstNameValidator.ErrorMessage = "* Invalid first name *";
            LastNameValidator.ErrorMessage = "* Invalid last name *";
            EMailValidator.ErrorMessage = "* Invalid email address *";
         }
      }

      // Event handler for the "Cancel" button click.
      public void Cancel_Click(Object sender, EventArgs args)
      {
         // Simply clear off values associated with the text box controls.
         LogonnameText.Text = "";
         PasswordText.Text = "";
         FirstnameText.Text = "";
         LastnameText.Text = "";
         EmailText.Text = "";
      }

      // Event handler for the "OK" button click.
      public void Ok_Click(Object sender, EventArgs args)
      {
         if (IsValid)
         {
            Profile userProfile;
            ProfilePropertyGroup generalInfo;
   
            // Create user profile by this name.         
            userProfile = profileContext.CreateProfile(LogonnameText.Text, "UserObject");
   
            // Different ways to set single-valued profile properties.
            // To learn about setting multi-valued profile properties, please refer
            // to reference documentation for ProfileProperty.Value.
            userProfile["GeneralInfo.user_security_password"].Value = PasswordText.Text;
            userProfile.Properties["GeneralInfo.first_name"].Value = FirstnameText.Text;
            userProfile.PropertyGroups["GeneralInfo"]["last_name"].Value = LastnameText.Text;

            generalInfo = userProfile.PropertyGroups["GeneralInfo"];
            generalInfo["email_address"].Value = EmailText.Text;

            // Persist user profile to underlying stores.
            userProfile.Update();
         }
      }
   </script>
   <body>
         <form ID="createprofile" method="post" >
         <asp:Label id="TitleLabel" Text="Create a profile"  /><br>
         <br>
         <asp:Table  id="profileData" BorderWidth="2">
            <asp:TableRow>
               <asp:TableCell>
                  <asp:Label id="LogonNameLabel" Text="Name:"  />
               </asp:TableCell>
               <asp:TableCell>
                  <asp:TextBox id="LogonnameText" ></asp:TextBox>
                  <asp:RequiredFieldValidator id="LogonnameValidator"  ControlToValidate="LogonnameText" Display="dynamic"></asp:RequiredFieldValidator>
               </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
               <asp:TableCell>
                  <asp:Label id="PasswordLabel" Text="Password:"  />
               </asp:TableCell>
               <asp:TableCell>
                  <asp:TextBox id="PasswordText" TextMode="Password" ></asp:TextBox>
                  <asp:RequiredFieldValidator id="PasswordValidator"  ControlToValidate="PasswordText" Display="dynamic"></asp:RequiredFieldValidator>
               </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
               <asp:TableCell>
                  <asp:Label id="FirstNameLabel" Text="First Name:"  />
               </asp:TableCell>
               <asp:TableCell>
                  <asp:TextBox id="FirstnameText" ></asp:TextBox>
                  <asp:RequiredFieldValidator id="FirstNameValidator"  ControlToValidate="FirstnameText" Display="dynamic"></asp:RequiredFieldValidator>
               </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
               <asp:TableCell>
                  <asp:Label id="LastNameLabel" Text="Last Name:"  />
               </asp:TableCell>
               <asp:TableCell>
                  <asp:TextBox id="LastnameText" ></asp:TextBox>
                  <asp:RequiredFieldValidator id="LastNameValidator"  ControlToValidate="LastnameText" Display="dynamic"></asp:RequiredFieldValidator>
               </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
               <asp:TableCell>
                  <asp:Label id="EmailLabel" Text="Email Address:"  />
               </asp:TableCell>
               <asp:TableCell>
                  <asp:TextBox id="EmailText" ></asp:TextBox>
                  <asp:RegularExpressionValidator id="EMailValidator"  ControlToValidate="EmailText" ValidationExpression=".*@.*\..*" Display="dynamic"></asp:RegularExpressionValidator>
               </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
               <asp:TableCell>
                  <asp:Button id="Ok" Text="Ok" OnClick="Ok_Click"  Width="80" />
               </asp:TableCell>
               <asp:TableCell>
                  <asp:Button id="Cancel" Text="Cancel" OnClick="Cancel_Click"  CausesValidation="false" Width="80" />
               </asp:TableCell>
            </asp:TableRow>
         </asp:Table>
      </form>
   </body>
</html>

See Also

CommerceContext Class (BCL)

Profile Class (BCL)

ProfileContext Class (BCL)

ProfilePropertyGroup Class (BCL)

Copyright © 2005 Microsoft Corporation.
All rights reserved.