共用方式為


UserProfileManager Class

Represents a collection of UserProfile objects that are used to access user profile data. To access a specific user profile, call the UserProfileManager class to create a UserProfile object and then retrieve the corresponding data from the user profile database.

Inheritance Hierarchy

System.Object
  Microsoft.Office.Server.UserProfiles.ProfileManagerBase
    Microsoft.Office.Server.UserProfiles.UserProfileManager

Namespace:  Microsoft.Office.Server.UserProfiles
Assembly:  Microsoft.Office.Server.UserProfiles (in Microsoft.Office.Server.UserProfiles.dll)

Syntax

'Declaration
<SharePointPermissionAttribute(SecurityAction.Demand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
Public Class UserProfileManager _
    Inherits ProfileManagerBase
'Usage
Dim instance As UserProfileManager
[SharePointPermissionAttribute(SecurityAction.Demand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public class UserProfileManager : ProfileManagerBase

Remarks

You must create the UserProfileManager object before you access a UserProfile object. You can then retrieve the user profiles that the user can 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 that the UserProfileManager object provides is meant to be read-only. Only the users who are listed in the Permissions ACL of the User Profile Service Application can create an instance of a UserProfileManager object.

Examples

The following code example uses the UserProfileManager class.

[Visual Basic]

      Public Sub UserProfileSample()
         'get current service context
         Dim strUrl As String = "http://SampleName"
         Dim site as SPSite = new SPSite(strUrl)
         Dim serviceContext As SPServiceContext = SPServiceContext.GetContext(site)

         'initialize user profile config manager object
         Dim upm As New UserProfileManager(serviceContext)
         '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 current service context
         Dim serviceContext As SPServiceContext = SPServiceContext.Current

         'initialize user profile config manager object
         Dim upm As New UserProfileManager(serviceContext)
         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 current service context
strUrl = "http://SampleName";
SPSite site = new SPSite(strUrl);
SPServiceContext serviceContext = SPServiceContext.GetContext(site);

//initialize user profile config manager object
UserProfileManager upm = new UserProfileManager(serviceContext);
//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 current service context
SPServiceContext serviceContext = SPServiceContext.Current;

//initialize user profile config manager object
UserProfileManager upm = new UserProfileManager(serviceContext);
string sAccount = "mydomain\\myalias";
UserProfile u = upm.GetUserProfile(sAccount);
u.CreatePersonalSite();
SPSite mysite = u.PersonalSite;
string myurl = u.PersonalUrl;
}

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

Reference

UserProfileManager Members

Microsoft.Office.Server.UserProfiles Namespace