Tuesday, October 27, 2009

Customize a NewForm.aspx to accept Query Parameters

Works on WSS 3.0 and MOSS 2007

This post will show you to create a hyperlink in a Data View Web Part (DVWP) that open’s a customized NewForm.aspx. When the NewForm.aspx opens, it’ll have some of the required inputs already filled out for you. We’ll set this magic up by using DVWP and parameters passed from the DVWP to the NewForm.aspx.

I need to show you how I have my lists setup first, so let’s dive into that for a minute. I have two lists setup: 1. Calendar with two custom columns. These two columns are Job # and Job Name. The rest is default or whatever you’d like to have. The next list I have is a Tasks list that has these two custom columns as well. Here are some screen shots:

CustomCalendar

CustomTasks

You should take notice that I’ve required Job # to be inputted on each form. This is a must because it’s our primary key, if you will. This is going to be used for filtering later on, so it’s important.

Now that we have those lists setup we can now create our custom dashboard that allows hyperlink filtering and our magical custom newform.aspx. Fire up sharepoint designer and create a new aspx page. Add a webpart zone to your page, so we’ll have a spot to place our DVWP. Now we’ll want to click on Data View, Insert Data View. Inside our Data Source Library, we’ll highlight the Calendar. Then you’ll select show data. This will allow us to select the rows we’d like to appear in our DVWP. I usually only select one row and then click the Insert Selected Fields As: Multiple Item View. After I get the DVWP to show up on my page, then I’ll choose to edit the columns as they seem fit. SAVE YOUR WORK! We’ll call this page: Dashboard Right now, we want to see only the Job # and Job Name fields from the calendar list. Now we need to add a column to the DVWP. This will provide us a place for an additional hyperlink to our Custom NewForm.aspx. In the new column, pick any row that has a job # and then type “Add Task…”. If you have done it right, it’ll populate your entire DVWP.

Here’s a quick glance at what it should look like:

CalendarDVWP

Once you get this setup how you like it, we’ll need to add two parameters to our DVWP. When you highlight the DVWP, you’ll see a chevron on the right of it. That is a menu for Common Data View Tasks. Listed inside this menu is our parameters section. Open that and add two Query Strings. Here is a photo to guide you along:

QueryParams

We’ll need these in order to carry information from our Dashboard to our CustomNewForm.

We now need to create another new aspx page and customize it. This will be our custom NewForm.aspx. I called it AddTask.aspx, but you certainly can make it whatever you like. I saved my page in the same place where the NewForm.aspx is for the Tasks list that I’m using. Once we have added a new web part zone to our page, we can now add a custom form. If you follow the same steps as above to add a new DVWP, you’ll be fine. There are a few differences from what we did before though. You will add the DVWP as a New Item Form instead of Multiple Item View. Also you’ll want to make sure the DVWP is for the Tasks list and not the Calendar. You’ll also want to customize the inputs as you seem fit. Be sure to include any required fields on your Tasks list on your AddTask.aspx page. Also make sure that Job # and Job Name are inputs on your AddTask.aspx page. Ok once that has been setup, now we can get down to the magic.

We’ll now delete both of the text boxes for Job # and Job Name. We are going to replace them with our own. Highlight the form where you’d like for them to show up, and then select Insert, ASP.Net Controls, Text Box. We have to do that because when using the DVWP for a new item form, it wouldn’t allow me to alter the Value property. When I do it this way with our own ASP.Net control, I’m able to. Now we need to make these text boxes reference our Tasks list. There will be a little chevron on each text box. Clicking that will give us the options we need. Change the data field for both to relate to the field you need. i.e. Job # = Job #, you get the idea. Now switch to code view and drop a few custom lines of magic. First we need to add our Query Strings as a parameter for our page to use. This is the code we need to add:

<ParameterBinding Name="JobNum" Location="QueryString(JobNum)" DefaultValue="Please Type a Job Num" />
<ParameterBinding Name="JobName" Location="QueryString(JobName)" DefaultValue="Please Type a Job Name" />

 

You’ll need to search the code view using:

<ParameterBinding Name

Just paste the code provided above in the <ParameterBinding Name section. I usually just search the code view for <ParameterBinding Name using ctrl+f. Put these two lines of code below everything else in this section. Now we need to add these two lines in the <xsl:stylesheet section as a parameter. Using the same logic as above, I searched the code view for <xsl:param This can be a bit convoluted because there are many <xsl:param under many other xsl:template names. I added mine directly under the xsl:stylesheet. You’ll find a few parameters there, so add them accordingly. Here is the code that you need to add:

<xsl:param name="JobNum" />
<xsl:param name="JobName" />

 

Now we need to add a value to our customized text boxes. Again search the code for Job Name. Below that is the text box code. It begins with:

<asp:TextBox runat="server"

 

We need to modify this line and add one attribute:

value=”{$JobName}”

Your text box code should look like this… I suggest only to add the line of code above and not copy what is listed below!

 

<asp:TextBox runat="server" id="ff10{$Pos}" value="{$JobName}" text="{@Job_x0020_Name}" __designer:bind="{ddwrt:DataBind('i',concat('ff10',$Pos),'Text','TextChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Job_x0020_Name')}" />

 

Now change the Job # text box the same way except we’ll add this line of code instead:

value=”{$JobNum}”

 

SAVE YOUR WORK! We need to add one last thing to our Dashboard page and that’s our magic hyperlink! With that page open in Design View, highlight the “Add Task…” text that we added above. Right click and select hyperlink. We have to link this page to our AddTask form and add our parameters. Our link should look like this:

http://addtasks.aspxurl/?JobNum={@Job_x0020__x0023_}&JobName={@Title}

Save this page on final time and cross your fingers because I think we are ready to test this baby out! Open your Dashboard page and click Add Task… This should open your AddTask.aspx page and automagically fill out Job # and Job Name.

If you like this or need some clarification, please feel free to drop me a comment.

 

Bookmark and Share

12 comments:

Shawn said...

Is it possible to go the other way? That is, I'm trying to fillin fields on a calendar. Everytime I try to customize the calendar, it breaks recurrence. Is there a way that I can pass data from newform.aspx of one form to a calendar?

Matt said...

Can you elaborate? What I have above is two different lists. I am using one list to pass information to the next. This is to create relations between the data. Do you have something similar? If so, can you explain?

Jack said...

This is working for me when other form fields don't have validation. however when I have validation on other form fields, and one has an error, after resolving the error, the databind data isn't passed to list. EnableViewState is true on ASP.Net text box as well. Any ideas??

Matt B. said...

This method of sending Query Strings to fill out forms seems a bit deprecated in regards to what SharePoint Hillbilly has to offer. Shortly after this post, I found his work and have been using his method ever since. I have multiple DataFormWebParts setup using this method and I just tested one to see if it works. The query string data does persist after failing validation. I would suggest using his method, because it was far easier than what I am providing. Here's a link to the videos: http://bit.ly/9zj0nD Ping back if you still need some help. BTW, I'm using SharePoint and not necessarily an ASP.Net application, so I'm not sure if that matters or not.

Jack said...

Wow...perfect! Thank you so much for responding. So often I find good stuff like this, have a question and never get a response. So again....thanks!

Matt B. said...

Thanks for the compliment!

Tom said...

Having issues I am getting 404 not found ideas?

@iOnline247 said...

Hi Tom,
Are you using this technique in the context of SharePoint or something else?

Tom said...

yes Sharepoint 2007.
When I click create Job or what ever it should take me to the new form but form some reason dies with a 404.
Also I am getting a weird error that I have not seen before.
this webpart does not have a vaild XSLT stylesheet Error: String literal was expected, but no opening quote character was found.
I have gone back over several times and cannot find what it is speaking of and everything is exact

Tom said...

Sorry for Typo's
I am getting frustrated.
Our objective is this
We have 27,000 + numbers with related data to each one, we need to create tickets based on those numbers in another list and autopopulate the dataset related from the one list to the others and append new data in the ticket using the new form and task list.

Tom said...

Oh just to mention I am running through your steps in a sandbox site so I can understand your steps.

@iOnline247 said...

Hey Tom,
Sorry you are running into trouble with this. My steps I've provided here were from a naive time in my SharePoint career. What I would suggest, is to watch over the videos about creating Parent/Child relationships. It is far easier than what I am providing. Here's a link to the videos: http://bit.ly/9zj0nD

Post back if you have any questions using Mark's method. I've used this so much, I could do it in my sleep.