1

How to bind all the users of Sharepoint(office 365) except system users like :

"System Account",

"NT AUTHORITY\authenticated users",

"_SPOCacheFull",

"YLO001_spocrwl_142_11094".

I want only custom added users like :

[email protected]

[email protected]

Currently I am binding all users like this:


using (SPSite spSite = SPContext.Current.Site)
{
    using (SPWeb web = spSite.OpenWeb())
    {
       web.AllowUnsafeUpdates = true;

       SPUserCollection users = web.AllUsers;
       foreach (SPUser user in users)
       {
          listAllUsers.Items.Add(new ListItem(user.Name, user.LoginName)); // listAllUsers is Listbox
       }
       web.AllowUnsafeUpdates = false;
    }  
}

2
  • Easiest option is by checking if users has a valid email address. Dec 8, 2014 at 14:04
  • Ronak Patel: inside the foreach look add a condition if(!string.IsNullOrEmpty(user.Email)) then only do listAllUsers.Items.Add Dec 8, 2014 at 14:12

2 Answers 2

1

The following example demonstrates how to distinguish a "regular" users in Web.SiteUsers:

public static IEnumerable<User> LoadUsers(ClientContext context)
{
   var users = context.LoadQuery(context.Web.SiteUsers.Where(u => u.PrincipalType == PrincipalType.User && u.UserId.NameIdIssuer == "urn:federation:microsoftonline"));
   context.ExecuteQuery();
   return users;
}

where:

0

Try this

ClientContext clientContext = new ClientContext("http://your site/");
Web web = clientContext.Web;
List siteUserInfoList = web.SiteUserInfoList;
CamlQuery query = new CamlQuery();
query.ViewXml = "";
IEnumerable<ListItem> itemColl = clientContext.LoadQuery(siteUserInfoList.GetItems(query));
clientContext.ExecuteQuery();
foreach (var item in itemColl)
{
    if(item["EMail"] != null && item["EMail"] != '') 
    {
        Console.WriteLine("ID:{0}  Email:{1} Title:{2}",item.Id,item["EMail"],item["Title"]);
    }
}

Get all Users with Info(LoginName,Id,Email,Title) in SharePoint 2010 Client Object Model C#

1
  • I have checked the Email now..but still getting this user: YLO001_spocrwl_142_11094 Dec 9, 2014 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.