Find User from LookUp Field

 This code snippet is how to get user from the Lookup field here I am
leaving upto SPUser object.

 

objItem is List Item which has user/group LookUp Field

SPFieldLookupValue UserLookUPValue = new SPFieldLookupValue(objItem[“YourLookUPFieldName”].ToString());

 if (((SPFieldUserValueCollection)objItem[[“YOURLOOKUPFIELDNAME”]).Count > 0)

 {

     while (intUserCount < ((SPFieldUserValueCollection)objItem[“YOURLOOKUPFIELDNAME”]).Count)

     {

         if (((SPFieldUserValueCollection)objItem[“YOURLOOKUPFIELDNAME”])[intUserCount].User != null)

         {

             //If field contain user name

             SPUser objUser = ((SPFieldUserValueCollection)objItem[“YOURLOOKUPFIELDNAME”])[intUserCount].User;

            

                  //Here you can write your Business Logic 

 

         }

         else

         {

             //If field contain group name

             string strGroupName = ((SPFieldUserValueCollection)objItem[“YOURLOOKUPFIELDNAME”])[intUserCount].LookupValue;

             try

             {

                 SPGroup objGroup = objSPWeb.SiteGroups[strGroupName];

                 if (objGroup.Users.Count > 0)

                 {

                     SPUserCollection objUsers = objGroup.Users;

                     foreach (SPUser objuser in objUsers)

                     {

                  //Here you can write your Business Logic 

                     }

                 }

             }

             catch(Exception Ex)

             {

             }

         }

         intUserCount++;

     }

 }

 

Happy coding  🙂

Advertisements

AD User Group in Sharepoint

An AD user by name domainName\ADUserName added in your SharePoint site in Site Owner group.
Now you change the user name in AD ,this change will not reflect in Sharepoint.

On reimport of user profile will update the User Profile Database . Sharepoint group uses UserInfo Table
STSADM -o migrateuser -oldlogin domainName\ADUserName -newlogin domainName\ADNewUserName -ignoresidhistory

This command will migrate the user but we need to check the display User Name.

Its better delete and add the User in Site Collection