Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Okay you whiny people…  You’ve been bugging me for months now about the JavaScript I had previously posted to automatically populate a SharePoint Form Field based upon a Query String Variable… “It doesn’t work right for me”… “I don’t know how to copy and paste”…  “I don’t like JavaScript”… and on and on and on!

Enough already! I get it.. you are lazy like me! There’s nothing wrong with that really…  Some of the greatest things in the world were invented by lazy people… naps… cold pizza… foot rests…. wine (think about it.. “oh crap.. I forgot I left my grape juice out.. oh well.. I’m sure it’s still good”).  It’s actually a lot more work to be lazy than you think.  Us lazy people spend a lot of brain power to try and figure out how to do things faster and with less effort. 

Enough already… this typing is taking too much work. Where was I?  Oh yes… previous blog post.  In my previous blog post on creating a parent/child relationship using SPD there was a section of the blog which contained a chunk of JavaScript.  This JavaScript was used in the NewForm.aspx page to automatically set the value of a SharePoint Form field based upon a value in the Query String.  I dove into the JavaScript a little more in a later post on Setting SharePoint Form Fields Using JavaScript. However, as I stated earlier, some people were having problems with the JavaScript.  Some people had issues getting it to work because they were not placing it in the correct location in their file or they forgot to copy part of the JavaScript or they were confused about how to tweak the JavaScript to make it work for them.  One individual just did not want to use JavaScript and wanted another solution.  Well… I took this as a challenge.  I’m always up for learning something new.  So, I wanted to see what I could figure out, and what do you know!  I actually got something working.  I was able to use SPD and not write a single line of JavaScript to automatically set a SharePoint Form Field based upon a Query String Variable.

If you are following along and want this to work for you, stop right now and go back to my post on creating a parent/child relationship and follow that post up until the section labeled “CREATE A PAGE FOR CREATING A NEW TIME LOG ENTRY AND STORE PARENT’S ID”. You with me? You know where we are at?  At this point you have a Issue display page that also shows the corresponding time entries in a list.  You also have a link to “Create a new Time Log Entry…” in that list.  Clicking that link should take you to the New Item entry page for a time entry with a Query String variable of “IssueID” and a value. 

image

Excellent.. now we can actually start to do something.  So, at a very high level we are going to:

  1. 1. Create a new page to hold new Time Entry form
  2. 2. Insert a Custom List Form on new page
  3. 3. Create a parameter that contains the IssueID passed from the Query String
  4. 4. Convert the field that is to contain the IssueID to a text box field
  5. 5. Set the converted field based upon the Parameter IssueID
  6. 6. Make sure it works, then hide the field from Display

Doesn’t sound too hairy I hope?  Let’s get to it:

Create a page to hold new Time Entry form

So, first thing we are going to do is create a new page that users will use to enter a new Time Entry.  Make sure to create the page in the same site as your Issues and Time Entry lists. If you are a total newb like I was at one point and don’t know how to create a page in SharePoint here is what you need to do:

  1. 1. Create a new document library to hold your pages.  (Site Actions –> Site Settings –> Site libraries and lists –> Create new content –> Document Library)
  2. 2. Give your document library a name (I generally use “Pages” because that’s how I roll).
  3. 3. Specify a Document Template of “Web Part page” from the drop down list and click “Create”

image

    1. 4.  When list is created click on “New” and create your new page.  As far as template layout is concerned, choose whatever floats your boat.  Also, name your page “NewTimeEntry” for the purposes of this blog, anything else is fine too if you just want to be a rebel.
    2. image
    3. image
    4. image

Okay! Your new page is now created… now comes the fun parts.  Yes.. it IS kinda sad that I consider this fun… you should see me on the weekends. 

Insert a Custom List Form on new page

Now that we have our new page let’s open it up in SharePoint Designer.

Fire up SPD and open the site (File->Open Site) containing your issues list, time entry list, and the page we just created.

Open up the page we just created (expand Pages and double click on “NewTimeEntry.aspx”).

image

Now insert a “Custom List Form” for a New Time Entry:

  1. 1. Click in the “Header” area of the design view to get the proper focus on the page.
  2. 2. Click Insert->SharePoint Controls->Custom List Form
  3. image
  4. 3. When the pop-up window appears select the Time Entry list as the “List or document library to use for form:”
  5. 4. Make sure “Item” is selected for “Content type to use for form” (unless you have a custom content type of course)
  6. 5. Make sure the radio button “New item form” is selected and click “OK”
  7. image

There you go.. you should now see a poorly formatted New Item entry page for your Time Entry:

image

Go ahead and save your page… #1 rule of SharePoint Designer… “Save Often”.

Before we go any further, let’s go back to the DispForm.aspx for our Issue Page and make our “Create a new Time entry” link point to the page we just created. 

image

Double click on that page to open it up.  Click on “Split” or “Code” view and find the link to the “Create a new Time Log entry…”  It will look something like:

<td class="ms-vb" colspan="99">
     <a href="../Time/NewForm.aspx?IssueID={$IssueID}" onclick="javascript:this.href = unescapeProperly(escape(this.href)); GoToLink(this); return false;" target="_self" class="ms-alignleft">Create a new Time Log Entry...</a>
</td>

We need to make it point to our new page so replace ‘href="../Time/NewForm.aspx?IssueID={$IssueID}"’ with ‘href="../../Pages/NewTimeEntry.aspx?IssueID={$IssueID}" ’.  So the resulting code looks like:

<td class="ms-vb" colspan="99">
     <a href="../../Pages/NewTimeEntry.aspx?IssueID={$IssueID}" onclick="javascript:this.href = unescapeProperly(escape(this.href)); GoToLink(this); return false;" target="_self" class="ms-alignleft">Create a new Time Log Entry...</a>
</td>

Save your page. Okay, so now… if you go to the “Issue” display page and click on the link to “Create a new time log entry” it should take you to our newly created page.  Pretty easy?  Still with me?

Another important note.. ALWAYS use RELATIVE URLS! Never use FQDN (Fully Qualified Domain Names) for your links.  You will thank me later.

Create a parameter that contains the IssueID passed from the Query String

Now we need to store the IssueID passed in the Query String and store it in a Parameter our DataFormWebPart can use.

So, go back to your “NewTimeEntry.aspx” page.  It’s okay.. I’ll wait…

Open up the “Common Data View Tasks” menu by click on that tiny arrow at the top right of your DataFormWebPart and click on “Parameters…”

image

Click on the “New Parameter” button. Give your Parameter the name “ParamIssueID”.  The Parameter Source is “Query String” and the Query String Variable is “IssueID”.  When all is said and done your parameter should look like the following:

image

Click “OK”. 

Convert the field that is to contain the IssueID to a text box field

We now need to get that Parameter Value stored in the IssueID field.  The only problem is, we can’t do it with the field in the current state.  Go ahead and try, if you try to set the IssueID’s value to the parameter value you get an error.. SPD won’t let you do it. 

What we need to do is convert the IssueID field to a text field and then we can store the value.  In order to convert the Lookup field to a text field do the following:

Right click on the “IssueID” field and select “Format Item as->Text Box”. 

image

Why? How hard did you think it was going to be?

Set the converted field based upon the Parameter IssueID

Okay.. we are just about done.  We now need to set the value of this IssueID Text Field to the value of the Parameter we previously created.

Open up your NewTimeEntry.aspx page up in “Split” or “Code” view and find the IssueID field.  The code will look something like:

<tr>
<td width="190px" valign="top" class="ms-formlabel">
     <H3 class="ms-standardheader">
          <nobr>IssueID</nobr>
     </H3>
</td>
<td width="400px" valign="top" class="ms-formbody">
     <asp:TextBox runat="server" id="ff4{$Pos}" text="{@IssueID}" __designer:bind="{ddwrt:DataBind('i',concat('ff4',$Pos),'Text','TextChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@IssueID')}" />
                            
     <SharePoint:FieldDescription runat="server" id="ff4description{$Pos}" FieldName="IssueID" ControlMode="New"/>
</td>
</tr>

We need to change the ‘text={@IssueID}’ to contain our parameter, so change that to ‘text={$ParamIssueID}’ (Really, that’s all there is to it).  So, when all is said and done the code will now look like:

<tr>
<td width="190px" valign="top" class="ms-formlabel">
     <H3 class="ms-standardheader">
          <nobr>IssueID</nobr>
     </H3>
</td>
<td width="400px" valign="top" class="ms-formbody">
     <asp:TextBox runat="server" id="ff4{$Pos}" text="{$ParamIssueID}" __designer:bind="{ddwrt:DataBind('i',concat('ff4',$Pos),'Text','TextChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@IssueID')}" />
                            
     <SharePoint:FieldDescription runat="server" id="ff4description{$Pos}" FieldName="IssueID" ControlMode="New"/>
</td>
</tr>

And there you have it.  Save your page.  Go to you Issue Display Page and click on the “Create new Time Log Entry” link.  It will open up the NewTimeEntry.aspx page we created and the “IssueID” field will contain the same value as the Query String variable “IssueID”.  Go ahead, save your entry… it works fine.  Trust me.  well.. don’t trust me… try it first.

Make sure it works, then hide the field from Display

Okay, you’ve made sure everything works okay.  You’ve saved a time log entry or two and made sure it appeared in your Issue ID Display page? right? Well… go do it… make sure it works… the rest of us will wait… but we are getting impatient…

Okay.. thanks.. welcome back… If you were go forward with this into production you do NOT want the “IssueID” field to be visible on the page for a user to start calling you and say “Hey, what’s that there for” or worse yet, they get clever and change the value which screws everything up.  So, lets hide the field from view but make sure the value still gets stored.

Go back to that code up there that we modified, you know.. this code:

<tr>
<td width="190px" valign="top" class="ms-formlabel">
     <H3 class="ms-standardheader">
          <nobr>IssueID</nobr>
     </H3>
</td>
<td width="400px" valign="top" class="ms-formbody">
     <asp:TextBox runat="server" id="ff4{$Pos}" text="{$ParamIssueID}" __designer:bind="{ddwrt:DataBind('i',concat('ff4',$Pos),'Text','TextChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@IssueID')}" />
                            
     <SharePoint:FieldDescription runat="server" id="ff4description{$Pos}" FieldName="IssueID" ControlMode="New"/>
</td>
</tr>

You don’t want to delete these lines of code obviously or everything we worked for will not work.  What we need to do is change the class of the label and field to “ms-hidden”.  This will prevent the field from displaying but still allow it to hold values.  So, when you are done, the code will look like:

<tr>
<td width="190px" valign="top" class="ms-hidden">
     <H3 class="ms-standardheader">
          <nobr>IssueID</nobr>
     </H3>
</td>
<td width="400px" valign="top" class="ms-hidden">
     <asp:TextBox runat="server" id="ff4{$Pos}" text="{$ParamIssueID}" __designer:bind="{ddwrt:DataBind('i',concat('ff4',$Pos),'Text','TextChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@IssueID')}" />
                            
     <SharePoint:FieldDescription runat="server" id="ff4description{$Pos}" FieldName="IssueID" ControlMode="New"/>
</td>
</tr>

Makes sense? Go back and test it again… the Issue ID field is gone from the NewTimeEntry.aspx page, but everything still works. 

And there you have it!  Happy?  Solve your issues? Open up new issues? Keep the questions coming and I’ll do what I can to help! Thanks again for stopping by.

posted @ Thursday, October 29, 2009 12:56 PM
Print

Comments on this entry:

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Fabian Williams at 10/29/2009 1:19 PM
Gravatar
Nice Post! Ill have to bookmark this page

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Jeff DeVerter at 10/29/2009 2:57 PM
Gravatar
Great post, Mark. There is a team member who was trying to do something like this. I'll send it over to him.

You should record a "screenjelly" of the working form to show us how it works:
http://www.screenjelly.com/
(my new favorite app)

Jeff

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by François at 10/31/2009 5:12 AM
Gravatar
Post very complete with a lot of tips&tricks very useful! Thanks.

I had a question on form field display: i didn't found how to remove label Filed and keep only Field Value?

For example in Title form field, Title Label is displayed, then title value and title description.

Do you know how hide Title Label?

Thanks for your help!

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Amir at 11/8/2009 4:37 AM
Gravatar
Dear Mark
its really a great post, i have followed botht he posts of yours (with javascript and without javascript), it works very fine.
but there is one aspect in which i woudl like to get your help, that is.. hwo to set Picture/Hyperlink fields of the form with QueryString parameters. if i convert the Hyperlink/Picture field to textbox field, it does show the parameter value in the text box btu then it doesnt save it when i click OK (for example on NewForm.aspx) for some list. the Picture / Hyperlink just remains empty fields where as all the other text (TextBox-able fields) gets saved.

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by FlameKeeper at 11/11/2009 10:45 PM
Gravatar
Hello! Your post was very useful to me, but I have some troubles with my issues: I try to automatically fill that asp textbox with the parameter that I get from dataview web-part, formatted as a selectbox. I use web-part connection between the selectbox and listform web-part and get the chosen record ID to the parameter in listform. But how to dynamically send that parameter to asptextbox? Thank you!

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by April at 11/17/2009 9:52 AM
Gravatar
We are having an issue getting the custom list form to render. Each field displays an error: "Object reference not set to an instance of an object". this happens with the single and multiple use forms (not for viewing the items though). it's an error we get on production and dev environments. any ideas on why the form controls do not render in spd???

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Kannan at 12/7/2009 8:52 PM
Gravatar
Great post Mark. Made our life easier.

Thanks a lot.

# Aww Yeah!!

Left by Mega-Doodle Inspired at 12/23/2009 8:59 AM
Gravatar
This solution worked great for me as I was one of those crazy people who couldn't get the javascript to work. = ) Thank you for putting up with and helping us.

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Bhadram at 1/14/2010 9:59 AM
Gravatar
The only problem i am facing here is that when i try to save the form its not getting saved. And more over it throws error saying " Invalid data has been used to update the list item. The field you are trying to update may be read only." I exactly followed what u said. So i dint make any field non editable then y is it giving error. How should i solve it please help.

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Jazz at 2/11/2010 10:18 AM
Gravatar
Very good post! Thanks.
I have a query. I have three pages. p1,p2 &p3. User will be redirected from p1 to p2. On p2 I have a Form Web Part. I want to fill up a text box of this web part with the value in querystring(from p1). After that, when user submits p2, he will be redirected to p3. On p3 I have DataFormWebPart that shows the status message according to what is submitted from p2. So, my question is, Is it possible to fill up textbox of Form Web Part with querystring value?

Thanks.

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Susan D at 5/20/2010 2:05 PM
Gravatar
Great article!
I got this to work just fine for a custom list... now... How do I make it work for a document library?

Thanks!

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by deepak lohia at 8/9/2010 8:39 AM
Gravatar
i do no have words to thank you..its so simple method to do..i got mad using javascript...thankyou bro once again

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Seva at 8/10/2010 1:14 AM
Gravatar
Hi
I have the same requirement in sharepoint 2010. Could you please give me the steps for sharepoint 2010?

I have followed the same in sharepoint 2010, but no luck.

The steps you mentioned is looking very simple. It would be great if I get the steps in Sharepoint 2010.

Thanks in advance.

Regards
Seva

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Seva at 8/10/2010 2:08 AM
Gravatar
Nice article for SharePoint 2007 developers.
Would be great if I get same steps in SharePoint 2011.

Thanks in advance

Regards
Seva

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Yan Locas at 8/24/2010 7:38 PM
Gravatar
Awesome post !! thanks :)

I have managed to make it work in SharePoint 2010... In SPD 2010 you cannot convert a field to a textbox, so you have to make this conversion manually in the code by replacing the <SharePoint:FormField ...> by a <asp:TextBox ...> and by renaming the 'Value' and 'ValueChanged' in the attributes by 'Text' and 'TextChanged'. Also I have removed the attributes ControlMode and FieldName because they don't apply to a Textbox control.

So I get something like this : <asp:TextBox runat="server" id="ff6{$Pos}" text="{$ParamRequestID}" __designer:bind="{ddwrt:DataBind('i',concat('ff6',$Pos),'Text','TextChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Request')}"/>

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by jcnet at 9/15/2010 6:06 PM
Gravatar
Most everybody is doing this in Javascript. I've done it in Javascript.

Not proud to say I spent at least 2 hours trying to find a way to do this without Javascript. I must have tried 20 different tests and read at least 20 blog entries I thought would help.. and then I found this!

Thank you so much for posting this. This is extremely valuable!

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by kmuise at 10/12/2010 3:12 PM
Gravatar
This works great for a text field, but how would you get it to work on a lookup field.

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Jeevan Desarda at 11/2/2010 7:58 AM
Gravatar
I need the same solution for SharePoint 2010 but without Javascript, please anybody can help.....!!!!Please.......I am stuck over here....

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Anne Loveless at 12/7/2010 11:14 AM
Gravatar
I was so happy when it worked, so I headed back up to the top of the page. It was from you!!! Thanks so much Mark for this example. You made my day.

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by nuno telo at 2/1/2011 5:22 PM
Gravatar
great post...thanks

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by George at 2/9/2011 7:00 AM
Gravatar
Yay for Yan Locas! I've spent half a day trying to work out how to get that damn field to be a TextBox!

Mark - any chance of you re-writing this & your other fab pages to be SP2010 specific?

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Fabiano at 3/22/2011 1:48 PM
Gravatar
Thanks for the post, Mark!
I found a kind of a Sharepoint bug... I have 2 required fields in my form, the first is a Sharepoint:FormField and the other is a asp:textbox which is receiving the querystring parameter. If you submit the form when the asp:textbox field is populated, but leaving the required sharepoint form field blank, the required field red message appears. After populating the sharepoint form field and submiting the form again, nothing is recorded in the textbox field. Did you experience this issue? I really need to fix this!

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Matt at 4/12/2011 2:31 PM
Gravatar
I successfully got this working. However, is it possible to set more than one field in the 'Time' list? Ive got an Equipment Management/Maintenance system built in Sharepoint. My 'Issues' list is called "Equipment List" and my 'Time' list is "Equipment Maintenance Log". My Eq List contains all of the equipment (name, mfg, model, serial number, location, etc). I then have cascading dropdowns on that lists NewForm.aspx so I can quickly pick a piece of equipment based on location. Using your parent/child instructions I am successfully 'linking' maintenance records to particular pieces of equipment. However, when I click on the piece of equipment I dont just want it to pass the ID across to the other form, but also the Device Name and Location. Any way to do this?

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by shreya at 6/7/2011 2:54 AM
Gravatar
Good Post...Mark. I have one other question. I have more than 3 web parts are on the page. Webparts are New Entry Form,Video in content Editor, List form. Now problem is that Content editor webpart with video is followed by new entry form. Input fields contains focus. So every time scroll bar comes in middle of the page.It is really annoying. It will be greatful if I can get any solution ASAP. Thanks.

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Robert at 7/14/2011 8:31 AM
Gravatar
This solution unfortunately does not work on the SPS2010 ... is there any other for SPS2010?

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by MrSena at 8/20/2011 12:14 PM
Gravatar
Work in SP Foundation 2010!
Thanks!

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Reshma at 11/30/2011 3:23 AM
Gravatar
You Saved My Life.. Thanks !!!!

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by ThorUnchained at 11/30/2011 10:27 AM
Gravatar
I have a problem -
I use <asp: TextBox runat = "server" Text = "{$ QueryString variable}" id = "$ {Pos} ff1" />, And works

but the record does not write the value unless I add or delete any character. There may be some way that the TextBox has already recognized the value of QueryString without needing to insert or delete characters to insert the record?
Thanks in advance

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Ulrich at 12/21/2011 2:18 PM
Gravatar
This is very cool, and I can make it work, just fine in 2010.

Anybody knows how todo the same in editform.aspx? It dosent seem to work at all.

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Eric at 3/6/2012 12:50 PM
Gravatar
Great post -- it was exactly what I needed. It works in 2010 as well, you might want to update your post to reflect that. I know that I skim past posts that aren't tagged with 2010, and this technique still works. Only difference is that you can't seem to get the Format As menu in Code view.

# re: Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Left by Pawan at 4/8/2012 12:04 PM
Gravatar
nice post .. looking for this fr an hour..Thanks a lot buddy

Your comment:



(not displayed)

 
 
 
 
 

Live Comment Preview:

 
«September»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011