Introduction
In this article I am going to explain you that How to use User Profiles and profile properties in Sharepoint Object model.
In SharePoint 2007, a userid is uniquely identified by his/her username. The username is Tied to the membershipprovider that the site is configured to authenticate against. A user can also have other information like phone number, email etc.
Even if business require then they can add custom property like Origin, Race etc.
Even if business require then they can add custom property like Origin, Race etc.
Detail:-
First, your code must have references to the Microsoft.SharePoint, Microsoft.Office.Server, and System.Web Assemblies. With those references available, you can use the SPSite class and the UserProfileManager class as entry points into the user profile subsystem within MOSS.
As per my opinion; there are few below important points while using user profiles programmatically:
1. Retrieving User Profiles
2. Read Profile Property
3. Modify User Profile
4. Create a User Profile
5. Create a User Profile property
2. Read Profile Property
3. Modify User Profile
4. Create a User Profile
5. Create a User Profile property
The Code:-
Main class I am going to explain in only 1st point. In rest other point I will only highlight code block which is related to respective point.
1. Retrieving User Profiles
We can get user profile object by using GetUserProfile method of UserProfileManager.
We can get user profile object by using GetUserProfile method of UserProfileManager.
2. Read Profile Property
We can read property using UserProfileManager.Properties
We can read property using UserProfileManager.Properties
foreach (Property prop in profileManager.Properties) { Console.WriteLine("t{0} : {1}", prop.DisplayName, profile[prop.Name].Value); }
3. Modify User Profile
profile["Origin"] = "India"; //Make sure that you call the Commit method //when you are finished making changes to the profile profile.Commit();
4. Create a User Profile
UserProfileManager profileManager = new UserProfileManager(context); string acId = @"domainavinash"; if (!profileManager.UserExists(acId)) { UserProfile profile = profileManager.CreateUserProfile(acId); if (profile == null) Console.WriteLine("ould not create user profile, an error occurred."); else Console.WriteLine("User profile has been created."); } else Console.WriteLine("User profile for this account " + acId + " already exists.");
5. Create user Profile Property
PropertyCollection pc = profileManager.Properties; Property p = pc.Create(false); p.Name = "TestProperty"; p.DisplayName = "Test Property"; p.Type = "String"; p.Length = 50; p.PrivacyPolicy = PrivacyPolicy.OptIn; p.DefaultPrivacy = Privacy.Organization; pc.Add(p);
Reference:-
Thanks
Nikhil S.
No comments:
Post a Comment