Monday, 3 December 2012

Sharepoint Object Model User Profiles



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.
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.

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
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.

Share This!


No comments:

Post a Comment

Translate

Total Pageviews

Powered By Blogger · Designed By Seo Blogger Templates