Bagikan melalui


How to Create a Profile Independently from a Commerce Server Site

For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.

You can create a profile independently from a Commerce Server site by using the Profiles Web service or by using the Profiles runtime. This topic shows how to create an instance of the Web service and pass the required XML element structure to create a new profile. It also shows how to use the Profiles runtime to perform the same functionality.

To create a profile by using the Profiles Web service

  1. Create an instance of the ProfilesWebService object.

  2. Specify the credentials needed by the Profiles Web service.

  3. Using a string variable, construct an XML representation of the profile that you want to create.

  4. Create an XML document from the XML string.

  5. Convert the XML document into an XML element.

  6. Call the CreateProfile method of the ProfilesWebService object.

To create a profile by using the Profiles runtime

  1. Create an instance of the ProfileContext object from the Profiles runtime.

  2. Create a new Profile object, and specify a new GUID value for the UserObject profile.

  3. Specify the properties for the Profile object.

  4. Call the Update method of the Profile object to set the properties and create the profile.

Example

This example creates an instance of the Profiles Web service. It uses the CreateProfile method to create a new profile, passing in the XML element structure needed to pass the default required properties. The example also builds the XML element structure by first constructing an XML string so that it can be easily read for example purposes.

// Declare and set variables.
XmlDocument profileXMLDoc = new XmlDocument();
XmlElement profileXMLElement = null;
String profileXML = "";
String newGuid = "{" + Guid.NewGuid().ToString() + "}";
String password = "password";
String eMail = "someone@example.com";
String passwordQuestion = "My Question";
String passwordAnswer = "My Answer";
String accountStatus = "1"; 
// active

try
{
    // Create an instance of the Profiles Web reference.
    ProfilesWS.ProfilesWebService profileWS = new ProfilesWS.ProfilesWebService();

    // Pass the correct credentials to the Web service.
    profileWS.Credentials = CredentialCache.DefaultNetworkCredentials;

    // Create an XML representation of the Profile.
    profileXML += "<ProfileDocument>";
    profileXML += "  <UserObject>";
    profileXML += "    <GeneralInfo>";
    profileXML += "      <user_id>" + newGuid + "</user_id>";
    profileXML += "      <user_security_password>" + password + "</user_security_password>";
    profileXML += "      <email_address>" + eMail + "</email_address>";
    profileXML += "      <password_question>" + passwordQuestion + "</password_question>";
    profileXML += "      <password_answer>" + passwordAnswer + "</password_answer>";
    profileXML += "    </GeneralInfo>";
    profileXML += "    <AccountInfo>";
    profileXML += "      <account_status>" + accountStatus + "</account_status>";
    profileXML += "    </AccountInfo>";
    profileXML += "  </UserObject>";
    profileXML += "</ProfileDocument>";

    // Load the Profile into an XML document.
    profileXMLDoc.LoadXml(profileXML);

    // Convert the XML document to an XMLElement.
    profileXMLElement = profileXMLDoc.DocumentElement;

    // Create the Profile. The false parameter indicates that this is not created from a BizTalk adapter.
    profileWS.CreateProfile(ref profileXMLElement, false);


    // Write a message to the console when the profile is successfully created. 
    Console.WriteLine("Successfully created profile");
}
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 creates an instance of the ProfileContext object. It uses the CreateProfile method to create a new profile object. Required properties are set, and then the ProfileContext is updated by calling the Update method. The parameter values for this example are the same as those specified in the previous example.

//  Declare and set variables.
String newGuid = "{" + Guid.NewGuid().ToString() + "}";
String password = "password";
String eMail = "someone@example.com";
String passwordQuestion = "My Question";
String passwordAnswer = "My Answer";
String accountStatus = "1"; //active

try
{
    // Get the Profiles runtime object.
    ProfileContext ctxt = CommerceContext.Current.ProfileSystem;

    // Create a new profile object.
    Profile prof = ctxt.CreateProfile(newGuid, "UserObject");

    // Set the profile properties.
    prof.Properties["GeneralInfo.user_security_password"].Value = password;
    prof.Properties["GeneralInfo.email_address"].Value = eMail;
    prof.Properties["GeneralInfo.password_question"].Value = passwordQuestion;
    prof.Properties["GeneralInfo.password_answer"].Value = passwordAnswer;
    prof.Properties["AccountInfo.account_status"].Value = accountStatus;

    // Update the profile with the property values.
    prof.Update();

    // Write a message to the console when the profile is successfully created.
    Console.WriteLine("Successfully created profile");
}
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 when you are using the Profiles Web service, you need to include these namespace directives:

    using System.Xml;
    using System.Net;
    
  • If you are using the Profiles Web Service, add a reference to the Profiles Web service. Example 1 assigns the Profiles Web Service the alias profileWS. For example, this is a valid reference:

    http://localost/ProfilesWebService/ProfilesWebService.asmx
    
  • To compile the code to use the Profiles runtime, you need to include these namespace directives:

    using Microsoft.CommerceServer.Runtime;
    using Microsoft.CommerceServer.Runtime.Profiles;
    

See Also

Other Resources

Profiles Concepts and Tasks

Developing with the Profiles System

Web Service Programming Sample