I have "User Information List" and i want to get user of current site collection only but it is fetching all users of Farm.
Below is the code.
SPSite currentSite = SPContext.Current.Site; SPWeb web =currentSite.OpenWeb();
SPUserCollection userCollection = web.SiteUsers;
SPList userList = web.Lists.TryGetList("User Information List");
if (userList != null)
{
foreach (SPListItem item in userList.Items)
{
}
}
Not sure what i am doing wrong ! Any thoughts on this ??
Loading
Karthik Muthu KaruppanPosted Apr 28, 2015, 1:01 PM
You can use a powershell script to get all the users under a particular site collection. For this you need to loop through all the webs under a particular site collection and get the users details out of it. The below piece of code can help you to get the user details under a particular site collection,
if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
Add-PSSnapin Microsoft.SharePoint.Powershell
}
$SiteCollURL = read-host "Enter the site collection URL "
$siteColl = get-spsite $SiteCollURL
foreach($web in $sitecoll.allwebs)
{
write-host "Users under the web "$web.url -fore cyan
foreach($user in $web.siteusers)
{
write-host "User: " $User.loginname -fore green
}
}
write-host ""
write-host "SCRIPT COMPLETED" -fore green