Hi,
This is my 2nd post on this subject.
This post will get down and dirty with code samples. The first post contains some background info and links to tutorials and guides. I recommand you have a look unless you’re a no-fear-willing-to-spand-days-making-this-work like me :)
To user user profiles with FBA users, you’ll have to extend
Chapter 3 – UserProfileManager and FBA (provider issues)
First thing’s first: THERE ARE 2, YES, 2, UserProfileManager CLASSES. The first (Microsoft.SharePoint.Portal.UserProfiles) is obsolete. The second (Microsoft.Office.Server.UserProfiles) is not.
I’ll use the one in the Microsoft.Office namespace. Note there are differences between them as well as between the corresponding UserProfile classes.
User Profiles in MOSS
User profiles are a nice feature in MOSS. They allow you to store meta data about the users in your portal importing it from other sources (SharePoint 2007: Importing User Profile Information by Sahil Malik).
User information commonly stored in user profiles includes: first and last name, birthday, manager name, company, department, address etc. User profiles are stored in a SSP making them available throughout the farm.
You can add and change the user profile properties using the management interface:
Central Administration –> Shared Services Provider –> User Profiles and Properties
For more info, try Google or Live! search.
Retrieving User Profile Info
In order to retrieve user profile info, you’ll have to have the SSP up and running. If it’s not, you’ll get errors trying to user a UserProfileManager object.
- Create a new UserProfileManager object:
UserProfileManager userProfileManager = new UserProfileManager();
This will create profile manager that will work in the context of the current site and the assigned SSP.
You can retrieve user profile information from other SSPs in the farm using this function: /// <summary>
/// Creates a <see cref="UserProfileManager"/> object for a specific site collection
///
/// Note the functions contains no error handling what so ever
/// </summary>
/// <param name="siteURL">The URL of the site</param>
/// <returns>The <see cref="UserProfileManager"/> object for the site collection</returns>
public UserProfileManager GetUserProfileManagerForAnotherSite(string siteURL)
{
SPSite anotherSite = new SPSite(siteURL); // reference the other site collection
ServerContext serverContext = ServerContext.GetContext(anotherSite); // get the server context
UserProfileManager userProfileManager = new UserProfileManager(serverContext); // create the profile manager object
return userProfileManager;
}
This function will be useful for bridging the gap between a site using Windows Authentication and a site using FBA.
You could access user profile info from either site using the correct context of UserProfileManager.
-
To create a user profile, use:
UserProfile profile = userProfileManager.CreateUserProfile(); // create a profile for the current user
or
UserProfile profile = userProfileManager.CreateUserProfile("domain\\username"); // create a profile for the provided user
or
UserProfile profile = userProfileManager.CreateUserProfile("Provider name:username"); // create a profile for the provided user (FBA)
Where:
Provider name is the name of the default membership provider defined in the web.config.
Example:
<membership defaultProvider="FBADemoMember">
Username is the name of the user from the membership database.
Note that unlike domain users that use ‘\’ as a separator, FBA user require ‘:’.
- Get the user profile for the current user:
UserProfile profile = userProfileManager.GetUserProfile(false);
The user’s profile can be null if there was no profile created. GetUserProfile can create the user profile if needed when you set bCreateIfNoExist to true:
UserProfile profile = userProfileManager.GetUserProfile(true);
GetUserProfile will allow you to retrieve user profiles for user by users referenced by username:
UserProfile profile = userProfileManager.GetUserProfile("domain\\username"); // retrieve the user profile for a domain user
UserProfile profile = userProfileManager.GetUserProfile("Provider name:username"); // retrieve the user profile for a FBA user
- As you can imagine, deleting a user profile will be quite simple. I’ll skip the code sample.
- Setting values in user profiles or retriving them is as easy a using any other collection. The only challenge is using the correct property names (not display names, but internal names). To help you out, you can use PropertyConstants for the OOO stuff.
Examples:
string firstName = (string)profile[PropertyConstants.FirstName].Value;
string lastName = (string)profile[PropertyConstants.LastName].Value;
DateTime hireDate = (DateTime)profile[PropertyConstants.HireDate].Value;
string myCustomProperty = (string)profile["MyCustomPropertyName"].Value; // retrieve the value for a custom property
Choice fields have multiple values, and you can take a look at this post: Setting MOSS User Profile properties by Dan Matthews.
Note you can access user profile information using the user profile service. Here’s a nice post (InfoPath team blog) demostrating the use with InfoPath.
What’s next?
The sky’s the limit… just kidding…
Here’s a sample method I came up with demonstrating a sample use for user profiles:
/// <summary>
///Looks for an anniversary, meaning any date in the user's profile matching today's month and day
/// </summary>
/// <param name="profile">The user profile to check</param>
/// <returns>true if found, false if not</returns>
public bool DoesTheUserHaveAnythingToday(UserProfile profile)
{
bool result = false; // assume the user has nothing today
// get the profile manager
UserProfileManager userProfilem = profile.ProfileManager;
// go over all the properties looking for dates
foreach (Property prop in userProfilem.Properties)
{
// is it possbible for a prop to be null?
if (prop != null)
{
try
{
if (prop.Type == "datetime" || prop.Type == "datenoyear" || prop.Type == "date")
{
DateTime date = (DateTime)profile[prop.Name].Value;
// check if the day and month match today
if (date.Month == DateTime.Today.Month && date.Day == DateTime.Today.Day)
{
// found a date so stop looking
result == true;
break;
}
}
else
{
// this property is not a date value
}
}
catch
{
// nothing to do... move on to the next property
}
}
}
return result;
}
Disclaimer: I didn’t run this code so… :)