Defaulting a Hidden Field on a Form
April 22, 2008
I needed to create a form that automatically populates the values for a user when they click the OK button. It turns out that SharePoint requires at least one editable field in order to save the form (If anyone knows a way around this requirement let me know). The scenario is that an Approver can click the Reviewed button in an email message to say that they have read the documents. They are taken to a modified new item form in a custom list and they click OK and the system records the necessary data. Here’s what I had to do to make it work.
Create the Custom List
The custom list was very basic in the case. We needed to be able to track which Approver was clicking OK and what date and time they clicked it. Luckily, ANY list records the Created and Created By data for an item. I simply changed the Title field to DocumentID and the list was ready to roll.
Because of the weird bug I encountered, I had to create another field called Notes. It’s a simple text field with the default options.
Create the Button
In my scenario, the button was to appear inside of an Outlook email - but you can use this logic for a button placed almost anywhere. The key was that we were using the URL to pass the variables to the form, so the button was a hyperlink to the new item form.
I created a button in photoshop and uploaded the image to a SharePoint picture library and grabbed the URL to the image. I then started by creating the following link in the email (part of a workflow):
<a href=”http://yoursharepointaddress/lists/customlistname/newitem.aspx?DocumentID=
At this point, I inserted a variable from the workflow. The workflow which is sending the email to the Approvers already ‘knows’ which DocumentID we are refering to. Therefore the code continued like this:
<a href=http://yoursharepointaddress/lists/customlistname/newitem.aspx?DocumentID=[DocumentIDVariable] target=”_blank”><img src=http://locationofyourimage /></a>
When the user clicks on the button image, it opens a new browser window and takes them to the specified URL. You can pass any variables that you want through the URL. The URL contains the DocumentID variable which we will make use of in the next step.
Create a New Item Form
I used SharePoint Designer (SPD) to edit the newitem.aspx file for the new custom list. I hid the default web part from view and then I added a new dataview. I linked the new dataview to the new custom list I created and inserted the DocumentID and Notes fields as a New Item Form (I will probably write something more detailed about these steps in the future - but the help files on the Microsoft website are perfect at explaining the basics).
I then changed to the DocumentID field on the new form to be a label and changed the Visible property to be False.
I created a new parameter in the Common Dataview Tasks and named that parameter DocumentID and set it to grab the DocumentID from the URL (Microsoft Help files on Dataviews). Remember the name of this parameter.
Now we get into the nitty-gritty. You MUST set the default value for the field in the actual code for the page and not in the properties for the field. I switched SPD to code view and located the code for the DocumentID field (sometimes it’s easier to set the view to Split mode and then click on the field to highlight the appropriate code). The code I added is highlighted:
<asp:TextBox runat=”server” id=”ff1{$Pos}” __designer:bind=”{ddwrt:DataBind(’i',concat(’ff1′,$Pos),’Text’,'TextChanged’,'ID’,ddwrt:EscapeDelims(string(@ID)),’@Title’)}” text=”{$DocumentID}” Visible=”False” Width=”21px” Height=”16px” />
The code I added sets the value of the ‘text’ to be the DocumentID parameter we created earlier (make sure you include the punctuation exactly as I have above. Save your new form and you are ready to test it.
Verify what you did
If you followed the previous steps you should now have the following:
- A hyperlink button you can place anywhere (on a site, in an email, etc.) that passes values through the URL.
- A customised new item form that takes the variable from the URL and populates a hidden field. You can apply these steps to any list you have.
- When the user clicks the button, it opens the custom form. The user then clicks OK on the custom form and the new item is created in the list.
As you may have realised by now, this is a truly powerful tool for getting information back from your users. I hope to come back at some point an elaborate on some of the earlier steps, but in the meantime, I recommend reading the Microsoft helpfiles - specifically those on the SharePoint Dataviews.
Thanks for reading.
Entry Filed under: SharePoint Designer, Web Application. .
7 Comments Add your own
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed
1.
Design Tip 4: Turn Any Fi&hellip | May 14, 2008 at 2:15 pm
[...] http://splittingshares.wordpress.com/2008/04/22/defaulting-a-hidden-field-on-a-form/ [...]
2.
LeoJ | June 13, 2008 at 9:46 am
i cannot find those code in SPD
Refer to your reponse in this article:
http://www.sharepointu.com/forums/p/3410/9037.aspx
When I click the field on Dataview in split mode after I insert the Query String parameter, I only can see this code:
and this sharepoint tag without any text value to be set. Would you mind to give me some hints?? Thanks.
3.
LeoJ | June 13, 2008 at 9:49 am
sorry for sending couple of post here, i cant show those code in comment…
Basically, I mean I cannot find asp tag in data view but only found sharepoint tag…. u know what i mean..?
Thanks!
4.
ThiNg | June 13, 2008 at 12:40 pm
LeoJ,
I’m going to take a stab at what you are referring to.
the “<asp:Textbox…” code only appears if you change the field to a “label”.
1. In design view, click on the field and select the flyout menu (Common FormField Tasks),
2. Change the Format As option from “List Form Field” to “Label”.
3. SharePoint will change the code from the SharePoint code to the Asp:Textbox code.
4. Locate the text value and change it as mentioned in the post.
I hope that helps clarify the steps a bit more?
Regards
Sanj
5.
LeoJ | June 16, 2008 at 4:33 am
Thanks Sanj and I found what u said in the flyout menu, but still one thing i cant figure out….
I have already set my Contact list with lookup field call “Company” and add the form action “netvigate to page” in the save button with follow link:
http://SharepointServer/Lists/Leads/NewForm.aspx?Company={@Company}
Also I added the Query String parameter in NewForm.aspx of Leads but when I go to browser to test it, it only show {@Company} in the company textbox of NewForm.aspx of Leads… it do not show text like Company A or something….. And also the Link shown in browser is
http://SharepointServer/Lists/Leads/NewForm.aspx?Company={@Company}
not
http://SharepointServer/Lists/Leads/NewForm.aspx?Company=CompanyA
I quite confirm i did every stpes u said….
Please kindly help!!! And Thank you very much!!!
6.
ThiNg | June 16, 2008 at 11:40 am
Hi Leo,
Ok, more than likely, the parameter is not being picked up on the source page (therefore not passed to the target URL). I would recommend testing it with a basic connection, that is, ignore the hidden field stuff and just form a hyperlink from one dataview to another.
Rather than rehash everything here, read this Microsoft help guide and follow all of the steps. Once you establish a link to the field, you can then change the field to a label and add the parameter to the code as per my instructions. Here is the link:
http://office.microsoft.com/en-us/sharepointdesigner/HA102403641033.aspx
I highly recommend reading ALL of the help topics on dataviews to make sure you have a firm grasp on how Microsoft ‘wants’ things to be done - then you can start tweaking.
If that still doesn’t work. let me know and we’ll set up test situation for you.
Good Luck
Sanj
7.
LeoJ | June 18, 2008 at 7:35 am
O my friend… this really drive me crazy, I have go through all those help topic and study alot about xml and xslt things… and also hv a little understanding of ddwrt… but still hv no answer with is… I cant grap the value from the previous list.. I think it should be something wrong when I set the form action, do you mind to post the code of the button with onclick=”javascript: {ddwrt:GenFireS…
really pain the ass… and thanks for your very kind response!
LeoJ