Code to Use the Site Term Class
Use the following code to retrieve, iterate, sort, compare, and validate site terms. This code sample requires that the CommerceProfileModule object is configured in the web.config file. For more information about the CommerceProfileModule class, see Commerce Modules and CommerceProfileModule Class. The code sample 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" %>
<script language="C#" >
private void Page_Load(object sender, System.EventArgs e)
{
ProfileContext pc;
SiteTerm st;
SiteTermElementCollection stc;
SiteTermElement ste, ste2;
ArrayList stNames;
SiteTermElement [] stElementArray;
Profile profile;
try
{
if (CommerceContext.Current != null)
{
pc = CommerceContext.Current.ProfileSystem;
// Retrieve all the siteterms in the catalog.
stNames = pc.SiteTermNames;
if (stNames.Count > 0)
{
// Retrieve all the elements contained in a
// particular siteterm, encapsulated within
// a SiteTerm class.
st = pc.GetSiteTerm((string)stNames[0]);
// Retrieve the siteterms in a collection
// and iterate through them.
stc = st.Elements;
Response.Write("SiteTerm: " + stNames[0] + " has " + stc.Count + " elements.<br>");
foreach (SiteTermElement steTemp in stc)
{
Response.Write("Name: " + steTemp.Name + " Value: " + steTemp.Value + "<br>");
}
Response.Write("<br><br>");
// Sort days of the week siteterm and display them.
stElementArray = new SiteTermElement[stc.Count];
stc.CopyTo(stElementArray, 0);
Array.Sort(stElementArray);
Response.Write("SiteTerm: " + stNames[0] + " in sorted order.<br>");
foreach (SiteTermElement steTemp in stElementArray)
{
Response.Write("Name: " + steTemp.Name + " Value: " + steTemp.Value + "<br>");
}
Response.Write("<br><br>");
// Reference the siteterm within the collection by name.
ste = stc["Guest User"];
Response.Write("Reference a siteterm element from a collection by name.<br>");
Response.Write("Name: " + ste.Name + " Value: " + ste.Value + "<br>");
Response.Write("<br><br>");
// Compare siteterm elements.
ste2 = stc["Registered User"];
if (ste.Equals(ste2))
Response.Write("The two SiteTermElement objects are equal.<br>");
else
Response.Write("The two SiteTermElement objects are not equal.<br>");
Response.Write("<br><br>");
// Validate a profile property through the siteterm class.
profile = pc.GetProfile("logon_name", "username", "UserObject");
if (profile != null)
{
' if there really is a profile in the profile store
if (st.Validate(profile.Properties[0]))
Response.Write("The profile property is validated.<br>");
else
Response.Write("The profile property is not valid.<br>");
}
else
Response.Write("The profile does not exist.<br>");
Response.Write("<br><br>");
}
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
</script>
<HTML>
<HEAD>
<title ID=L_Login_HTMLTitle>Login</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="https://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
</body>
</HTML>
See Also
SiteTermElementCollection (BCL)
Copyright © 2005 Microsoft Corporation.
All rights reserved.