SPUser with person and group field

來源:互聯網
上載者:User

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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.