SPUser with person and group field

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.

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值