Once I got a request from one of colleagues to create custom button field which will kick off a workflow for the list item which it get associated. Then, I had created a custom field which inherited from SPFieldText and rendered an ASP.net button control and in the click event I have implemented the code to kick off the workflow.
Everything worked fine and field rendered perfectly in the New, Edit, and Display form. But in the list view it doesn’t shows the button, we can see the column name in the list but can’t see the button in the list item. Below is the code that I have used to create the field and the FLDTypes.xml file.
1: using System;
2: using System.Runtime.InteropServices;
3: using System.Security.Permissions;
4: using Microsoft.SharePoint;
5: using Microsoft.SharePoint.WebControls;
6: using Microsoft.SharePoint.Security;
8: namespace CustomButtonField
10: // TODO: Replace, as needed, "SPFieldText" with some other class derived from SPField.
11: // TODO: Update, as needed, ParentType element in fldtypes*.xml in this solution.
14: public class ButtonFieldWFField : SPFieldText
16: public ButtonFieldWFField(SPFieldCollection fields, string fieldName)
17: : base(fields, fieldName)
22: public ButtonFieldWFField(SPFieldCollection fields, string typeName, string displayName)
23: : base(fields, typeName, displayName)
27: public override BaseFieldControl FieldRenderingControl
29: [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
32: BaseFieldControl fieldControl = new ButtonFieldWFFieldControl();
33: fieldControl.FieldName = this.InternalName;
35: return fieldControl;
6: using Microsoft.SharePoint.Workflow;
8: using System.Web;
9: using System.Web.UI;
10: using System.Web.UI.WebControls;
11: using System.Web.UI.HtmlControls;
13: namespace CustomButtonField
15: // TODO: Replace, as needed, "TextField" with some other class derived from Microsoft.SharePoint.WebControls.BaseFieldControl.
18: public class ButtonFieldWFFieldControl : TextField
21: private Button oBtnWF = null;
23: protected override void CreateChildControls()
28: if (this.ControlMode == SPControlMode.Edit || this.ControlMode == SPControlMode.New || this.ControlMode == SPControlMode.Display)
30: oBtnWF = new Button();
31: oBtnWF.Text = "Start the custom WF";
32: oBtnWF.Click += new EventHandler(oBtnWF_Click);
38: void oBtnWF_Click(object sender, EventArgs e)
41: SPSite oSite = this.ListItem.Web.Site;
43: //get the workflow associated with this doc lib and kick if off
44: if (this.List.WorkflowAssociations.Count > 0)
46: // kick off the first workflow..you can kick off your custom workflow by taking it properly
47: SPWorkflowAssociation wrkFl = this.List.WorkflowAssociations;
48: oSite.WorkflowManager.StartWorkflow(this.ListItem, wrkFl, wrkFl.AssociationData, true);
54: protected override void Render(HtmlTextWriter output)
1: <?xml version="1.0" encoding="utf-8"?>
4: <Field Name="TypeName">ButtonFieldWFField</Field>
5: <Field Name="TypeDisplayName">ButtonFieldWFField</Field>
6: <Field Name="TypeShortDescription">ButtonFieldWFField</Field>
7: <Field Name="ParentType">Text</Field>
8: <Field Name="UserCreatable">TRUE</Field>
9: <Field Name="FieldTypeClass">4a71f8a6-a7d1-45bb-9ced-8ed1676c95d6</Field>
After doing research on this issue confirmed that, by default we can’t render any controls like button, imagebutton, etc in list view. Because the items are rendering in the list view through CAML and <![CDATA [ html goes here ] ], so here the only work-around is construct the control using CDATA and display it.
10: <RenderPattern Name="DisplayPattern">
13: <Column Name="ContentTypeId" />
15: <!-- This is the Content Type Id for FOLDERs -->
16: <Case Value="0x012000499529A73A47A1408DC7541F73C144CE">
17: <!-- We do not want to display anything for folders -->
21: <!-- This is the List ID -->
22: <!-- ListProperty Select="Name" / -->
25: <![CDATA[<input type="button" onClick="Test();" value="Start the custom WF" id="btn_]]>
27: <Column Name="ID" />
29: <![CDATA[" />]]>
36: function Test()
But, here another obstacle was the execution of server side code to kick off the workflow. We can’t write the server side code in <! [CDATA  ]>, so we can kick off the workflow by putting that code in the page load of an aspx page and open it while clicking the button.
(eg : window.open(“_layouts/custompage.aspx”)).
Another work-around would be adding an ECB menu item for the list item and implement this functionality while clicking on it.
Also please check this How-To video to know how we can create custom fields in WSS 3.0, if you are not familliar with this already.
PingBack from http://mstechnews.info/2008/11/work-around-to-render-the-custom-field-in-list-view/
Hi Can u send me the code of the work around, When I was trying to do the same proposed at the top it gives me an error saying the field types are not deployed properly
My mail id is firstname.lastname@example.org
I have sent a sample application to your gmail ID.
Thanks for posting such a great post.
I am trying to follow your steps in displaying the button in the list view.
I modified the XML file to add the CDATA portion like what you did..but no luck in displaying the button.
I added all the other part right.
This is my Render Pattern i guess i messed this one.
<![CDATA[<input type="button" value="MarkMeComplete"id="btn_]]>
New to CDATA concept what i am missing here?.please advise.
For me the code worked fine.
I wrote two classes(renamed to mine) in VS2005 c# library projet and using the same code of soumyan and build the project and placed the dll manually in GAC.
I had no build errors though was never able to display the button
Just my 2 cents.
I just sent a sample custom button field to you. Please check it out.
Reddy, could you please create a very simple custom field without renderpattern and see whether it is working fine or not.
Check the deployment is happening correctly or not.
I recieved your code and i am able to show the button in the list view form.
Also i sent an email about the customization i am working on for your guidance.
I just checked my mail box and got your message.
As a work around, while clicking on the button you can redirect the page to an aspx page and write the server side code in the page_load.
Oh ok..Thanks so much sowmyan for the idea.
so in my case.
i get hold of the sharepoint list date column modify the date and update and refresh the view right? makes sense..(all this in page_load?)
one question still remains ..
i have the button column to be shown..only if the date column is blank
any code snippet sample or idea for the java script how to do this..
Thanks sowmyan for the feedback.
so i have a jscript function hideButtonField() which has all the functionality..
where do i call this function it needs to be called on sharepoint page(my custom list view)load..
how can i do this?