Thursday, August 21, 2008

Adding PeopleEditor control in webpart

Hi All,

I wonder sometimes how this people editor works, Because once you just place the people editor control in web part and then it automatically synchronize the Active Directory if web application is configured as Windows Authentication or from Profile database if Web application is configured as Form Based Authentication. you do not need to do any programming for that. it is really a wonderful thing and i really liked this approach.

I faced several challenges while implementing this control in web part. The requirement is like this. we are not supposed to use a default web parts provided in NewForm, EditForm and DispForm, so we removed those web part and placed our own data entry web part having many validations and pop up windows and many code related stuff that would not be possible with the default web parts.

So but obvious that i have to use people editor control in my web part and use it for getting users from AD and then to store it in the List. Now, because we use our own web part, we ourselves have to write a logic to insert the data entered in data entry web part in the list. so we have to code for this control as well.

so i do it like this, first i add the Field in the list and select set the column to People or Group and select the show Field property as Name(with Presence).

First add the Directive at the top of the page

<%@ Register TagPrefix="wssawc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


And now you can add the control for peopleeditor anywhere in your webpart. Just place this tag.

<wssawc:PeopleEditor id="peopleContact"
AllowEmpty="true"
ValidatorEnabled="false"
MultiSelect="false"
runat="server"
SelectionSet="User"
width='200px'
Height='25px' />


You can set different properties depending upon your requirements.

Once you have done this, add a reference for this control in your supporting cs file for the web part (ASCX control).

Do not forget to add reference for : Microsoft.SharePoint.WebControls

Add this line

PeopleEditor peopleContact;


Add this line to your CreateChildControl method and before Page.IspostBack condition

peopleContact = (PeopleEditor)this.ctrlCategoryUserControl.FindControl("{controlnameinascx}");


And finally at the time of inserting the data in the list, just perform the following steps.

SPUser objUser = SPContext.Current.Web.Users[peopleContact.CommaSeparatedAccounts];
String strUserToInsert = objUser.ID + ";#" + objUser.Name;


This is the way you will get the user, if peopleeditor's Multi Select True Or False, this will require modifying this code a bit only.

This will do the insertion for this.

At the time of displaying or editing the same value in Editform Webpart and DispForm Webpart (Your custom made, not the default one that comes with moss), you have to perform the following steps.

I assume that you have fetched a ListItem record in table and now that value for peopleeditor from the field of that List is stored in "UserToDisplay" variable. I also assume that you have performed the same steps as mentioned above for NewForm.

then, just set the following.

peopleContact.CommaSeparatedAccounts = UserToDisplay;


This will display the same User in peopleeditor control.

That's it. Your job is done.

1 comment:

gitolekha said...

I have a tab control in which I have a peopleeditor in the first tab called "pplowner". The last tab is a summary of values of controls selected in the other tabs. I have to do this using javascript. currently, I am doing,

var elem = document.forms[0].elements;
for(var i=0; i < elem.length; i++)
{
if(elem[i].id.indexOf("pplOwner") != -1 && elem[i].id.indexOf("TextBox") != -1) users = elem[i].value;
}

Now, I display the value in users in a label.
The poblem that I am facing is,
if I delete a value from the peopleeditor's textbox, I still get the old values in users.
Also, the names are displayed as domain/userid, whereas I want it as user's name as it is shown in the peopleeditor.
Can you suggest a solution?




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com