Delete button in listview

June 25th, 2009 | Posted by Jeffrey Paarhuis in Configuring

What I wanted to do is adding a delete button to each item in the listview to delete a record directly from the overview. Sharepoint has an edit button that can be inserted by adding the “Edit” column in the view editor. It’s a shame that Sharepoint doesn’t have a similar option for deleting. This doesn’t mean it is impossible, it’s just not that easy, here you go:

<IsVisible>true</IsVisible>

Open SPD(Sharepoint Designer) and go to the list you want to add the button to and open the AllItems.aspx

duplicate the ListViewWebPart by copying the following code and pasting it directly after itself.

<WebPartPages:ListViewWebPart runat="server" __MarkupType="xmlmarkup" WebPart="true" __WebPartId="{28B30FD4-BA90-4FFE-8EA6-2608237AF16B}" >
...
...
</WebPartPages:ListViewWebPart>
find the following line in the second web part and change
<IsVisible>true</IsVisible>

to

<IsVisible>false</IsVisible>

Be sure that the first listform is now visible when you go to the list with your browser.

In SPD rightclick the first listview and click “Convert to XSLT Data View”. You can now edit every column, value and formula. Create a new column where you want the delete button to be. In my case I want it next to the edit button so I rightclick that column and insert a column to the right.

In the newly created column you can change the header to “Delete” or “Remove” or whatever you want.

Now we need to locate the empty cell in code view. Assuming you have split view we’re going to select only the first two cells (not the header) of the newly created column, you see the code view jump.  Now locate the empty

<TD Class="{$<SomeId>}"></TD>

or

<TD Class="{$<SomeId>}">

 

<xsl:text xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:nbsp-preserve="yes" disable-output-escaping="yes">&amp;nbsp;</xsl:text>
</TD>

in your selected code and replace it with

<TD Class="{$<SomeId>}">

<xsl:choose>

<xsl:when test="ddwrt:IfHasRights(4)">

<a href="javascript: {ddwrt:GenFireServerEvent(concat('__cancel;__delete={ID=',@ID,'};__commit'))}"><img border="0" alt="Edit" src="/_layouts/images/delete.gif" /></a>

</xsl:when>

<xsl:otherwise>

<xsl:text xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:nbsp-preserve="yes" disable-output-escaping="yes">&amp;nbsp;</xsl:text>

</xsl:otherwise>

</xsl:choose>
</TD>

Now we are done, save and enjoy deleting! =)

Update

For them who want to have a conformation box before deleting just insert this code instead of the above:

<TD>
<xsl:choose>
<xsl:when test="ddwrt:IfHasRights(4)">
<a href="javascript: if(confirm('Are you sure you want to delete?')){ddwrt:GenFireServerEvent(concat('__cancel;__delete={ID=',@ID,'};__commit'))}"><img border="0" alt="Edit" src="/_layouts/images/delete.gif" /></a>
</xsl:when>
<xsl:otherwise>
<xsl:text xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:nbsp-preserve="yes" disable-output-escaping="yes">&amp;nbsp;</xsl:text>
</xsl:otherwise>
</xsl:choose>
</TD>

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

6 Responses



Leave a Reply

%d bloggers like this: