User Profiles in SharePoint Object Model
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 membership provider 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.
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
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | using System; using System.Collections.Generic; using System.Text; using Microsoft.Office.Server; using Microsoft.Office.Server.Administration; using Microsoft.Office.Server.UserProfiles; using Microsoft.SharePoint; using System.Web; namespace UserProfilesConsoleApp { class Program { static void Main(string[] args) { using ( SPSite site = new SPSite("site url")) { ServerContext context = ServerContext.GetContext(site); UserProfileManager profileManager = new UserProfileManager(context); // you can also get current user from web object. // syntex: SPWeb.CurrentUser UserProfile profile = profileManager.GetUserProfile(@"domainuser"); Console.WriteLine("Profile {0}", profile.MultiloginAccounts[0]); // you got user profile object. now you can use any property of it. // like profile.ID, profile.PersonalSite.RootWeb.Titl e, profile.PersonalSite.Url etc.. } } } } |
2. Read Profile Property
We can read property using UserProfileManager.Properties
1 2 3 4 5 | foreach (Property prop in profileManager.Properties) { Console.WriteLine("t{0} : {1}", prop.DisplayName, profile[prop.Name].Value); } |
3. Modify User Profile
1 2 3 4 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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
1 2 3 4 5 6 7 8 9 | 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:-
http://office.microsoft.com/en-us/sharepoint-server-help/about-user-profiles-HA001160317.aspx
Thanks!
Avinash
March 28, 2012
В·
Infoyen В·
8 Comments
Tags: Accessing User Profile properties, Create/Manage User Profile Properties, Get SharePoint User Profile of Specific User, Getting User Profile Properties from SharePoint SSP, How to add custom user profile properties, How to get UserProfile information programmatically, How to: Retrieve User Profile Properties, MOSS, Programmatically create User Profile, Programmatically editing the user profile, Programmatically Reading User Profile, Read/writing to user profile programmatically, Set User Profile properties programmatically, SharePoint, userprofilemanager.getuserprofile В· Posted in: MOSS, Security, SharePoint
8 Responses
Can you help me?
I need to retrieve user ad attributes (such as login name,dept, division…) in sharepoint 2010, what is the easy way to do this task and how?
I need to take these values and post this to a asp.net application (using a url)
thank you
Neel
I think you can use SPWeb.EnsureUser which will return SPUser object. Then in debug mode see the all properties of SPUser object.
I hope you will get the required results.
Avinash,
Thank you.
Now is the issue, assume, i get all the values I need, I can assign them to varaible and next step is to post this value to another url using submit button
Can i submit values to different url which is not on the same server, using a submit button on the webpart.
Basically after the code, based on the ad values, i need to show reports based on their ad values
Good to know that your one issue is solved.
Your another issue is how to pass values to another url?
Yes, You can submit values to different url which is not on the same server, using a submit button on the webpart.
In your case i think it would be good to pass query parameter with url.
Please refer below url:-
http://sharepoint.infoyen.com/2012/03/13/pass-page-values-from-one-to-another-page-in-asp-net/
Regards,
Avinash
I have sharepoint 2010 site with fba only enabled.
How will the UserProfile created and how to map the account name to the membership provider? And how will sharepoint validates the accountname.(There is no sharepoint user profile synchronization service enabled)
Membership provider is not AD. It is external LDAP server.
Thank you.
Murali
This article explains that how to do various operation for user profile in sharepoint object model.
But your issue is different. Your issue is how to synchronize external LDAP server user roles in sharepoint.
This is not the scope of this article.
However following articles may help you:-
http://technet.microsoft.com/en-us/library/ee806890%28v=office.15%29.aspx
http://technet.microsoft.com/en-us/library/ee806882.aspx
http://social.technet.microsoft.com/Forums/en-US/sharepoint2010setup/thread/45da3932-49f8-476d-a07a-9fcd27e891c0
http://technet.microsoft.com/en-us/library/cc262350%28v=office.15%29.aspx#planfba
http://blogs.msdn.com/b/tehnoonr/archive/2010/11/21/how-to-setup-user-profile-synchronization-between-sharepoint-2010-and-sun-ldap.aspx
http://www.codeproject.com/Tips/382312/SharePoint-2010-Form-Based-Authentication
Hi, I appreciate the blog. very helpfull. I was just creating an instance of a userprofilemanager and i get an error in the same line
UserProfileManager myuserprofilemanager = new UserProfileManager(mycontext, true, true);
Error is –
“UserProfileApplicationNotAvailableException_Logging :: UserProfileApplicationProxy.ApplicationProperties ProfilePropertyCache does not have 0d3993c7-9fe0-46b6-ae17-241e6d502bd3”
Any help is highly appreciated.
Nice to know that this post help you.
About your error: It seems that its related to access. Please verify. It might help.
Leave a Reply