Sunday, December 9, 2007

Checkbox in ListViewWebpart

When we see the checkbox to select multiple user in People and Groups page, every (new) sharepoint developers wonders how it can be implemented to their custom list. First we will look at how it is implemented in the People and Groups (people.aspx) Page.


The people.aspx page host the UserInfo list as in the following line

<SharePoint:ListView id="UserListView" ListID="UserInfo" runat="server"/>

and the schema for the users list is defined in the schema.xml file in the <12hive>\TEMPLATE\GLOBAL\Lists\USERS folder. The checkbox is implemented as a Computed type column as follows.

<Field ID="{625e82b6-fb2b-4c37-9a45-ba7541f8d139}" Type="Computed" ReadOnly="TRUE" Name="UserSelection" DisplayName="$Resources:userinfo_schema_selectionbox;" Sortable="FALSE" Filterable="FALSE" EnableLookup="FALSE" HeaderImage="unchecka.gif" ClassInfo="Icon" AuthoringInfo="$Resources:userinfo_schema_selectionbox_editinfo;" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="UserSelection">
<FieldRefs>
<FieldRef Name="ID" />
<FieldRef Name="Name" />
<FieldRef Name="EMail" />
<FieldRef Name="SipAddress" />
<FieldRef Name="Title" />
</FieldRefs>
<DisplayPattern><HTML><![CDATA[<input type="checkbox" name="spUserSelectionCheckBox_]]></HTML>
<Counter Type="View" /><HTML><![CDATA[" id="spUserSelCb_]]></HTML>
<Counter Type="View" />
<Column Name="ID" /><HTML><![CDATA[" ]]></HTML><HTML><![CDATA[value="]]></HTML>
<Column Name="ID" HTMLEncode="TRUE" /><HTML><![CDATA[" account="]]></HTML>
<Column Name="Name" HTMLEncode="TRUE" /><HTML><![CDATA[" email="]]></HTML>
<Column Name="EMail" HTMLEncode="TRUE" /><HTML><![CDATA[" ]]></HTML><HTML><![CDATA[sip="]]></HTML>
<Column Name="SipAddress" HTMLEncode="TRUE" /><HTML><![CDATA[" ]]></HTML><HTML><![CDATA[onclick="UserSelectionOnClick(this,']]></HTML>
<Counter Type="View" /><HTML><![CDATA[');" ]]></HTML><HTML><![CDATA[title="]]></HTML>
<Column Name="Title" HTMLEncode="TRUE" /><HTML><![CDATA["/> ]]></HTML>
</DisplayPattern>
</Field>

The DisplayPattern element tells how the column should be rendered when a ListViewWebpart renders a view.

If you have list definition for your custom list you can directly go and modify the schema. When you don’t have schema but you want to achieve the checkbox in your custom list, this where comes the handy AddFieldAsXml of SpFieldCollection type.

So to add a checkbox, to select multiple listitem, in the custom list, declare a xml string as follows. (This is simplified version of DisplayPattern from the userinfo schema)
<Field Type="Computed" ReadOnly="TRUE" Name="ListItemSelection" DisplayName="Select" Sortable="FALSE" Filterable="FALSE" EnableLookup="FALSE" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="ListItemSelection">
<FieldRefs>
<FieldRef Name="ID" />
</FieldRefs>
<DisplayPattern>
<HTML><![CDATA[<input type="checkbox" ]]></HTML>
<HTML><![CDATA[LItemId="]]></HTML>
<Column Name="ID" HTMLEncode="TRUE" />
<HTML><![CDATA["/> ]]></HTML>
</DisplayPattern>
</Field>

and call the list.Fields.AddFieldAsXml(“xml string”);. Include this as a first column in your custom list’s view. You are done.



Cheers,
Murugan G.

24 comments:

AutoSponge said...

Can you clarify what you mean by "declare a xml string" and "call the list.Fields.AddFieldAsXml(“xml string”);"?

I don't understand where you put this information.

murugan.gs said...

Declare a string variable and assign the above said xml string then pass the string variable to the function.

I usually keep this kind of string declaration in a resource file as string resource and use it in code.

Murugan G.

aru said...

what do you mean with: Include this as a first column in your custom list’s view. You are done.

just create a column

aru said...

what do you mean with: Include this as a first column in your custom list’s view. You are done.

normal column creation?

murugan.gs said...

aru,

not in the column creation. after creating the column as explained in the article using xml, go the view(all items) editing/creating page and include the newly added column as first column, so that it will appear, when you are viewing all items, it display the checkbox in the first column.

mohican said...

but how can i check condition of these checkboxes in the column ?

CAMERA AFIRI said...

Hi,
i want buuton check all same user, and i want know how to process check all

Unknown said...

Hi all,

I have found on many many blog site for this problem. And get a deadly solution with no way out!!!

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
mattpeden said...

Great article, I have learned a great deal from stepping through your solution. I am currently trying to implement the "Select All" button that operates on the title bar of the newly created field. If you can provide any insight into how we can accomplish this it would be greatly appreciated.

Thank You

Unknown said...

Hi,
This post was really very helpful , which solved one of my issue.
Can U please let me know how to fetch checked/selected checkboxes programatically in a webpart.

That would be of great help if u can post some code sample.

Pham Van Trung said...

Great Article.

Thanks.

Kurko ml. said...

Hi Murugan,

Where do i paste "..HTML..CDATA..input id="checkboxID"... " when i export schema.xml from existing list?
When i add this to ViewBody>tag in schema.xml checkbox appere always when i add any of column(text, number, date,...) to list.
And then how i reach this checkbox in code from SPList, SPListItem or other object?
Can you show me piece of code?

ramesh said...

hi,
i was succefully added check box control to my List.when i was trying to edit the item iam getting following error:

One or more field types are not installed properly. Go to the list settings page to delete these fields

Unknown said...

How to access selected items?

i just want to access selected items in that listview webpart for not to update the same list.

Unknown said...

j

Maik Zeyen said...

Hello,

i'm very new to sharepoint but your solution is exact the thing i'm looking for.

But i don't know how to realize it. Maybe can you give me some tips on how to implement this.

I understand that i have to create the checkboxes with your xml but don't know then where to add the call list.Fields.AddFieldAsXml(“xml string”); and what i have to put in for "xml string".

Please can you help me.

regards Mike

Maik Zeyen said...
This comment has been removed by the author.
Unknown said...

I appreciate your article. The only thing that I would recommend is to use ReadOnly="FALSE" and to add ShowInNewForm="FALSE" ShowInViewForm="FALSE" ShowInEditForm="FALSE". The reason I suggest this is because when I tried to remove it because of some other problems I had, I received an error indicating that I couldn't delete a readonly field. Rather than writing code to update the entire schema xml again, it was easier to add these settings and use the field delete method.

Unknown said...

i have a custom list and i want to put a check box for each of the item and if i check the box then the item sholud open in a new window and by default in same window.. please help

jadoo123 said...

even after setting ReadOnly="FALSE" and adding ShowInNewForm="FALSE" ShowInViewForm="FALSE" ShowInEditForm="FALSE" the field can not be deleted. While deleting the field, it throws error that one or more fields are not installed properly.

Unknown said...

Great !!!
keep it up...

Sukumar Jena
www.objectfrontier.com

Unknown said...

Can you please clarify where should we declare xml variable and put the code ?