Validation in InfoPath

Hi everyone!

Due to a discussion in a thread at Microsoft Forum, I decided to create a post about a couple of ways to handle validation in InfoPath.

The two ways I will show you are these:

  • Disable a button until all questions are answered.
  • Showing a message if a question is missed.

This is the how the form looks like.

As you can see we have a couple of questions to answer and then we can move on to the next view to continue with more questions. To stop the user from being able to go to the next view we could disable the button, this is how.

First click on the button Next view, and then on Manage Rules in the ribbon.

Now for the button Next view create a Formatting rule called DisableButton, that looks like this.

Now the button can’t be used until Question1 and Question2 is answered. Use Prereview to make sure it works.

Ok, so now lets move over of how to show a message if the user missed to answer a question. First you have to delete the rule from the previous example.

These are the fields in the form.

First of all, above the button I have placed the field called Message, and it is formatted like this.

No border, read only and with the same color as the rest of the cell, this way it’s invisible until a message is written to it.

As before, choose Manage Rules for the button Next view, this time create two Action rules.

The first rule I name Message_Blank and this rule looks like this.

As you can see I use the action Set a field’s value and the only thing we do here is to set the field Message to nothing/blank. You should not add the apostrophes, InfoPath will do that for you, just leave the field blank.

Now we need to create one more rule to show the message. Let’s call it Message_Error, it’s the same kind of rule with a few differences and it looks like this.

As you can see we I have a condition, the message is not blank, but actually holds the text we would like to show the user. And last but not least, I have check marked Don’t run remaining rules… That is because the last rule this button should have is to switch view, but not if this rules condition is met.

This is how it will look like if the user trys to move on before all questions are answered.

That’s it!

Try it in preview to make sure you done it the right way.

CU!

/Niax

Advertisements

Add customizable information to a library page

Hi everyone!

As you know when you add a web part to a library page, like Allitems.aspx or a page of a custom view that you’ve made, the ribbon gets wacky. That’s because it doesn’t know which web part it should have its focus on.

So what could we do if we really want some information, let’s say above the library web part?

And the information should be able to edit in an easy manner without using SharePoint Designer (SPD).

In this example I have a top site and underneath this I have some sub sites that are created from a site template I made. The things we are going to do now is made to that template so it could be reused.

First of all we have to edit the page’s code a bit, so bring up the page in your favorite editing tool SPD!

Find where the web part zone for the library begins and add two DIV’s right before it starts, like this:

PlaceHolderMain" runat="server">
<div id="success"></div>
<div id="error"></div>
<WebPartPages:WebPartZone runat="server" FrameType="None" ID="Main" Title="loc:Main">

As you can see I have added two DIV’s right after PlaceHolderMain with the ID’s of success and error. Don’t close this page, we are coming back here real soon.

What we are going to do now is to add some content to the success DIV.

First we need to have some content, right?

On the top site, let’s create a new Web Part Page, call it content.aspx and choose Full Page, Vertical.

Add a Content Editor to the page, click inside it, choose to Edit HTML Source…

…and paste this code:

</pre>
<div id="<span class=">MyDiv"></div>
<pre>
The content within this DIV will be shown above the document library.
<div>
<ol>
<li>To think about...</li>
<li>And this as well...</li>
</ol></div></div>

Ok, so now we have som content and as you can see the content is kept inside a DIV with the ID of MyDiv.

Now we are going back to the first page (where we have the library and added the two DIV’s) to add this information to the success DIV.

Let’s add som jQuery!

Find AdditionalPageHeader and add this code:

<asp:content contentplaceholderid="PlaceHolderAdditionalPageHead" runat="server">
 <SharePoint:RssLink runat="server"/>
<script type="text/<span class=">// <![CDATA[
javascript</span>" src="/sitename/Shared%20documents/jquery-1.6.2.min.js">
// ]]></script>
<script type="text/javascript">
 $(document).ready(function() {
 $("#success").load("/sitename/sitename/SitePages/content.aspx #MyDiv",
 function(response, status, xhr) {
 if (status == "error") {
 var msg = "Page is missing: ";
 $("#error").html(msg + xhr.status + " " + xhr.statusText);
 }
 });
});
</script>
</asp:content>

As you can see, first I have a reference to my jQuery file, you should replace the URL so that it corresponds to your environment.
Then in the next section we are loading the content in MyDiv into the DIV with the ID of success.

Now we are done!

Save all pages and try it, it should look something like this:

This way you can create new sites from this site template and just by editing the Content Editors in the page content.aspx you could update the contents on all sites.

Isn’t that nice?

CU!

/Niax


Open an InfoPath form in a dialog

Hi everyone!

I would like to share with you how you could open a form in a dialog in SharePoint.

  • Add a Form web part on a page of your choise.
  • Replace the code with this:
<div> <a href="javascript:{ SP.UI.ModalDialog.OpenPopUpPage('/sitename/_layouts/FormServer.aspx?XsnLocation=/sitename/library/forms/template.xsn&OpenIn=browser&SaveLocation=/sitename/library', null, 1000); };">Open form</a></div>
  • Replace sitename and library with the ones in your environment.
  • Click Save and OK in the web part and try it out!

Thats it!

/Niax


Add som nice picture buttons to your form

Hi everyone!

I thought that I should show you how easy it is to spice-up your form with som nice pictures as buttons instead of the boring ones that comes along.

Start by adding a couple of ordinary buttons to your form.

When that is done highlight a button by clicking on it.

In the ribbon, choose the Layout tab.

Then in the Table section, choose the drop down Change to to change the button to a picture button.

Now when that is done open the properties of the button. As you will notice you are given the opportunity to add not one, but two pictures for each button. The second button will act as the a hover-button, isn’t that nice 🙂

If you choose the Advanced tab you could add some text as well that will be shown when you hover over the button.

CU!

/Niax


Expense report (follow-up)

Hi everyone!

Ok, so this is a follow-up on my last post Name of month in expense report.

I promised that I would tell you how I did to get this expense report, to be editable only until a certain date and how the field that is telling us for which month this report is valid for, would keep it’s value even if the report is opened later on.

In this example, let’s say that our finance dep would like the employees to deliver their expense reports prior to the 16 of the current month. The employees should still be able to open these reports later on to be able to check and compare but not being able to make any changes or to save the report.

If you haven’t already created a data connection to submit the form back to the form library and added a button for the users to do this with, please do so 🙂

Set the correct month and keep its value

Now we need to do a few things to keep the value of the field for which month this report is for. What I am taking about is the field right after the title.

Start by adding a new field of the type string called ValidMonth.

Add another field of the type whole number (integer) called Day and give it the value substring(TodaysDate, 9, 2), now we know which day it is.

We should now give the submit button a rule.

Highlight the button, click on the button Manage rules in the ribbon.

The action we would like this rule to do for us is to set the value of ValidMonth to the name of the month for which this report should have. It could be the current month but it could also be the upcoming month if it is created between the 16th and 31st.

Create a new rule with the condition to be fired only if ValidMonth is equal to Empty and Day is less than or equal to 15. The action should be to set ValidMonth to the name of the current month. The rule should look like this:

But what if  we would create a new form between the 16th and 31st?
We need to create one more field to hold the name of the next month.

Give the field the name NextMonth with the value MonthNr +1

Ok, so now we have the number of the upcoming month, let’s use it to set the name of the next month.

Create one more rule for the save button with the condition to be fired only if ValidMonth is equal to Empty and Day is less than or equal to 31 and greater than 15. The action should be to set ValidMonth to the name of the next month. The rule should look like this:

There you go!

Now the form’s month is set only once and depending on if it should be valid for this month or the next, it will get the correct one. If you add these rules to the Form load it will be set when you open the form.

Editable until a certain date

Let’s add a rule for the submit button. Make it a formatting rule with the condition to only fire when Day is greater than 15 and mark the choice to Hide this control. That way the button will be hidden when the date is greater than 15th, and they will not be able to save any changes.

What if we want to create a new report between the 16th and 31st?
Ok, so we also need to check if ValidMonth is equal to MonthName, in that case the submit button should not be seen.

So, add another condition to that same rule that says:

and ValidMonth equals MonthName (be sure to use “and”)

Ok, there you go!

That was pretty easy 🙂

CU!

/Niax


Name of month in expense report

Hi everyone,

Right now I am composing a form in InfoPath for an expense report. I thought that I would share a couple of things with you guys.

I will start of with this post and will probably follow up with one or two more.

In this post I will show you how I accomplished this:

  • Putting in the name of the month into the form

Ok, first of all you need to create a new data connection to get information from a XML-file.

Copy this code, put it in a Notepad and save it in UTF-8 format as Month.xml:

<?xml version="1.0" encoding="UTF-8"?>
<months>
<month id="1" name="Januari"/>
<month id="2" name="Februari"/>
<month id="3" name="Mars"/>
<month id="4" name="April"/>
<month id="5" name="Maj"/>
<month id="6" name="Juni"/>
<month id="7" name="Juli"/>
<month id="8" name="Augusti"/>
<month id="9" name="September"/>
<month id="10" name="Oktober"/>
<month id="11" name="November"/>
<month id="12" name="December"/>
</months>

Now create a data connection and choose to get data from another source such as a XML-file. Browse to the file Month.xml and choose to get data whenever the form is opened.

In the Main data source create a new field of the type string called TodaysDate.
Give this field the default value of Today() which will give you todays date.

Create another field of the type string called MonthNrText.
Give this field the default value of substring(TodaysDate, 6, 2) (that is when the date format is like 2012-01-06)

Now let’s create a new field and convert MonthNrText to an integer to take away the leading zeros.

Call this new field MonthNr and give it the value of number(MonthNrText)

Now we have the number of the current month, let’s make one more filed to get the name of this month.

Let’s create a field of the type string and name it MonthName. With this field we will compare the id in the secondary data source against our field MonthNr.
Give this filed the default value of @name[@id = MonthNr], this is how it is done.

Open up the properties of MonthName and click on the function-button to the right of the value field.

Click on the left button insert Field or Group... Choose the secondary data source Month and the field name. Then choose to add a filter to that data by clicking the button Filter data. A new dialog opens up where we choose to Add a new filter.

In the first drop down choose select a field or group and point to id in the secondary data source. In the second drop down choose is equals to and in the final drop down choose select a field or group and point to MonthNr. Here you have all the dialogs:

Ok, so there it is! Now we have the name of the current month.

In the next post I will show you how I made the form keep the name of the valid month, even if it is opened up later and how I made the form editable only until a certain date.

CU!

/Niax