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

InfoPath alternatives for form designing SharePoint

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:

– How to create event receivers using Visual Studio 2010 in SharePoint 2010?

– Create workflow using SharePoint designer 2013 in SharePoint 2013

– 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

Similar SharePoint 2013 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 →

Leave a Reply