Question

i am stuck to an issue where i have a List ID - list of Ids i have to find the LoginName of SPUser or SPGroup with the id in the ListID

i tried the following but it threws exception as it did not find user when group id is there

              foreach (var id in ID)
                {
                    SPUser spUser = web.SiteUsers.GetByID(Convert.ToInt32(id));
                    if (spUser != null)
                    {
                        lstUsers.Add(spUser.LoginName);
                         continue;
                    }
                    SPGroup spGroup = web.SiteGroups.GetByID(Convert.ToInt32(id ));
                    if (spGroup != null)
                    {
                        lstGroups.Add(spGroup.LoginName);
                         continue;

                    }

                }

please suggest what to do !!!

Was it helpful?

Solution

I would suggest two changes.

  1. Use SPFieldUserValue. It doesn't throw an exception if user doesn't exist.
  2. Use the SPGroupCollection.GetCollection(int[] ids)

    SPUserCollection users = web.SiteUsers;
    SPGroupCollection groups = web.SiteGroups;
    foreach (var id in ID)
    {
      // try to get user-name
      SPUser spUser = new SPFieldUserValue(web, Convert.ToInt32(id), null).User;
      if (spUser != null)
      {
        lstUsers.Add(spUser.LoginName);
        continue;
      }
      // try to get group-name
      var foundGroups = groups.GetCollection(new int[] { Convert.ToInt32(id) });
      if (foundGroups.Count > 0) 
      {
        lstGroups.Add(foundGroups[0]);
        continue;
      }
      // If execution reaches this point: Nothing found with this id
    }
    

OTHER TIPS

If i read your question correct: You have list called ID in which you have numeric ids (user and group ids) and you want to add the name of the user or group corresponding to the id to an other list (called lstUsers or lstGroups).

The exception is to be expected if the id being handled is the id of a group. The function web.SiteUsers.GetById() throws an exception when the given id is not found and that is what you try first. Try wrapping it in a try-catch-block.

Bonus: store SiteUsers and SiteGroups in a variable, to avoid getting this infomation over and over again from the database in each iteration of the loop.

SPUserCollection users = web.SiteUsers;
SPGroupCollection groups = web.SiteGroups;
foreach (var id in ID)
{
  // try to get user-name
  try{
    SPUser spUser = users.GetByID(Convert.ToInt32(id));
    if (spUser != null)
    {
      lstUsers.Add(spUser.LoginName);
      continue;
    }
  }catch(Exception){
     //  no user found with id.
  }

  // try to get group-name
  try {
    SPGroup spGroup = groups.GetByID(Convert.ToInt32(id ));
    if (spGroup != null)
    {
      lstGroups.Add(spGroup.LoginName);
      continue;
    }
  }catch(Exception){
     // no group found with id
  }

  // If execution reaches this point: Nothing found with this id
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top