שתף באמצעות


How to Access Embedded Profiles

This topic describes how to use the Profiles Web Service to create an Address profile object that adds two addresses: one for work and one for home. These objects are then embedded into an Organization profile object.

To access embedded profiles

  1. Create an instance of the ProfileManagementContext object and pass it the URL of the Profiles Web Service.

  2. Using string variables, create XML representations of both work and home address information.

  3. Convert the XML string for the work address into an XML document.

  4. Call the CreateProfile method of the ProfileManagementContext object for the work address.

  5. Repeat steps 3 and 4 for the home address.

  6. Create an instance of the ProfileContext run-time object to create an Organization profile object.

  7. Assign the GeneralInfo.address_list property for the GUID of the work address.

  8. Assign the GeneralInfo.name property. This property is required to create the Organization profile.

  9. Update the profile by calling the Update method of the Profile object.

  10. Assign the GeneralInfo.address_list property for the GUID of the home address. You do not have to specify the GeneralInfo.name property again because it has already been specified for the current Organization profile object.

  11. Update the profile by calling the Update method of the Profile object.

Example

The following code example shows how to access embedded profiles. It connects to the Profiles Web service and creates a ProfileManagementContext object. It then creates two Address profiles (one for work and one for home), and embeds the addresses into an Organization profile.

Note

This example shows how to create addresses by using the Profiles Web Service, but the embedded Organization profile uses the Profiles runtime. The only reason for doing this is to illustrate that your code can work with either method.

// Declare variables.
XmlDocument profileXMLDoc = new XmlDocument();
String profileXMLWork = "";
String profileXMLHome = "";
String guidWork = Guid.NewGuid().ToString();
String guidHome = Guid.NewGuid().ToString();
String guidOrg = Guid.NewGuid().ToString();

// Access the Profiles Web service.
ProfileManagementContext ctxtWS = ProfileManagementContext.Create(
    "https://localhost/ProfilesWebService/ProfilesWebService.asmx");

try
{

    // Create XML representation of Address profile (work address).
    profileXMLWork += @"<ProfileDocument>";
    profileXMLWork += @"  <Address>";
    profileXMLWork += @"    <GeneralInfo>";
    profileXMLWork += @"      <address_id>{" + 
        guidWork + "}</address_id>";
    profileXMLWork += @"      <address_name>Work</address_name>";
    profileXMLWork += @"      <address_line1>One Microsoft  Way</address_line1>";
    profileXMLWork += @"      <region_code>WA</region_code>";
    profileXMLWork += @"      <postal_code>98052</postal_code>";
    profileXMLWork += @"      <country_code>US</country_code>";
    profileXMLWork += @"    </GeneralInfo>";
    profileXMLWork += @"  </Address>";
    profileXMLWork += @"</ProfileDocument>";

    // Create XML representation of Address profile (home address).
    profileXMLHome += @"<ProfileDocument>";
    profileXMLHome += @"  <Address>";
    profileXMLHome += @"    <GeneralInfo>";
    profileXMLHome += @"      <address_id>{" + guidHome + 
        "}</address_id>";
    profileXMLHome += @"      <address_name>Home</address_name>";
    profileXMLHome += @"      <address_line1>One Main Street</address_line1>";
    profileXMLHome += @"      <region_code>WA</region_code>";
    profileXMLHome += @"      <postal_code>98052</postal_code>";
    profileXMLHome += @"      <country_code>US</country_code>";
    profileXMLHome += @"    </GeneralInfo>";
    profileXMLHome += @"  </Address>";
    profileXMLHome += @"</ProfileDocument>";

    // Load the Address profile (work) into an XML document.
    profileXMLDoc.LoadXml(profileXMLWork);

    // Create the Address profile (work), converting the XML document 
    // to an XML element.
    ctxtWS.CreateProfile(profileXMLDoc.DocumentElement);

    // Load the Address profile (home) into an XML document.
    profileXMLDoc.LoadXml(profileXMLHome);

    // Create the Address profile (home), converting the XML document 
    // to an XML element.
    ctxtWS.CreateProfile(profileXMLDoc.DocumentElement);

    // Get the Profiles run-time object.
    ProfileContext ctxtRuntime = CommerceContext.Current.ProfileSystem;

    // Create a new Organization profile object.
    Profile orgProfile = ctxtRuntime.CreateProfile(guidOrg, 
        "Organization");

    // Load the first address value and name, and update the profile.
    orgProfile.Properties["GeneralInfo.address_list"].Value = guidWork;
    orgProfile.Properties["GeneralInfo.name"].Value = "MyOrganization";
    orgProfile.Update();

    // Load the second address value, and update the profile.
    orgProfile.Properties["GeneralInfo.address_list"].Value = guidHome;
    orgProfile.Update();
}
catch (Exception ex)
{
    // Failure!
    Console.WriteLine("Exception: {0}Message: {1}", ex.GetType(), 
        ex.ToString());
}

See Also

Other Resources

How to Use Multiple-Valued Profile Properties

Profiles Concepts and Tasks

Developing with the Profiles System