How to Retrieve Site Terms
To retrieve a site term, you must know its name. You can retrieve a site term by using code or by using the Commerce Server Manager. This topic shows how to retrieve a site term in code.
To retrieve a site term
Add a drop-down list box to your Web page and name it ElementList. This list box will contain a list of all elements in the site term.
Create a ProfileContext object. This object is used to interact with the Profiles System runtime.
Call the GetSiteTerm method and pass it the site term that you are trying to retrieve.
Bind the XML structure results of the GetSiteTerm method call to the drop-down list box by assigning the DataSource property to the Elements property of the site term result.
Assign the DataTextField property of the drop-down list box to "Name" and assign the DataValueField to "Value".
Initiate the binding process by calling the DataBind method of the drop-down list.
Example
This example shows how to write site code that retrieves a specific site term from the Profiles System.
// Declare variables.
SiteTerm mySiteTerm = null;
string siteTerm = "Locations";
try
{
// Get runtime object.
ProfileContext ctxt = CommerceContext.Current.ProfileSystem;
// Get Site term.
mySiteTerm = ctxt.GetSiteTerm(siteTerm);
ElementList.DataSource = mySiteTerm.Elements;
ElementList.DataTextField = "Name";
ElementList.DataValueField = "Value";
ElementList.DataBind();
// Success!
Console.WriteLine("Successfully retrieved site term.");
}
catch (Exception ex)
{
Console.WriteLine("Exception: {0}\r\nMessage: {1}", ex.GetType(), ex.ToString());
if (ex.InnerException != null)
{
Console.WriteLine("\r\nInner Exception: {0}\r\nMessage: {1}", ex.InnerException.GetType(), ex.InnerException.Message);
}
}
Compiling the Code
- To compile the code, you need to include these namespace directives:
using Microsoft.CommerceServer.Runtime;
using Microsoft.CommerceServer.Runtime.Profiles;