Click to Rate and Give Feedback

  Switch on low bandwidth view
Community Content
In this section
Statistics Annotations (2)
UserProfileManager Class (Microsoft.Office.Server.UserProfiles)
A collection of UserProfile objects used to access user profile data. To access a specific user profile, call the UserProfileManager class to create a UserProfile object and retrieve the corresponding data from the user profile database.

Namespace: Microsoft.Office.Server.UserProfiles
Assembly: Microsoft.Office.Server (in microsoft.office.server.dll)
Visual Basic (Declaration)
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel:=True)> _
<SharePointPermissionAttribute(SecurityAction.Demand, ObjectModel:=True)> _
Public Class UserProfileManager
    Implements IEnumerable
Visual Basic (Usage)
Dim instance As UserProfileManager
C#
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel=true)] 
[SharePointPermissionAttribute(SecurityAction.Demand, ObjectModel=true)] 
public class UserProfileManager : IEnumerable

You must create the UserProfileManager object before accessing a UserProfile object. You can then retrieve the user profiles to which the user has access. Full access to everyone's user profile requires "Manage User Profiles" rights. Full access to your own profile requires "Use Personal Features" rights. Everyone has read access to all profiles

You must use the UserProfileConfigManager class object instead of the UserProfileManager object to manage metadata. The metadata access provided by the UserProfileManager object is meant to be read-only.

The following code example shows the use of the UserProfileManager class.

[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 'UserProfileSample


      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;
}
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Updated Code Sample      Steve Peschka   |   Edit   |   Show History

The code samples provided here are outdated for MOSS; you should not use the TopologyManager, PortalSite or PortalContext classes. Instead, you can get the appropriate context with an SPSite object. For example, this Visual Basic code can replace the code above to get the "portal site context":

Dim theSite As SPSite

Tags What's this?: Add a tag
Flag as ContentBug
Updated C# Example      Brian Culver   |   Edit   |   Show History
As speschka has pointed out, TopologyManager, PortalSite and PortalContext are obsolete classes (from SharePoint 2003). Instead, you should use ServerContext when invoking the UserProfileManager. Here is a useful snippet to achieve this:
using Microsoft.Office.Server;
...

using (SPSite site = new SPSite("http://servername")) {
ServerContext svrContext = ServerContext.GetContext(site);
UserProfileManager userProfileManager = new UserProfileManager(svrContext); ... do something here ...
}
Flag as ContentBug
Example code is incorrect      Backlash78   |   Edit   |   Show History

This code (from the example) won't even compile, because the property indexer is read-only:

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


I suggest reading this MSDN article for good examples of how to use this class:

http://msdn.microsoft.com/en-us/library/cc973103.aspx

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker