Objective:In this article, I will show you how to fetch all the user name of a SharePoint site collection using program or Visual Studio.Step1:Create a SharePoint site and add few users in that by configuring the site in SharePoint. I have created a site called "Document Center" and added few users in that. The site with user is as below image.Step2:Create a new project in Visual studio. Select any type for the project. I am choosing the project type as a Console Application. So to create new console application project, select New->Project->Console Application. Step 3:Add the assemblies (dll) required to access SharePoint site. To add assemblies click on Reference in solution Explorer then selects Add Reference and adds below assemblies. Now you would able to see,Microsoft.SharePoint dll in reference list of your project. Step 4:Add the namespaceusing Microsoft.SharePoint;Step 5: Create Instance of SPSite SPSite spsite = new SPSite("http://adfsaccount:2222/");I am passing root site URL in constructor. We will get all the sites inside this site collection specified by URL.So now spsite is a site collection. Step 6:Take collection of sites in site collection using SPWebSPWeb web= spsite.RootWeb;Step 7:Take all the users in SPUserCollectionSPUserCollection userlist= web.AllUsers;So, the entire code to fetch the user name in Console application is as follows Program.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.SharePoint;
namespace ConsoleApplication1{ class Program { static void Main(string[] args) {
SPSite spsite = new SPSite("http://adfsaccount:2222/"); SPWeb web= spsite.RootWeb; SPUserCollection userlist= web.AllUsers; foreach (SPUser u in userlist) { Console.WriteLine(u.Name); } Console.Read(); } }}OutputConclusion:In this article, I showed how to fetch the entire user name programmatically from a SharePoint site collection. Thanks for reading.Happy Coding
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: