Using the User Profile Store

Many assets and services in the Partner Portal application, such as the Partner Collaboration site collection and the pricing service, rely on the association between a partner ID and a user profile to ensure that partners see only the information that is intended for them. The Partner Portal application stores the partner ID in the SharePoint user profile store. When a new partner wants to use the application, you must create a profile for the user and enter the appropriate value in the PartnerId field. The Partner Portal installation script creates and populates several user profiles. The following code shows how to add new property to the user profile store.

ServerContext serverContext = ServerContext.GetContext(spSite);
UserProfileManager userProfileManager = new UserProfileManager(serverContext);

// Add the PartnerId profile property.
try
{
    Property property = userProfileManager.Properties.Create(false);
    property.Name = "PartnerId";
    property.DisplayName = "Partner Id";
    property.Type = PropertyDataType.String;
    property.Length = 50;

    userProfileManager.Properties.Add(property);
    
}
catch(DuplicateEntryException ex)
{
    // Catch the exception and do nothing if the PartnerId profile property
    // is already created.
}

The following code creates a new user profile for a specific user account and partner ID.

private static void CreateUserProfileAndPartnerId(UserProfileManager userProfileManager, string accountName, string partnerId)
{
    UserProfile userProfile =   
        userProfileManager.CreateUserProfile(accountName);
    userProfile["PartnerId"].Value = partnerId;
    userProfile.Commit();
}

An instance of the PartnerSiteDirectory class reads the PartnerId property from the user's profile. The following code shows the GetPartnerIdFromUserProfile method of the PartnerSiteDirectory class.

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
private string GetPartnerIdFromUserProfile(string accountName)
{
    UserProfile userProfile = null;
    ServerContext serverContext = ServerContext.GetContext(SPContext.Current.Site);
    UserProfileManager userProfileManager = new UserProfileManager(serverContext);

    if (!userProfileManager.UserExists(accountName))
    {
        ThrowPartnerIdNotInUserProfileException(null);
    }

    userProfile = userProfileManager.GetUserProfile(accountName);
    if (userProfile["PartnerId"] == null)
    {
        ThrowPartnerIdNotInUserProfileException(null);
    }
    return userProfile["PartnerId"].Value.ToString();
} 

Home page on MSDN | Community site