質問

リストIDがある問題に固執しています - IDSのリストspuserまたはspgroupのログニネームを見つけなければなりません。

私は次のことを試しましたが、グループIDがそこにあるときにユーザーが見つからなかったので、それは例外を投げました

              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;

                    }

                }

何をすべきかを提案してください!!!

役に立ちましたか?

解決

2つの変更をお勧めします。

  1. 使用する SPFieldUserValue. 。ユーザーが存在しない場合、例外は投げかけません。
  2. 使用 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
    }
    

他のヒント

私があなたの質問を正しく読んだら正しい:あなたは呼ばれるリストを持っています ID 数値ID(ユーザーおよびグループID)があり、IDに対応するユーザーまたはグループの名前を他のリスト(LSTUSERSまたはLSTGROUPSと呼ばれる)を追加する必要があります。

例外は、処理されているIDがグループのIDである場合に予想されます。関数 web.SiteUsers.GetById() 指定されたIDが見つからない場合、例外をスローし、それが最初に試してみるものです。 aでラップしてみてください try-catch-ブロック。

ボーナス: Loopの各反復でデータベースから何度も何度もこのインフォメーションを取得しないように、SiteUSERSとSiteGroupsを変数に保存します。

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
}
ライセンス: CC-BY-SA帰属
所属していません sharepoint.stackexchange
scroll top