Microsoft.Office.Server.Use ...


UserProfile Class (Microsoft.Office.Server.UserProfiles)
Represents a user profile for a person in the user profile database.

Namespace: Microsoft.Office.Server.UserProfiles
Assembly: Microsoft.Office.Server (in microsoft.office.server.dll)
Syntax

Visual Basic (Declaration)
<DefaultMemberAttribute("Item")> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel:=True)> _
Public Class UserProfile
Visual Basic (Usage)
Dim instance As UserProfile
C#
[DefaultMemberAttribute("Item")] 
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)] 
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel=true)] 
public class UserProfile
Example

The following code examples show how to use the UserProfile class to create a user profile and personal site.

Visual Basic
Public Sub UserProfileSample() 
   'get portal site context from topology          
   Dim strUrl As String = "http://SampleName"          
   Dim tm As New TopologyManager()          
   Dim ps As PortalSite = tm.PortalSites(New Uri(strUrl))          
   Dim pc As PortalContext = PortalApplication.GetContext(ps)
   'initialize user profile config manager object          
   Dim upm As New UserProfileManager(pc)          
   'create user sample          
   Dim sAccount As String = "mydomain\myalias"          
   If Not upm.UserExists(sAccount) Then
      upm.CreateUserProfile(sAccount)
   End If           
   'to set prop values on user profile          
   Dim u As UserProfile = upm.GetUserProfile(sAccount)          
   Dim sPropName As String = "PreferredName"          
   u(sPropName) = sAccount          
   u.Commit()           
   'remove user profile sample          
   upm.RemoveUserProfile(sAccount)       
End Sub 

Public Sub CreatePersonalSiteSample()          
   'get portal site context from topology          
   Dim strUrl As String = "http://SampleName"          
   Dim tm As New TopologyManager()          
   Dim ps As PortalSite = tm.PortalSites(New Uri(strUrl))          
   Dim pc As PortalContext = PortalApplication.GetContext(ps)
   'initialize user profile config manager object          
   Dim upm As New UserProfileManager(pc)          
   Dim sAccount As String = "mydomain\myalias"          
   Dim u As UserProfile = upm.GetUserProfile(sAccount)
   u.CreatePersonalSite()          
   Dim mysite As SPSite = u.PersonalSite          
   Dim myurl As String = u.PersonalUrl       
End Sub 'CreatePersonalSiteSample
C#
public void UserProfileSample() 
{ 
   //get portal site context from topology string 
   strUrl = "http://SampleName"; 
   TopologyManager tm = new TopologyManager(); 
   PortalSite ps = tm.PortalSites[new Uri(strUrl)]; 
   PortalContext pc = PortalApplication.GetContext(ps);  

   //initialize user profile config manager 
   object UserProfileManager upm = new UserProfileManager(pc); 

   //create user sample 
   string sAccount = "mydomain\\myalias"; 
   if (!upm.UserExists(sAccount)) upm.CreateUserProfile(sAccount);  

   //to set prop values on user profile UserProfile 
   u = upm.GetUserProfile(sAccount); 
   string sPropName = "PreferredName"; u[sPropName] = sAccount;
   u.Commit();  

   //remove user profile sample 
   upm.RemoveUserProfile(sAccount); 
}  

public void CreatePersonalSiteSample() 
{ 
   //get portal site context from topology 
   string strUrl = "http://SampleName"; 
   TopologyManager tm = new TopologyManager(); 
   PortalSite ps = tm.PortalSites[new Uri(strUrl)]; 
   PortalContext pc = PortalApplication.GetContext(ps);  
   //initialize user profile config manager object 
   UserProfileManager upm = new UserProfileManager(pc); 
   string sAccount = "mydomain\\myalias"; 
   UserProfile u = upm.GetUserProfile(sAccount);
   u.CreatePersonalSite(); 
   SPSite mysite = u.PersonalSite; 
   string myurl = u.PersonalUrl; 
}
Inheritance Hierarchy

System.Object
  Microsoft.Office.Server.UserProfiles.UserProfile
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also

Tags :


Community Content

AndersR
Property lookup
the class TopologyManager is for SharePoint Server 2003. So the code is outdated for MOSS 2007.

just to make the point, when you try to look for a property
say :

anyControl.Text = userProfile["Company"].ToString();

you must first check if that property exist
you could not rely on : userProfile["Company"] != null
you must check also userProfile["Company"].Value != null

A good way to check for null that wont throw an exception:

if

(userProfile.ProfileManager.Properties.GetPropertyByName(value) == null)
Tags : property

Thomas Lee
Correct code for MOSS 2007
As indicated above, the code is not compatible with MOSS 2007 (for a few reasons including TopologyManager)...
the correct code is (in C#):

string strUrl = "http://SampleName";
using (Microsoft.SharePoint.SPSite site = new SPSite(strUrl)
{
Microsoft.Office.Server.ServerContext sc = Microsoft.Office.Server.ServerContext.GetContext(site);
Microsoft.Office.Server.UserProfiles.UserProfileManager upm = new Microsoft.Office.Server.UserProfileManager(sc);

[remainder of code]

}
Tags : contentbug

Page view tracker