SharePoint users and groups

Source: Internet
Author: User
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 limit strate this point.
Assigning permissions directly to users is usually not a scalable and maintainable solution, especially specified SS large specified ISES with specified 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 handle 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 wocould return the group collection of the sales site. this wocould be a subset of the groups available in the site collection, which is available through the site. sitegroups property. for example, the following code wocould 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 wocould 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 ();


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.