Search This Blog

Thursday, January 16, 2020

People and Groups : Get All People in c#




using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace SampleConsoleApp
{
    class Program
    {


        public static void Main(string[] args)
        {

            DataTable dtAllUsr = geSiteUserInfoListDetails("http://test.site.com/hr/");

        }
        public static DataTable geSiteUserInfoListDetails(string siteUrl)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Sno", typeof(string));
            dt.Columns.Add("Title", typeof(string));
            dt.Columns.Add("FullLoginName", typeof(string));
            dt.Columns.Add("LoginName", typeof(string));
            dt.Columns.Add("EMail", typeof(string));
            dt.Columns.Add("MobilePhone", typeof(string));
            dt.Columns.Add("IsSiteAdmin", typeof(string));
            dt.Columns.Add("UserInfoHidden", typeof(string));
            dt.Columns.Add("Department", typeof(string));
            dt.Columns.Add("JobTitle", typeof(string));
            dt.Columns.Add("FirstName", typeof(string));
            dt.Columns.Add("LastName", typeof(string));
            dt.Columns.Add("WorkPhone", typeof(string));
            dt.Columns.Add("Created", typeof(string));
            dt.Columns.Add("Modified", typeof(string));
            dt.Columns.Add("Deleted", typeof(string));
            dt.Columns.Add("LastModified", typeof(string));
            dt.Columns.Add("CreatedDate", typeof(string));
            dt.Columns.Add("IsActive", typeof(string));
            DataRow dr;
            ClientContext clientContext = new ClientContext(siteUrl);
            Web site = clientContext.Web;
            clientContext.Load(site);

            List l = site.SiteUserInfoList;
            clientContext.Load(l);
            clientContext.ExecuteQuery();

            CamlQuery query = new CamlQuery();
            query.ViewXml = @"<View><Query><Where>" +
                            "<IsNotNull><FieldRef Name='Title'/><Value Type='Text>" +
                            "</IsNotNull></Where></Query></View>";

            ListItemCollection items = l.GetItems(query);
            clientContext.Load(items);
            clientContext.ExecuteQuery();
            int count = 0;
            foreach (ListItem item in items)
            {
                count++;
                dr = dt.NewRow();
                dr["Sno"] = count.ToString();
                dr["Title"] = Convert.ToString(item.FieldValues["Title"]);
                dr["FullLoginName"] = Convert.ToString(item.FieldValues["Name"]);
                dr["LoginName"] = Convert.ToString(item.FieldValues["UserName"]);
                dr["EMail"] = Convert.ToString(item.FieldValues["EMail"]);
                dr["MobilePhone"] = Convert.ToString(item.FieldValues["MobilePhone"]);
                dr["IsSiteAdmin"] = Convert.ToString(item.FieldValues["IsSiteAdmin"]);
                dr["UserInfoHidden"] = Convert.ToString(item.FieldValues["UserInfoHidden"]);
                dr["Department"] = Convert.ToString(item.FieldValues["Department"]);
                dr["JobTitle"] = Convert.ToString(item.FieldValues["JobTitle"]);
                dr["FirstName"] = Convert.ToString(item.FieldValues["FirstName"]);
                dr["LastName"] = Convert.ToString(item.FieldValues["LastName"]);
                dr["WorkPhone"] = Convert.ToString(item.FieldValues["WorkPhone"]);
                dr["Created"] = Convert.ToString(item.FieldValues["Created"]);
                dr["Modified"] = Convert.ToString(item.FieldValues["Modified"]);
                dr["Deleted"] = Convert.ToString(item.FieldValues["Deleted"]);
                dr["LastModified"] = Convert.ToString(item.FieldValues["Last_x0020_Modified"]);
                dr["CreatedDate"] = Convert.ToString(item.FieldValues["Created_x0020_Date"]);
                dr["IsActive"] = Convert.ToString(item.FieldValues["IsActive"]);
                dt.Rows.Add(dr);
            }
            return dt;
        }
    }
 
}

No comments:

Post a Comment