Fetch all SharePoint user into custom list using event receiver feature in SharePoint Server 2010


In this post, we will discuss how we can retrieve all SharePoint users into a custom list using event receiver feature in SharePoint 2010.

Also you can check out:

Create remote event receiver sharepoint online as provider hosted add-in and deploy into Microsoft Azure step by step tutorial

SharePoint workflow examples: Collect Feedback Workflow

Make non editable field in list using PowerShell in SharePoint 2010

1. First create Empty SharePoint project using visual studio 2010.

2. Now add the custom list definition for fetching the user in your project.

3. In solution explorer find the feature, if missing create feature.

4. Click this feature and Add Event Receiver. See in fig

Fetch all SharePoint user into custom list using event receiver feature in SharePoint Server 2010
Fetch all SharePoint user into custom list using event receiver feature in SharePoint Server 2010

5. Now open the Feature2.EventReceiver.cs file and remove the comment from FeatureActivated method and under this method add the following code.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite currentsite;
SPWeb rootweb;
SPList CustomUserInformationList;
SPListItem newItem;
SPUserCollection userCollection;
try
{
currentsite = (SPSite)properties.Feature.Parent;
rootweb = currentsite.RootWeb;
rootweb.AllowUnsafeUpdates = true;
userCollection = rootweb.AllUsers;

if (userCollection.Count > 0)
{
CustomUserInformationList = rootweb.Lists[“UserInformation Lst”];

//UserInformation Lst is a custom list where we show the all user

foreach (SPUser user in userCollection)
{
newItem = CustomUserInformationList.Items.Add();
newItem[“Title”] = user.ToString();
// Title is the column of UserInformation Lst
newItem.Update();
}
}

rootweb.AllowUnsafeUpdates = false;

}
Catch
{
}
}

6. Now Save and deploy the solution.


7. Now you go you SharePoint site and click your custom list and see the all user listed in your SharePoint. See in fig

 Fetch all SharePoint user into custom list using event receiver feature in SharePoint Server 2016
Fetch all SharePoint user into custom list using event receiver feature in SharePoint Server 2016
Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Navneet Singh

I am Navneet Singh, Working at C3it software solutions pvt. ltd. in Pune. My core skills are SharePoint 2013, Office 365, SharePoint 2010, SharePoint Designer, SharePoint Designer Workflow, Nintex Workflow, Infopath, Webpart, CSOM/SSOM, Power-shell, C#, ASP.NET and SQL Server.

View all posts by Navneet Singh →