SharePoint Users and Groups API
The WSS object model tracks user identities by using the SPUser class. If you want to access the SPUser object for the current user, you use the CurrentUser property of the SPWeb object associated with the current site. The following simple example shows you how to access some of the properties available through the SPUser class.
SPUser currentUser = SPContext.Current.Web.CurrentUser;
string userName = currentUser.Name;
string userLogin = currentUser.LoginName;
string userEmail = currentUser.Email;
The current user is always the user who was authenticated when the SPSite site collection object was created. If your code is running in the WSS Web site context, this is the user who authenticated to either WSS or the ASP.NET authentication provider. If your code is running in the context of a console application, the current user is the user whose Windows principal was used to create the initial SPSite reference. You cannot switch the security context of the site collection or its objects after it is created; it is always the user principal who first accessed the site collection that is the current user. We will look at elevation of privilege, delegation, and impersonation later in this chapter to further illustrate this point.
Assigning permissions directly to users is usually not a scalable and maintainable solution, especially across large enterprises with many users and sites. Besides the maintenance issues, as ACLs grow larger, they can bog down performance of WSS. This is not an issue unique to WSS, for it is the same issue solved by Active Directory users and groups.
WSS supports the creation of groups within a site collection to ease the configuration of authorization and access control. Groups are never created in the context of the site-they are always created in the context of the site collection and assigned to a site. For example, assume that we have a site located at /homeo/drugs, and that the /homeo/drugs site reference is the current context returned from SPContext.Current.Web. Given this environment, site.Groups would return the group collection of the sales site. This would be a subset of the groups available in the site collection, which is available through the site.SiteGroups property. For example, the following code would return the groups Team Site Members, Team Site Owners, and Team Site Visitors.
SPSite siteCollection = new SPSite("http://www.beyondweblogs.com/homeo/drugs/");
SPWeb site = siteCollection.OpenWeb();foreach(SPGroup group in site.Groups){
Console.WriteLine(group.Name);
}
Groups cannot directly be added to a site-they must be added to the site collection. If you try to add a group to the site's Groups collection, you get an exception stating, "You cannot add a group directly to the Groups collection. You can add a group to the SiteGroups collection." This situation occurs because the SPGroup is always created at the Site Collection level and assigned to the site. The following code is valid and adds the HomeoSecurityGroup to the site collection groups.
// Adds a new group to the site collection groups:
site.SiteGroups.Add("HomeoSecurityGroup", site.CurrentUser,
site.CurrentUser, "A group to manage homeo Security");
However, this still does not associate the group with our site, nor would it be useful within the site without any permissions. To add the group to the site, create a new SPRoleAssignment by associating an SPRoleDefinition with the SPGroup, and then add that role assignment to the site, as in the following code sample:
SPGroup secGroup = site.SiteGroups["HomeoSecurityGroup"];
SPRoleAssignment roleAssignment = new SPRoleAssignment(secGroup);
SPRoleDefinition roleDefinition = site.RoleDefinitions["Full Control"];
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
site.RoleAssignments.Add(roleAssignment);
How to retrieve User Profile info in Sharepoint using C# code API
You can use following code snippet to retrieve User Profile info in Sharepoint using C# code:
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint.Portal.Topology;
using Microsoft.Office.Server;
ServerContext context = ServerContext.GetContext(SPContext.Current.Site);
UserProfileManager profileManager = new UserProfileManager(context);
Microsoft.Office.Server.UserProfiles.PropertyCollection props = profileManager.Properties;
foreach ( Property prop in props){
Response.Write(prop.Name + "<br/>" );
}
ServerContext context2 = ServerContext.GetContext(SPContext.Current.Site);
UserProfileManager profileManager2 = new UserProfileManager(context2);
foreach (UserProfile profile in profileManager2)
{
UserProfileValueCollection FirstNameProp = profile["AccountName"];
Response.Write(FirstNameProp[0].ToString() + "<br/>");
}
Update User Profile in Sharepoint Programmatically
In Sharepoint, there is a special list ‘User Information List’ for user’s profile information storage. This list is quite handly to play around with user profile data. When a user is added to the Sharepoint site, a list item is automatically created in this list. To update the user’s information in ‘User Information List’, for instance, to update the user’s photo, I am using following code and it works fine:
SPSite siteCollection = null;
SPWeb web = null;siteCollection =
SPContext .Current.Site;web = siteCollection.RootWeb;
web.AllowUnsafeUpdates = true ;Microsoft.SharePoint.SPList list = web.Lists["User Information List"];Microsoft.SharePoint.
SPUser u = web.SiteUsers[ @"sharepoint\waqas" ];Microsoft.SharePoint.SPListItem it = list.Items.GetItemById(u.ID);it[
"Picture" ] = "http://www.beyondweblogs.com/personal/trothman/Shared%20Pictures/Profile%20Pictures/waqas.jpg" ;it.Update();