internal static Dictionary <int, string> DistinctLookupValues(this SPField field) { var res = new Dictionary <int, string>(); SPSecurity.RunWithElevatedPrivileges( () => { var siteId = field.ParentList.ParentWeb.Site.ID; string connectionString; using (var site = new SPSite(siteId)) { connectionString = site.ContentDatabase.DatabaseConnectionString; } var listId = field.ParentList.ID; var colName = field.AttributeValue("ColName"); var sourceListId = field.SourceListId(); using (var cmd = new SqlCommand { CommandType = CommandType.Text }) { cmd.CommandText = FieldDistinctLookupValues.Replace("%SqlColName%", colName); cmd.Parameters.Add(new SqlParameter("@ListId", listId)); cmd.Parameters.Add(new SqlParameter("@SourceListId", sourceListId)); using (var con = new SqlConnection(connectionString)) { cmd.Connection = con; con.Open(); var reader = cmd.ExecuteReader(); while (reader != null && reader.Read()) { if (!res.ContainsKey((int)reader[0])) { res.Add((int)reader[0], (string)reader[1]); } } } if (cmd.Connection.State != ConnectionState.Closed) { cmd.Connection.Close(); } } }); return(res); }