Export (0) Print
Expand All

Code to Create and Set Profile Properties

Commerce Server 2002

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).gif Note

  • 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#" runat="server">
      
      // 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" runat="server">
         <asp:Label id="TitleLabel" Text="Create a profile" runat="server" /><br>
         <br>
         <asp:Table runat="server" id="profileData" BorderWidth="2">
            <asp:TableRow>
               <asp:TableCell>
                  <asp:Label id="LogonNameLabel" Text="Name:" runat="server" />
               </asp:TableCell>
               <asp:TableCell>
                  <asp:TextBox id="LogonnameText" runat="server"></asp:TextBox>
                  <asp:RequiredFieldValidator id="LogonnameValidator" runat="server" ControlToValidate="LogonnameText" Display="dynamic"></asp:RequiredFieldValidator>
               </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
               <asp:TableCell>
                  <asp:Label id="PasswordLabel" Text="Password:" runat="server" />
               </asp:TableCell>
               <asp:TableCell>
                  <asp:TextBox id="PasswordText" TextMode="Password" runat="server"></asp:TextBox>
                  <asp:RequiredFieldValidator id="PasswordValidator" runat="server" ControlToValidate="PasswordText" Display="dynamic"></asp:RequiredFieldValidator>
               </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
               <asp:TableCell>
                  <asp:Label id="FirstNameLabel" Text="First Name:" runat="server" />
               </asp:TableCell>
               <asp:TableCell>
                  <asp:TextBox id="FirstnameText" runat="server"></asp:TextBox>
                  <asp:RequiredFieldValidator id="FirstNameValidator" runat="server" ControlToValidate="FirstnameText" Display="dynamic"></asp:RequiredFieldValidator>
               </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
               <asp:TableCell>
                  <asp:Label id="LastNameLabel" Text="Last Name:" runat="server" />
               </asp:TableCell>
               <asp:TableCell>
                  <asp:TextBox id="LastnameText" runat="server"></asp:TextBox>
                  <asp:RequiredFieldValidator id="LastNameValidator" runat="server" ControlToValidate="LastnameText" Display="dynamic"></asp:RequiredFieldValidator>
               </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
               <asp:TableCell>
                  <asp:Label id="EmailLabel" Text="Email Address:" runat="server" />
               </asp:TableCell>
               <asp:TableCell>
                  <asp:TextBox id="EmailText" runat="server"></asp:TextBox>
                  <asp:RegularExpressionValidator id="EMailValidator" runat="server" ControlToValidate="EmailText" ValidationExpression=".*@.*\..*" Display="dynamic"></asp:RegularExpressionValidator>
               </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
               <asp:TableCell>
                  <asp:Button id="Ok" Text="Ok" OnClick="Ok_Click" runat="server" Width="80" />
               </asp:TableCell>
               <asp:TableCell>
                  <asp:Button id="Cancel" Text="Cancel" OnClick="Cancel_Click" runat="server" 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.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft