it-support-at
asked on
How I retrieve a Sharepoint group with C#
How can I get / retrieve all members of a specific Sharepoint group with C#?
For example I have the Sharepoint group "Visitors" and I need all members of it respectively their e-mail adresses.
For example I have the Sharepoint group "Visitors" and I need all members of it respectively their e-mail adresses.
ASKER
I want only the e-mail adresses of the a sharepoint group, what are the steps I have to follow?
use following code
SPSite site = new SPSite("Yousharepoint site here");
SPWeb web = site.RootWeb;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
// code using the web object from outside the delegate to get the members of a group.
});
SPSite site = new SPSite("Yousharepoint site here");
SPWeb web = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedSite = new SPSite(web.Site.ID))
{
using(SPWeb elevatedWeb = elevatedSite.OpenWeb(web.Url))
{
// get all group members here
}
}
});
ASKER
And how I return the users or only the e-mail adress with this code?
try this example
public List<NameValuePair> GetGroupMembers(string siteUrl, string groupName)
{
List<NameValuePair> groupMembers = new List<NameValuePair>();
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
NameValuePair groupMember;
SPGroup group = web.Groups[groupName];
foreach (SPUser user in group.Users)
{
groupMember = new NameValuePair();
groupMember.Value = groupMember.Name = user.Name;
groupMembers.Add(groupMember);
}
}
}
return groupMembers;
}
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Open in new window
https://www.c-sharpcorner.com/blogs/get-group-users-using-csom1