Using SharePoint PeopleEditor user control

I had the chance to play with PeopleEditor user control on my previous project and just thought would be good to put another blog about it out there, so here you go:

Started with creating a visual web part and the next step is adding the control to the ascx page

<SharePoint:PeopleEditor ID="peUser" ValidatorEnabled="true" runat="server"
                        SelectionSet="User" Width="350px" IsValid="true" AllowTypeIn="true"
                        MultiSelect="true"></SharePoint:PeopleEditor>
<SharePoint:InputFormRequiredFieldValidator ID="rfvUser" 
                        SetFocusOnError="true" runat="server" ControlToValidate="peUser" BreakBefore="true"
                        ErrorMessage="Required field." EnableClientScript="false"
                        Display="Dynamic"></SharePoint:InputFormRequiredFieldValidator>

To get the selected users we can iterate through its resovedEntities property

for (index = 0; index <= peControl.ResolvedEntities.Count - 1; ++index)
{
    PickerEntity entity = (PickerEntity)peopleEdControl.ResolvedEntities[index];
    SelectedUser selectedUser = new SelectedUser(){ Id = Convert.ToInt32(entity.EntityData["SPUserID"])
                                                    , Name = entity.DisplayText
                                                    , LoginName = entity.Key };
    selectedUserList.Add(selectedUser);
}

To assign a value to PeopleEditor user control (in my case I use a string of login name separated by a semicolon)

string[] delimiter = { ";" };
string[] loginNames = loginNameString.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);

foreach(string loginName in loginNames)
{
     PickerEntity pe = new PickerEntity();
     pe.Key = loginName;

     pe = peUser.ValidateEntity(pe);
     ArrayList entityList = new ArrayList() { pe };

     peUser.UpdateEntities(entityList);
}

Hope that helps 🙂

Advertisements

Convert a string or an integer to enum type

Something that I always use whenever I use enumeration :

public enum StatusType
	{
		Approved = 1,
		Pending = 2,
		Cancelled = 3
	}

Convert a string to enum :

string currentStatus = "Approved";
StatusType status = (StatusType) Enum.Parse(typeof(StatusType), currentStatus);

Convert an int to enum :

int currentStatusId = 1;
StatusType type = (StatusType) Enum.ToObject(typeof(StatusType), currentStatusId); 

Just be careful with enum conversion, especially when you want to use Enum.IsDefined method as it is very expensive.

Asynchronous processing in ASP.Net using delegate

There are times when we need to get the control of the UI after telling the server to do something, i.e. Bulk upload.  The example below uses delegate to return the control after extracting a large file:

public delegate string ExtractFile(Stream fs);

public void Run(Stream fs)
{
ExtractFile ef = new ExtractFile(ProcessFile);
ef.BeginInvoke(fs, new AsyncCallback(ProcessFileEnd), ef);
}
private string ProcessFile(Stream fileStream)
{
string message = DoSomething();

return message;
}

void ProcessFileEnd(IAsyncResult result)
{
ExtractFile ret = result.AsyncState as ExtractFile;

string message = ret.EndInvoke(result);
}

Notice that I use AsyncCallback when calling the beginInvoke. This can be used if we want to do something after the long task. However I haven’t found a good way to update the UI in ASP.Net -_-