dinsdag 10 november 2009

Add description to SharePoint survey questions

Whoot. This is cool.

Together with my customer, we decided to use the SharePoint survey in our intranet environment. Although, there was one functionality missing out-of-the-box: providing a description with every question. This function usually is supported in SharePoint, but unfortunately it is not implemented by default. I asked myself the question, what do I need to do to accomplish this?

1. Create a place in the object model to save the Description;
2. Edit the qstedit.aspx & qstnew.aspx to provide the Description;
3. Edit the SurveyForm template so my description would show up.

Part 1
While testing part 1 of my solution, I created a feature that added an extra field to the SurveyList.Fields. I accomplished this by using the SPList.Fields.AddFieldAsXml() method. An unpleasant and unexpected surprise: this didn't result in an extra property per question, but an extra question named Description itself! That made me curious about how SharePoint saves the survey data, so I opened my SharePoint Manager.



As you can see in the picture above, there is already a property to save a Description! This is gonna be easy!

Part 2
The form to create a new response to a survey is located at: ~/layouts/qstnew.aspx. Inside this page you'll find alot of Javascript and some HTML. You just have to add the following HTML (@ line 1263) to provide a textarea where the description can be provided:


<!-- description -->
<tr>
<td class="ms-authoringcontrols" width=10>&nbsp;</TD>
<td class="ms-authoringcontrols" ID=TD1><label for="idColName">Description:</LABEL><font size=3>&nbsp;</FONT>

<table border=0 cellspacing=1><tr> <td colspan=2>
<textarea class="ms-long" cols=40 rows=5 name="Description" id="Description" size=30><%SPHttpUtility.HtmlEncode(strDescription,Response.Output);%></TEXTAREA>
</TD> </TR></TABLE></TD>
</TR>

<!-- end description -->


Also edit the qstedit.aspx in the virtual layouts folder. This seems to be my lucky day, because the Javascript to include the current Description from the object model in the textbox area is already there! You only have to add the following code @ line 1661.


<!-- description -->
<tr>
<td class="ms-authoringcontrols" width=10>&nbsp;</TD>
<td class="ms-authoringcontrols" ID=onetidFldEditGuts2><label for="idColName">Description:</LABEL><font size=3>&nbsp;</FONT>

<table border=0 cellspacing=1><tr> <td colspan=2>
<textarea class="ms-long" cols=40 rows=5 name="Description" id="Description" size=30><%SPHttpUtility.HtmlEncode(strDescription,Response.Output);%></TEXTAREA>
</TD> </TR></TABLE></TD>
</TR>
<!-- end description -->


Your form will look something like this:



Part 3
At this point we can save a Description per question. I assumed the functionality to present the Description in the SurveyForm template wasn't build by Microsoft, because there was no possibility to provide a description. At this point, I was afraid that I had to rewrite the SurveyForm template, including Branching Logic. A pleasant and unexpected surprise: the description was already presented! The description is shown nicely on the form when a user is creating a response for a survey, as you can see below:



Summarized
To provide a description with your survey questions, just alter the qstnew.aspx and qstedit.aspx pages in the layouts folder! SharePoint will do the rest for you.

maandag 9 november 2009

Preset form values in SharePoint 2007

My current customer is implementing a new system that is currently using a Windows help file (.hlp) to provide help/workinstructions for a specific form or field in that application. In the current situation, a user is presented with a static help article when F1 is pressed. Because it's difficult to maintain this help file, we decided to migrate these help/workinstructions to a Microsoft SharePoint Wiki library. Together with an approval workflow, it's easier to maintain the content of the help library.

In the new situation, a browser instance is started when a user presses F1, referring to the SharePoint portal. The ID's related to the window or field where the user pressed the F1 key are provided through parameters in the URL. A custom webpart fetches the querystring parameters from the URL and checks with a CAML query if there are any matching Wiki articles. When a single article is found, the user is redirect to the matching article. When there are no articles found, the user is provided with a link to create a new article. The default newform page doesn’t have the ability to preset any values. Because I cannot explain to an end-user to manually enter the metadata properties for the new article, those values need to be filled in automatically according to the parameters in the URL.

I accomplished this using a brilliant javascript provided by Rob Howard, a Microsoft developer with a blog. There are some interesting comments underneath his blogpost.

CodePlex offers a solution that makes it easy to include javascript or jQuery in your SharePoint list forms, using a custom field. Unfortunately, this only works when the form pages are located in the forms folder of the SharePoint list. The form used by a Wiki to create a new article is located in the layouts folder (~/_layouts/CreateWebPage.aspx). Using ASPX forms outside the form folder of the list is not supported by this solution. In case you want to do the same for a document library or custom list, you can use this pragmatic solution.