I'm working on a site which uses Forms Based Authentication, and I want to extend the standard user profile with some of my own custom properties.
I knew you could define a ProfileProvider in the MOSS web.config file, but the tricky part was getting and setting the values since the framework is unaware of the provider. You can't access the properties at design-time like a normal Asp.Net Web Application, and I couldn't find any documentation for accessing Profile Provider Properties as a collection like propcollection["propname"]. I've seen a couple places where people talk about creating and using custom profile provider properties in MOSS, but there was nowhere that explained how to do it.
Then, yesterday there was the post on the MOSS Team Blog about the Profile Import Tool being released on CodePlex - which is very cool in itself. I figured, CodePlex - open source; Profile Import - I bet they have code in their project that gets the properties out in the way I need since it's a dynamic process for importing profile property values. Bingo! Thank you Steve, Ron, and Lawrence!
Here's the code I used to Get/Set a custom profile property named 'UserCity' for the current user in MOSS, most of which was pulled and pieced together from the Profile Import Tool project:
Profile section in MOSS web.config:
<profile defaultProvider="MOSSPortalProfile" enabled="true" automaticSaveEnabled="true">
<providers>
<add connectionStringName="AspNetDb_ConnectionString" applicationName="MOSSPortal" name="MOSSPortalProfile" type="System.Web.Profile.SqlProfileProvider,System.Web,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
<properties>
<add name="UserCity" customProviderData="DisplayName=City;" />
</properties>
</profile>
Get/Set C# code:
//object to hold the profile properties defined in the web config
SettingsPropertyCollection profileProperties = new SettingsPropertyCollection();
//get the profile section from the web config
ProfileSection profileConfigSection = (ProfileSection)ConfigurationManager.GetSection("system.web/profile");
//get the collection of defined properties
RootProfilePropertySettingsCollection configProperties = profileConfigSection.PropertySettings;
//create collection to contains properties to pass to the profile provider
SettingsPropertyCollection propertiesToReturn = new SettingsPropertyCollection();
//populate the collection with properties from the web config
foreach (ProfilePropertySettings configProperty in configProperties)
{
SettingsProperty profileProperty = new SettingsProperty(configProperty.Name);
profileProperty.PropertyType = Type.GetType(configProperty.Type);
if (profileProperty.PropertyType == null)
{
profileProperty.PropertyType = typeof(System.String);
}
profileProperty.DefaultValue = configProperty.DefaultValue;
propertiesToReturn.Add(profileProperty);
}
//get the profile provider
ProfileProvider provider = ProfileManager.Providers["MOSSPortalProfile"];
//create a SettingsContext for the current user
SettingsContext userContext = new SettingsContext();
userContext["UserName"] = SPContext.Current.Web.CurrentUser.Name;
userContext["IsAuthenticated"] = true;
//get the values of the properties for the current user from the database
SettingsPropertyValueCollection returnVal = provider.GetPropertyValues(userContext, propertiesToReturn);
//get the value out of the collection
string contactCity = Convert.ToString(returnVal["UserCity"].PropertyValue);
if (contactCity == string.Empty)
{
contactCity = "Denver";
}
//set value
returnVal["UserCity"].PropertyValue = contactCity;
//save user profile properties back to the database
provider.SetPropertyValues(userContext, returnVal);By the way, the
SharePoint project sites on
CodePlex are great places to learn about the inner workings of WSS3/MOSS. By scouring the source code of the projects, you can extract some very cool ways to develop on the platform.