First, it should be explained that the user profile setup differs depending on whether you are operating in a full MOSS environment, or simply using the basic WSS structure. In the full MOSS environment, user profile information is stored in a central database, and profile information is shared across site collections that may be hosted on your server. This profile info can be read and/or edited via the UserProfileManager class within the Microsoft.Office.Server dll.
Read a little deeper about Profiles administration in more in one of my previous posts
http://panvega.wordpress.com/2008/11/05/mapping-user-properties-in-the-sp-central-administration
As mentioned above, this profile information is held in a global profile database, and persists across different site collections on your server. On the other hand, if you are operating in only a WSS environment, the user profile information is stored directly in a hidden list specific to an individual site collection, which is called the User Information List. If multiple site collections exist, the server will store separate user profiles for each one. These profiles can be access via the following:
using(Microsoft.Sharepoint.SPSite site = new Microsoft.Sharepoint.SPSite("http://url")){ Microsoft.Sharepoint.SPWeb web = site.RootWeb; Microsoft.SharePoint.SPList userList = web.Lists["User Information List"]; foreach (Microsoft.SharePoint.SPListItem user in userList.Items) { Console.WriteLine(user["ID"] + ": " + user["Name"]); } }Before you begin screaming that this second method does not make use of the SPUser class, you should note that the SPUser class provides access to fewer properties than those that are available by accessing this user list directly. However, for sake of completeness (and for those of you who shy away from tinkering under the hood), a list of all SPUser objects associated with the site collection can be obtained via the following:
using(Microsoft.Sharepoint.SPSite site = new Microsoft.SharePoint.SPSite("http://url")){ Microsoft.SharePoint.SPWeb web = site.RootWeb; foreach(Microsoft.SharePoint.SPUser user in web.Users){ System.Console.WriteLine(user.ID + ": " + user.Name); } }As I stated above, the profile properties available from the SPUser class are fewer than those available from the User Information List. The SPUser class allows you to access/change the following profile properties (e.g. those that show up in the My Settings page):
By comparison, the User Information List provides access to the following profile properties (plus many more that I’m not listing here):
- ID
- Name
- LoginName
- Notes
- DisplayName
- PrincipalType…
As you can see, these different methods of accessing user profile information can be a bit confusing. A good first step in tackling this problem is to look at the default layout of your website. If you have a link for My Site at the top, near your name, then you are in a full MOSS environment, and should access user profiles via the first method described. If this link does not exist, you may be only using WSS profiles, in which case either of the other methods will be your preferred mode of access.http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.propertyconstants_members.aspx
- Title
- Name
- Notes
- SipAddress
- Locale
- IsSiteAdmin
- Picture
- Department
- JobTitle
- IsActive
- FirstName
- LastName
- WorkPhone
- UserName
- WebSite
- Office
- ID
- Modified
- Created
- Author
- Editor
CodeSnippet, Accessing the WSS Profile of the SP MySite for the PeopleEditor:
protected PeopleEditor editor;…..
ArrayList peEntities = editor.Entities;
// 2. cast object Entities in PickerEntity class
PickerEntity pickEn = (PickerEntity)peEntities[i];
if (pickEn.IsResolved)
{
Hashtable hstEntityData = pickEn.EntityData;
builder.AppendLine(pickEn.DisplayText);
builder.AppendLine(pickEn.Description);
builder.AppendLine(Convert.ToString(hstEntityData["DisplayName"]));
builder.AppendLine(Convert.ToString(hstEntityData["Email"]));
builder.AppendLine(Convert.ToString(hstEntityData["PrincipalType"]))
In this Code above you can only access the basic properties.
How to retreive all Profile Properties with the SPUserProfileManager and store them in a drop down list:
;
SPSite site = SPContext.Current.Site;
ServerContext context = ServerContext.GetContext(site);
UserProfileManager profileManager = new UserProfileManager(context);
PropertyCollection props = profileManager.Properties;
DropDownList ddl = new DropDownList();
foreach (Property prop in props)
{
ddl.Items.Add(prop.Name);
}
Important SP Objects:
- SPUserInfo
- SPUserProfileManager
- PickerEntity
- UserProfile
- PeopleEditor
for (int i = 0; i < peoplePicker.ResolvedEntities.Count; i++)
{
PickerEntity pickerEntity = (PickerEntity)peoplePicker.ResolvedEntities[i];
SPUserInfo userInfo = new SPUserInfo();
userInfo.LoginName = pickerEntity.Key;
if (isGroup(web, userInfo))
{
// Loop through all members of this group and add them individually
foreach (SPUser usr in web.Groups[userInfo.LoginName].Users)
{
// Add the User
if (profileManager.UserExists(usr.LoginName))
{
UserProfile user = profileManager.GetUserProfile(usr.LoginName);
givenName = user[PropertyConstants.FirstName];
lastName = user[PropertyConstants.LastName];
email = user[PropertyConstants.WorkEmail];
}
}
}
else
{
// pickerEntity relates to an individual user
// Add the User
if (profileManager.UserExists(userInfo.LoginName))
{
UserProfile user = profileManager.GetUserProfile(userInfo.LoginName);
givenName = user[PropertyConstants.FirstName];
lastName = user[PropertyConstants.LastName];
email = user[PropertyConstants.WorkEmail];
}
}
}
Note:
Actually you can retreive all Users and Groups in the entire Active Directory forest when using the PeoplePicker Addressbook. However when you use the profileManager.UserExists(usr.LoginName), the Profilemanager looks only into the MOSS UserProfile store. Otherwise you have to import all users from the entire forest or make a custom specific AD User import.
Or access the properties without the PropertyConstants:
UserProfile user = profileManager.GetUserProfile(userInfo.LoginName);
string givenName = user[FirstName].Value.toString();
More Information:
http://www.codeplex.com/MOSSProfileImport
http://blogs.technet.com/tothesharepoint/archive/2008/10/21/3139351.aspx
No comments:
Post a Comment