Printer Friendly Version      Send     
Click to Rate and Give Feedback
SPUser Class (Microsoft.SharePoint)
Represents a user in Microsoft Windows SharePoint Services.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
Public NotInheritable Class SPUser
    Inherits SPPrincipal
Visual Basic (Usage)
Dim instance As SPUser
C#
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)] 
public sealed class SPUser : SPPrincipal

Use the AllUsers or SiteUsers property of the SPWeb class, the GetAllAuthenticatedUsers method of the SPUtility class, or the GetUniqueUsers method of the SPAlertCollection class, to return the following:

  • All the users of a site

  • All the users in the site collection

  • All authenticated users of a site

  • A list of users for a collection of alerts.

Otherwise, use the Users property of the SPGroup, SPRole, or SPWeb class to return the users in a group, role definition, or site.

Use an indexer to return a single user from the collection. For example, if the collection is assigned to a variable named collUsers, use collUsers[index] in C#, or collUsers(index) in Visual Basic, where index is either the index number of the user in the collection or the user name of the user.

Every user has a unique member ID (ID property), has the permissions associated with that membership, and can be represented by an SPMember object. The following example assigns a user to an SPMember object, given a specified SharePoint Web site:

Visual Basic
Dim oWebsite As SPWeb = SPContext.Current.Web
Dim oMember As SPMember = oWebsite.AllUsers("Domain\\User_Alias")
C#
SPWeb oWebsite = SPContext.Current.Web;
SPMember oMember = oWebsite.AllUsers["Domain\\User_Alias"];

For general information about users and security, see Security, Users, and Groups.

The following code example modifies the e-mail address, display name, and notes for a specified user object.

Visual Basic
Dim siteCollection As SPSite = SPControl.GetContextSite(Context)
Dim site As SPWeb = siteCollection.AllWebs("Site_Name")
Dim user As SPUser = site.AllUsers("User_Name")

user.Email = "E-mail_Address"
user.Name = "Display_Name"
user.Notes = "User_Notes"

user.Update()
C#
SPSite oSiteCollection = SPContext.Current.Site;
using (SPWeb oWebsite = oSiteCollection.AllWebs["Website_Name"])
{
    SPUser oUser = oWebsite.AllUsers["User_Name"];

    oUser.Email = " E-mail_Address";
    oUser.Name = " Display_Name";
    oUser.Notes = " User_Notes";

    oUser.Update();
}
NoteNote:

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Best Practices: Using Disposable Windows SharePoint Services Objects.

System.Object
   Microsoft.SharePoint.SPMember
     Microsoft.SharePoint.SPPrincipal
      Microsoft.SharePoint.SPUser
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker