Pregunta

I am trying to Get multiple users from user field in page load, and display loginName and navigate url in a hyperlinks in a repeater, but only the last user in the list are displayed?

in ascx page i have this code

 <asp:Repeater ID="RepeaterLinks" runat="server">
           <ItemTemplate >
   <asp:HyperLink ID="MyLinks" runat="server" Text='<%# Eval("LoginName") %>' NavigateUrl='<%# Eval("NavigateUrl") %>'></asp:HyperLink>
</ItemTemplate>
       </asp:Repeater>

In ascx.cs i have this

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

     foreach (SPFieldUserValue value in userField8)
     {
       string script = "<script language='javascript'>alert('" + value.User.ToString() + "')</script>";
         Page.ClientScript.RegisterClientScriptBlock(GetType(), "Register", script);

         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))
          {
              var profile8 = profileManager8.GetUserProfile(spuserField.LoginName);
               string personUrl8 = profile8.PersonalSite.Url;

                string userNames = userItem8["Title"].ToString();

                DataTable dt = new DataTable();
                dt.Columns.Add("LoginName", Type.GetType("System.String"));
                    dt.Columns.Add("NavigateUrl", Type.GetType("System.String"));
                    DataRow row = dt.NewRow();
                    row["LoginName"] = userNames;
                    row["NavigateUrl"] = personUrl8;
                    dt.Rows.Add(row);

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

                }

            }
¿Fue útil?

Solución

The problem in your code is you are creating data table inside user loop of each user that's why it showing only last user. Correct code should be

    Guid fieldID8 = item.Fields.GetFieldByInternalName("Field8").Id;
string valueField8 = item[fieldID8].ToString();
SPFieldUserValueCollection userField8 = (SPFieldUserValueCollection)item.Fields[fieldID8].GetFieldValue(valueField8);
DataTable dt = new DataTable();
dt.Columns.Add("LoginName", Type.GetType("System.String"));
dt.Columns.Add("NavigateUrl", Type.GetType("System.String"));
 foreach (SPFieldUserValue value in userField8)
 {
   string script = "<script language='javascript'>alert('" + value.User.ToString() + "')</script>";
     Page.ClientScript.RegisterClientScriptBlock(GetType(), "Register", script);

     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))
      {
          var profile8 = profileManager8.GetUserProfile(spuserField.LoginName);
           string personUrl8 = profile8.PersonalSite.Url;

            string userNames = userItem8["Title"].ToString();


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



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

I hope your issue will be resolved using above code.

Please mark it as answer if it will solve your issue

Licenciado bajo: CC-BY-SA con atribución
scroll top