SPUser with person and group field

来源:互联网 发布:淘宝儿童卡通墙贴 编辑:程序博客网 时间:2024/06/04 18:09

Use the AllUsers property of theSPWeb class to return all the users of a site. This includes users granted permissions directly, users granted permissions through a group who have then visited the site, and users who have been referenced in a person field, such as being assigned a task. CallingAllUsers[name] will throw an exception if the user is not there.

Use the SiteUsers property of theSPWeb class to return all the users in the site collection.

Use the GetAllAuthenticatedUsers method of theSPUtility class to return all authenticated users of a site.

Use the GetUniqueUsers method of theSPAlertCollection class to return a list of users for a collection of alerts.

Otherwise, use the Users property of theSPGroup orSPWeb class to return the users in a group or site.

Use an indexer to return a single user from the collection. For example, if the collection is assigned to a variable namedcollUsers, usecollUsers[index] in C#, orcollUsers(index) in Visual Basic, whereindex 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 anSPMember object. The following example assigns a user to anSPMember object, given a specified SharePoint Web site:

 

SPWeb oWebsite = SPContext.Current.Web;SPMember oMember = oWebsite.AllUsers["Domain\\User_Alias"];

 


SPSite oSiteCollection = SPContext.Current.Site;using (SPWeb oWebsite = oSiteCollection.AllWebs["Website_Name"]){    SPUser oUser = oWebsite.AllUsers["User_Name"];//It is Id    oUser.Email = " E-mail_Address";    oUser.Name = " Display_Name";    oUser.Notes = " User_Notes";    oUser.Update();}
 
Try using the EnsureUser() function of the SPWeb object.SPWeb web = SPContext.Current.Web;SPUser user = web.EnsureUser(@"domain\username");
 

The following code example uses the SiteUsers property to return the collection of users for the current site collection and the user display names

This example requires using directives (Imports in Microsoft Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

using (SPWeb oWebsite = SPContext.Current.Site.OpenWeb("Website_URL")){    SPUserCollection collUsers = oWebsite.SiteUsers;    foreach (SPUser oUser in collUsers)    {        Response.Write(SPEncode.HtmlEncode(oUser.Name) + "<BR>");    }}

Use the Groups property of the SPUser or SPWeb class to return the collection of groups for the user or Web site. Otherwise, use the OwnedGroups property of the SPUser class to return the collection of groups owned by a user, or the SiteGroups property of the SPWeb class to return all the groups in the site collection.

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

Every group can be represented by an SPMember object and has a unique member identifier (see ID property). The following example assigns a group to an SPMember object:

SPMember oMember = oWebsite.SiteGroups["Cross-Site_Group_Name"];

For general information about groups and security, see Authorization, users, groups, and the object model in SharePoint 2013.

Examples

The following code example changes the name, owner, and description of a group in a site collection.

using (SPWeb oWebsite = SPContext.Current.Site.RootWeb){    SPGroup oGroup = oWebsite.SiteGroups["Original_Name"];    oGroup.Name = "New_Name";    oGroup.Owner = oWebsite.Users["Domain_Name\\User"];    oGroup.Description = "Description";    oGroup.Update();}
 
 

Using SharePoint object model we can pull out all the cross-site groups either by using SPWeb.Groups or by using SPWeb.SiteGroups. Then what is the difference between the two?

SPWeb.Groups will allow you to pull out only the groups which have some / any kind of permissions defined within the site.

SPWeb.SiteGroups will pull out all the cross-site groups irrespective of any permission defined.

So, the next time you try to pull out the Groups using SPWeb.Groups, you will not get astonished by not finding few of the Groups within the retrieved collection.

 
 
Note: The same also applies to the cross-site user collection retrieval using either SPWeb.Users or SPWeb.SiteUsers.
原创粉丝点击