Setting SharePoint Form Fields Using Query String Variables Without Using JavaScript

Originally posted on: http://geekswithblogs.net/SoYouKnow/archive/2009/10/29/setting-sharepoint-form-fields-using-query-string-variables-without-using.aspx

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.

Families in Germany who are facing divers health problem, such persons can buy drugs from the Web without prescription. With the market flooded with divers web-sites selling sundry medicaments, purchasing medicines from th WEB is no longer a trouble for common man. Certain medications are used to treat infections caused by dental abscesses. Of course it isn’t all. If you’re concerned about erectile health problem, you probably know about Xenical and Hoodia. Probably each adult knows about Garcinia. (Read more PhentermineXenical). The symptoms of sexual health problems in men switch on impossibility to have an erection sufficient for sexual functioning. Certain medications may add to sex drive difficulties, so its vital to cooperate with your health care professional so that the prescription can be tailored to your needs. Preparatory to taking Levitra or other preparation, speak to your pharmacist if you have any allergies. Talk to your health care purveyor for more details. Preparatory to ordering this remedy, tell your doctor if you are allergic to anything.