Fetch all users to a custom list using event receiver in SharePoint

This SharePoint event receiver tutorial explains, how to retrieve all SharePoint users into a custom list using the event receiver feature in SharePoint 2010.

Fetch all users to custom list using event receiver in SharePoint

First, create an Empty SharePoint project using visual studio 2010.
Now add the custom list definition for fetching the user in your project.
In the solution explorer, find the feature, if missing create a feature.
Click this feature and Add Event Receiver. See in fig

Fetch all users to custom list using event receiver in SharePoint

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
{
}
}

Now Save and deploy the solution. 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 users to custom list using event receiver in SharePoint 2010

You may like following SharePoint tutorials:

This tutorial, we learned how to fetch all users to the custom list using an event receiver in SharePoint 2010.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>