Code to Retrieve and Display Profile Properties

Use the following code to retrieve and display 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.

<%@ 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;
   
      // Default page action.
      void Page_Load(Object sender, EventArgs e)
      {
         if (!IsPostBack)
         {
            LogonnameValidator.ErrorMessage = "* User does not exist *";
         }
      }

      // Event handle for the click event on the "Ok" button.
      public void Ok_Click(Object sender, EventArgs args)
      {
         DisplayProperties(LogonnameText.Text);
      }

      // This method retrieves the user profile for the specified logon
      // name, and displays the value for every property on this profile.
      void DisplayProperties(string logonName)
      {
         Profile userProfile;
         
         // Retrieve an existing profile from the 
         userProfile = profileContext.GetProfile("logon_name", logonName, "UserObject");
         
         if (userProfile == null)
         {
            LogonnameValidator.IsValid = false;
            ProfilePropertyGroups.DataSource = null;
            ProfilePropertyGroups.DataBind();                        
         }
         else
         {
            ProfilePropertyGroups.DataSource = userProfile.PropertyGroups;
            ProfilePropertyGroups.DataBind();
            
            BindProperties();
         }
      }
      
      // This method binds the properties collection on all the top-level property
      // groups to individual nested repeater controls.
      void BindProperties()
      {
         int i;
         ProfilePropertyGroup ppg;
         ProfilePropertyGroupCollection ppgc;
         
         ppgc = ProfilePropertyGroups.DataSource as ProfilePropertyGroupCollection;
         for (i = 0 ; i < ppgc.Count ; i++)
         {
            Repeater propertiesControl;

            // Retrieve the ith property group.            
            ppg = ppgc[i];
            
            // Bind the properties collection on this property group to the 
            // nested "ProfileProperties" repeater.
            propertiesControl = ProfilePropertyGroups.Items[i].FindControl("ProfileProperties") as Repeater;
            propertiesControl.DataSource = ppg.Properties;
            propertiesControl.DataBind();
         }      
      }
   </script>
   <body>
   <body>
      <form ID="displayprofile" method="post" >
         <asp:Label id="TitleLabel" Text="Display 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:Button id="Ok" Text="Ok" OnClick="Ok_Click"  Width="80" />
               </asp:TableCell>
            </asp:TableRow>
         </asp:Table>
         <asp:Repeater ID="ProfilePropertyGroups" Runat="server">
            <HeaderTemplate>
               <hr/>
            </HeaderTemplate>
            <ItemTemplate>
               <p>
               <%# DataBinder.Eval(Container.DataItem, "Name") %> :
               <br/>
               <asp:Repeater ID="ProfileProperties" Runat="server">
                  <ItemTemplate>
                     &nbsp;&nbsp;&nbsp;&nbsp;<%# DataBinder.Eval(Container.DataItem, "Name") %> : <%# DataBinder.Eval(Container.DataItem, "Value") %>
                     <br/>
                  </ItemTemplate>            
               </asp:Repeater>
            </ItemTemplate>         
            <FooterTemplate>
               <hr/>
            </FooterTemplate>
         </asp:Repeater>
      </form>
   </body>
</html>

See Also

CommerceContext Class (BCL)

Profile Class (BCL)

ProfileContext Class (BCL)

ProfilePropertyGroup Class (BCL)

ProfilePropertyGroupCollection Class (BCL)

Copyright © 2005 Microsoft Corporation.
All rights reserved.