Search code examples
sharepointcheckboxwindows-sharepoint-services

Add a checkbox to each row of a ListViewWebpart


I'm working on a WSS 3 site that has a ListViewWebpart displaying various columns.

I need to add a checkbox to each row and a button to the header that will perform a server side action for the selected rows.

Do I need to make my own custom webpart or can the ListViewWebpart support checkboxes?

Adding checkboxes to each row

I've found a post Checkbox in ListViewWebpart which suggests

...to add a checkbox, to select multiple listitem, in the custom list, declare a xml string as follows.

<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.

I'm assuming the latter part requires a SPList. E.g.

SPList list = SPContext.Current.Web.Lists["MyList"];
list.Fields.AddFieldAsXml(stringWithXmlFieldDefinition);

Adding a button to the header row

One option for the header button is a CustomAction. This should create a button in the toolbar.


Solution

  • Here is a post to create a custom web part, then you can add a check box to your custom web part using the post you have found (Checkbox in ListViewWebpart).

    Be aware that,list.Fields.AddFieldAsXml(stringWithXmlFieldDefinition); can end up adding many duplicated check boxes to you sharepoint. The duplicated can be deleted from database and are located at AllLists table, tp_Fields column.

    To find the right fields, you can search by the list guid.

    declare @xmlString as xml
    Select  @xmlString = tp_Fields
    From        [WSS_Content].[dbo].[AllLists]
    Where   tp_id ='xxxx'
    Select @xmlString
    

    then, update the fields

    declare @string as varchar(max)
    set @string ='new value without duplicated checkbox'
    UPDATE [WSS Content] . [dbo] . [AlILists)
    SET [tp_Fields] = @string
    WHERE tp_ID = 'xxx'
    

    You can simply add a button by

    ToolBarButton newbtn = (ToolBarButton)Page.LoadControl("~/_CONTROLTEMPLATES/ToolBarButton.ascx");
    

    but possibly you need create another toolbar to hold the button, you can even create your own toolbar. You just need to put it in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES\YourCutomToolBar.ascx