Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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 !!!

share|improve this question
    
I'm confused...What is ID? What users are you trying to get? Any user with permissions on each list item? –  Tim Gabrhel Sep 20 '11 at 17:48
    
Noops when i fetch collection from a Person or Group type column Using sharepoint entity model it will retun List<int?>UserIds –  Umbrella Corporation Sep 21 '11 at 7:54
add comment

2 Answers

up vote 3 down vote accepted

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
}
share|improve this answer
add comment

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
}
share|improve this answer
    
Thanks for your reply ... but want to ask is there any other better option - i got a suggestion to read XML of web.SiteUsers . i want to know which one is better approach –  Umbrella Corporation Sep 21 '11 at 7:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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