Al final de esta demostración nos daremos cuenta que al extender este control podemos realizar búsquedas sobre diferentes objetos y/o diferentes fuentes de datos, porque este control no está casado con objetos de tipo SPUser como se podría pensar en primera instancia.
Sin más, pasemos al código.Para poder extender la funcionalidad de este control, basicamente tenemos que heredar de tres clases: EntityEditorWithPicker, PickerDialog y SimpleQueryControl.
Dejemos que el código se explique solo :)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint.WebControls; using System.Collections; using Microsoft.SharePoint; namespace Blog.WebControls { public class CustomPeoplePicker : EntityEditorWithPicker { protected bool m_onInitCalled; protected string m_commaSeparatedAccountsDelayedUntilOnInit; public CustomPeoplePicker() { } protected override void OnInit(EventArgs e) { base.OnInit(e); //definimos el tipo del PickerDialog this.PickerDialogType = typeof(CustomPickerDialog); this.m_onInitCalled = true; if (!string.IsNullOrEmpty(this.m_commaSeparatedAccountsDelayedUntilOnInit)) { base.UpdateEntities(this.GetAccountsEntities(this.m_commaSeparatedAccountsDelayedUntilOnInit)); this.m_commaSeparatedAccountsDelayedUntilOnInit = null; } } ////// Devuleve la lista de usuarios seleccionados en forma de cadena /// /// ///private string GetResolvedEntityAccounts() { if (this.ResolvedEntities == null) { return string.Empty; } if (this.ResolvedEntities.Count == 0) { return string.Empty; } List names = new List (); string list = string.Empty; foreach (PickerEntity entity in this.ResolvedEntities.OfType ()) { names.Add(entity.Key); } list = string.Join(this.EntitySeparator.ToString(), names.ToArray()); return list; } protected ArrayList GetAccountsEntities(string commaSeparatedAccounts) { ArrayList list = new ArrayList(); if (commaSeparatedAccounts != null) { foreach (string str in commaSeparatedAccounts.Split(new char[] { this.EntitySeparator }, StringSplitOptions.RemoveEmptyEntries)) { string str2 = str.Trim(); if (!string.IsNullOrEmpty(str2)) { PickerEntity entity = new PickerEntity(); entity.Key = str2; list.Add(entity); } } } return list; } /// /// Valida las entidades /// /// ///public override PickerEntity ValidateEntity(PickerEntity needsValidation) { needsValidation.IsResolved = false; ; CustomQueryControl c = new CustomQueryControl(); foreach (CustomUser user in c.GetPeople()) { if (needsValidation.Key.Equals(user.EmployeeNumber, StringComparison.InvariantCultureIgnoreCase)) { needsValidation.IsResolved = true; } } return needsValidation; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint.WebControls; using System.Collections; namespace Blog.WebControls { public class CustomPickerDialog : PickerDialog { public CustomPickerDialog() : base(new CustomQueryControl(), new TableResultControl(), new CustomPeoplePicker()) { ArrayList columnDisplayNames = ((TableResultControl)base.ResultControl).ColumnDisplayNames; columnDisplayNames.Clear(); columnDisplayNames.Add("Employee Number"); columnDisplayNames.Add("Email"); columnDisplayNames.Add("Name"); ArrayList columnNames = ((TableResultControl)base.ResultControl).ColumnNames; columnNames.Clear(); columnNames.Add("EmployeeNumber"); columnNames.Add("Email"); columnNames.Add("Name"); ArrayList columnWidths = ((TableResultControl)base.ResultControl).ColumnWidths; columnWidths.Clear(); columnWidths.Add("20%"); columnWidths.Add("30%"); columnWidths.Add("50%"); } protected override void OnLoad(EventArgs e) { CustomQueryControl control = (CustomQueryControl)this.QueryControl; base.OnLoad(e); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint.WebControls; using System.Data; namespace Blog.WebControls { public class CustomQueryControl : SimpleQueryControl { public CustomQueryControl() { Load += CustomQueryControl_Load; } void CustomQueryControl_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { EnsureChildControls(); mColumnList.Items.Add("EmployeeNumber"); mColumnList.Items.Add("Name"); mColumnList.Items.Add("Email"); } } protected override int IssueQuery(string search, string groupName, int pageIndex, int pageSize) { CustomUser[] usersAux = this.GetPeople(); if (usersAux == null) return 0; ListallPeople = new List (usersAux); if (allPeople.Count == 0) return 0; List searchPeople = new List (); DataTable resultTable = null; searchPeople = allPeople; if (groupName == "Name" && !string.IsNullOrEmpty(search)) { searchPeople = allPeople.Where(x => SearchString.Match(x.Name, search)).ToList(); } else if (groupName == "Email" && !string.IsNullOrEmpty(search)) { searchPeople = allPeople.Where(x => SearchString.Match(x.Email, search)).ToList(); } resultTable = GetPeopleTable(searchPeople.ToArray()); PickerDialog.Results = resultTable; PickerDialog.ResultControl.PageSize = resultTable.Rows.Count; return resultTable.Rows.Count; } private DataTable GetPeopleTable(CustomUser[] people) { DataTable dummyTable = new DataTable(); dummyTable.Columns.Add("EmployeeNumber"); dummyTable.Columns.Add("Name"); dummyTable.Columns.Add("Email"); foreach (CustomUser person in people) { DataRow row1 = dummyTable.NewRow(); row1["Name"] = person.Name; row1["EmployeeNumber"] = person.EmployeeNumber; row1["Email"] = person.Email; dummyTable.Rows.Add(row1); } return dummyTable; } string _excludedUsers; public string ExcludedUsers { get { return this._excludedUsers; } set { this._excludedUsers = value; } } public virtual CustomUser[] GetPeople() { List people = new List (); //agregar toda la logica necesaria para obtener los usuarios people.Add(new CustomUser() { EmployeeNumber = "11111", Email = "juan.topo@contoso.com", Name ="Juan G. Topo" }); people.Add(new CustomUser() { EmployeeNumber = "22222", Email = "max.power@contoso.com", Name = "Max Power" }); people.Add(new CustomUser() { EmployeeNumber = "33333", Email = "bambi.venado@contoso.com", Name = "Venado Bambi" }); people.Add(new CustomUser() { EmployeeNumber = "44444", Email = "homero.simpson@contoso.com", Name = "Homero J. Simpson" }); return people.ToArray(); } public override PickerEntity GetEntity(DataRow dr) { PickerEntity entity = new PickerEntity(); entity.DisplayText = dr["Name"].ToString(); entity.Key = dr["EmployeeNumber"].ToString(); entity.Description = dr["Email"].ToString(); entity.IsResolved = true; return entity; } } }
Happy Coding!
1 comentarios:
Excelente artículo y lógica impecable, efectivamente, el código habla por sí mismo.
Saludos
Publicar un comentario