0

i have a list were i can add users or group to my field, I have know if u select users in people column then i can get the name and mysite url to the user, but if i select a group in people column,then i want to get the users of that group and display name and mysite url here is my code for geting users from multi field in list

                    Guid fieldID8 = item.Fields.GetFieldByInternalName("MyField").Id;
                                    string valueField8 = item[fieldID8].ToString();
                                    SPFieldUserValueCollection userField8 = (SPFieldUserValueCollection)item.Fields[fieldID8].GetFieldValue(valueField8);


                                    DataTable dt = new DataTable();
                                    dt.Columns.Add("ImageUrl", Type.GetType("System.String"));
                                    dt.Columns.Add("LoginName", Type.GetType("System.String"));
                                    dt.Columns.Add("NavigateUrl", Type.GetType("System.String"));

                                    foreach (SPFieldUserValue value in userField8)
                                    {

                                        SPUser spuserField = SPContext.Current.Web.EnsureUser(value.User.ToString());

                                        SPList userInformationList8 = SPContext.Current.Web.SiteUserInfoList;
                                        SPListItem userItem8 = userInformationList8.Items.GetItemById(spuserField.ID);
                                        SPServiceContext contextSp = SPServiceContext.GetContext(siteCollection);
                                        UserProfileManager profileManager8 = new UserProfileManager(contextSp);


                                        if (profileManager8.UserExists(spuserField.LoginName))
                                        {
                                            DataRow row = dt.NewRow();
                                            var profile8 = profileManager8.GetUserProfile(spuserField.LoginName);

                                            string personUrl8 = profile8.PersonalSite.Url;
                                            string userNames = userItem8["Title"].ToString();
                    string pictureURL = userItem8["Picture"].ToString();


                                            row["ImageUrl"] = pictureURL;
                                            row["LoginName"] = userNames;
                                            row["NavigateUrl"] = personUrl8;
                                            dt.Rows.Add(row);

                                        }





                                        RepeaterLinks.DataSource = dt;
                                        RepeaterLinks.DataBind();


                                    }
1

Below is your answer

Guid fieldID8 = item.Fields.GetFieldByInternalName("MyField").Id;
string valueField8 = item[fieldID8].ToString();
SPFieldUserValueCollection userField8 = (SPFieldUserValueCollection)item.Fields[fieldID8].GetFieldValue(valueField8);
DataTable dt = new DataTable();
dt.Columns.Add("ImageUrl", Type.GetType("System.String"));
dt.Columns.Add("LoginName", Type.GetType("System.String"));
dt.Columns.Add("NavigateUrl", Type.GetType("System.String"));

foreach (SPFieldUserValue value in userField8)
{

if(value.User != null) // Value is User
{
    SPUser spuserField = SPContext.Current.Web.EnsureUser(value.User.ToString());
    SPList userInformationList8 = SPContext.Current.Web.SiteUserInfoList;
    SPListItem userItem8 = userInformationList8.Items.GetItemById(spuserField.ID);
    SPServiceContext contextSp = SPServiceContext.GetContext(siteCollection);
    UserProfileManager profileManager8 = new UserProfileManager(contextSp);
    if (profileManager8.UserExists(spuserField.LoginName))
    {
        DataRow row = dt.NewRow();
        var profile8 = profileManager8.GetUserProfile(spuserField.LoginName);    
        string personUrl8 = profile8.PersonalSite.Url;
        string userNames = userItem8["Title"].ToString();
        string pictureURL = userItem8["Picture"].ToString();
        row["ImageUrl"] = pictureURL;
        row["LoginName"] = userNames;
        row["NavigateUrl"] = personUrl8;
        dt.Rows.Add(row);
    }
}
else // Value is Group
{
    SPGroup group = [YourWeb].SiteGroups[user.LookupValue];
    foreach(SPUser spuserField in group.Users)
    {       
        SPList userInformationList8 = SPContext.Current.Web.SiteUserInfoList;
        SPListItem userItem8 = userInformationList8.Items.GetItemById(spuserField.ID);
        SPServiceContext contextSp = SPServiceContext.GetContext(siteCollection);
        UserProfileManager profileManager8 = new UserProfileManager(contextSp);
        if (profileManager8.UserExists(spuserField.LoginName))
        {
            DataRow row = dt.NewRow();
            var profile8 = profileManager8.GetUserProfile(spuserField.LoginName);    
            string personUrl8 = profile8.PersonalSite.Url;
            string userNames = userItem8["Title"].ToString();
            string pictureURL = userItem8["Picture"].ToString();
            row["ImageUrl"] = pictureURL;
            row["LoginName"] = userNames;
            row["NavigateUrl"] = personUrl8;
            dt.Rows.Add(row);
        }
    }   
}
}
RepeaterLinks.DataSource = dt;
RepeaterLinks.DataBind();

Hope it will help to you.

  • I get object reference not set? – Batistuta Jun 5 '15 at 9:37
  • in which line you are getting this exception? – Kalpesh Vaghela Jun 5 '15 at 9:38
  • I am getting Specified argument was out of the range of valid values – Batistuta Jun 5 '15 at 9:54
  • you can debug the code and get exact link which throws that exception – Kalpesh Vaghela Jun 5 '15 at 9:56
  • Yes i know but i cant because i have other this in my projekt that i dont wont to deploy, – Batistuta Jun 5 '15 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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