How to Create and Update Site Terms
For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.
The process of updating site terms is almost the same as creating site terms. Both processes build an XML element structure containing the site terms and their definitions. The only difference is that if the site term does not exist, it must be created. After it is created, it can be updated using the same XML structure, but supplying different values.
The XML element structure required to either create or update a site term needs these elements and attributes.
Site Term Element |
Description |
---|---|
Term |
Root structure of the element. The Term element requires these attributes:
|
Value |
One or more Value elements are required to be inside the Term element to indicate which values are to be available to the site term. Each Value element requires these attributes:
|
To create or update site terms
Create a ProfilesServiceAgent object by connecting to the Profiles Web service.
Set the credentials on the ProfilesServiceAgent object.
Create a ProfileManagementContext object, and pass the instance of the ProfilesServiceAgent object as an argument.
Build an XML representation of the desired site term to create or update the site term and store it in a string variable.
Load the string into an XML document.
Convert the XML document into an XML element.
Call the CreateSiteTerm method on the ProfileManagementContext object, and pass it the XML element.
Example
This example creates a new site term with the name Locations. It contains two separate values for the site terms, one for US and one for UK. The following code sample first connects to the Profiles Web service, and creates a ProfileManagementContext object. Then, it calls the CreateSiteTerm method to create the site term.
try
{
// Connect to the Profiles Web service by using the agent.
ProfilesServiceAgent agent = new ProfilesServiceAgent("https://localhost/ProfilesWebService/ProfilesWebService.asmx");
agent.Credentials = CredentialCache.DefaultNetworkCredentials;
ProfileManagementContext ctxt = ProfileManagementContext.Create(agent);
// Declare variables.
XmlDocument profileXmlDoc = new XmlDocument();
XmlElement siteTermXml = null;
String Xml = "";
// Create an XML representation of the site term.
Xml += " <Term xmlns='https://schemas.microsoft.com/CommerceServer/2006/03/SiteTerms' name='Locations' description='Service Locations' displayName='Locations'>";
Xml += " <Value name='0' description='US' displayName='US' />";
Xml += " <Value name='1' description='UK' displayName='UK' />";
Xml += " </Term>";
// Load the site term into an XML document.
profileXmlDoc.LoadXml(Xml);
// Convert the XML document to an XML element.
siteTermXml = profileXmlDoc.DocumentElement;
// Create the site term.
ctxt.CreateSiteTerm(siteTermXml);
// Success!
Console.WriteLine("Successfully created 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);
}
}
}
This example builds on the prior example and updates the site term named Locations. It adds two more locations, FR and ES. The following code sample first connects to the Profiles Web service and creates a ProfileManagementContext object. Then it calls the UpdateSiteTerm method to update the existing site term.
Note
If you try to update a site term that does not exist, an error will occur.
try
{
// Connect to the Profiles Web service by using the agent.
ProfilesServiceAgent agent = new ProfilesServiceAgent("https://localhost/ProfilesWebService/ProfilesWebService.asmx");
agent.Credentials = CredentialCache.DefaultNetworkCredentials;
ProfileManagementContext ctxt = ProfileManagementContext.Create(agent);
// Declare variables.
XmlDocument profileXmlDoc = new XmlDocument();
XmlElement siteTermXml = null;
String Xml = "";
// Create an XML representation of the site term.
Xml += " <Term xmlns='https://schemas.microsoft.com/CommerceServer/2006/03/SiteTerms' name='Locations' description='Service Locations' displayName='Locations'>";
Xml += " <Value name='0' description='US' displayName='US' />";
Xml += " <Value name='1' description='UK' displayName='UK' />";
Xml += " <Value name='2' description='FR' displayName='FR' />";
Xml += " <Value name='3' description='ES' displayName='ES' />";
Xml += " </Term>";
// Load the site term into an XML document.
profileXmlDoc.LoadXml(Xml);
// Convert the XML document to an XML element.
siteTermXml = profileXmlDoc.DocumentElement;
// Update the site term.
ctxt.UpdateSiteTerm(siteTermXml);
// Success!
Console.WriteLine("Successfully updated 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.Profiles;
using System.Xml;