——————————————————————————
MOSS 2007 – Populating form fields dynamically
Populating form fields dynamically from querystring parameter in lists or surveys
There is no built in support in Sharepoint to dynamically populate a form field in a survey. We had a requirement where we wanted to pass in a querystring parameter to set the value for an input field in the survey. To solve this, I used the following approach :
1. Create the survey with your field which you want populated through querystring. Don’t specify any default value.
2. Browse to http://SPSServer/Lists/YourSurvey/NewForm.aspx
3. Do a view source. Find the name of your input field new TextField(frm,”Field_x0020_Field_x0023_“,”Please enter code#”,””), where please enter code is your field label. Grab the stuff in red color for later use.
4. Use Frontpage 2003 to connect to your sharepoint website. Go to Lists and you will find your survey there. Expand the survey folder and open Newform.aspx
5. Paste below code just above the closing </form> tag
6. <script lang=”javascript”>
var text=unescape(location.href);
var Variable=text.substr(text.indexOf(’?QueryStr=’)+10);
document.getElementsByName(’urn:schemas-microsoft-com:office:office#Field_x0020_Field_x0023_‘)(0).value =Variable;
</script>
7. Thats it. You should be able to populate the field by change the QueryStr value in the querystring.
——————————————————————————
MOSS 2007 – Create a survey, add question and choices programmatically
I was trying to create an out of the box survey programmatically and wanted to add the question and choices to it too. Below is the sample implementation in C#.
SPWeb web = SPControl.GetContextWeb(HttpContext.Current);
// create the out of the box survey using the list template
Guid surveyId = web.Lists.Add(“Name of the Title”, “Description of the survey”, SPListTemplateType.Survey);
SPList survey = web.Lists[surveyId];
string Question = “what?”;
StringCollection choices = new StringCollection();
choices.Add(“choice1″);
choices.Add(“choice2″);
choices.Add(“choice3″);
// Set the question and choices for the survey
survey.Fields.Add(Question, SPFieldType.Choice, true, false, choices);
// save the changes back to the list
survey.Update();
// save the changes made in the site
web.Update();
——————————————————————————
Get the no. of days between 2 dates in C#:
DateTime dt = Convert.ToDateTime(“04/14/2005″);
DateTime dt1 = Convert.ToDateTime(“04/03/2005″);
TimeSpan ts = dt.Subtract(dt1);
int day = ts.Days;
——————————————————————————
<codexes> <codex> <id>0</id> </codex> <codex> <id>1</id> </codex> </codexes>