How to auto save a browser enabled info path form every X minutes in Sharepoint 2007, 2010 and 2013

Info Path forms can be saved automatically every X minutes by updating the FormServer.aspx file.
This update will avoid for users to lose their information when the session has expired and they forgot to save the form. The following code add some conditions so only the forms within a specific site and form view will be saved.
I recommend you to copy the FormServer.aspx file before start with the changes to facilitate the file recovery if it fails.

Update the FormServer.aspx file using a text editor as following:

 

If you are using SharePoint 2007:


Include before the  </head> tag  the following:

<script language="javascript" type="text/javascript">

var timer_is_on = 0;
var t;

function doTimer()
{
clearTimeout(t);
if (document.URL.indexOf("SiteName") != -1)
if (g_objCurrentFormData[9] != "FormViewToExclude" && PostbackBody.intPostbacksInProgress == 0)
if (!timer_is_on)
{
timer_is_on = 1;
timedCount();
}
else {clearTimeout(t);
timer_is_on = 0;
}
}
 
function timedCount() {
if (timer_is_on != 1 && PostbackBody.intPostbacksInProgress == 0 && g_objCurrentFormData[9] != "FormViewToExclude")
{
var elem = document.getElementById('FormControl');
elem.focus();
elem.childNodes[0].focus();
t = setTimeout("save()", 500);
}
timer_is_on = 2;
t = setTimeout("timedCount()", 540000);
}
function save()
{
if (timer_is_on != 1 && PostbackBody.intPostbacksInProgress == 0 && g_objCurrentFormData[9] != "FormViewToExclude")
EventLog_Add(14, null, "", false, false, true, false, false, 10, 0);
}
</script>

Now, we will call the function when the form is loading:

Look for the <body style="margin: 0px;overflow:auto;"> and add the onload expression to call the doTimer() function when the form is loading:
<body style="margin: 0px; overflow: auto;" onload="return doTimer();">

SiteName: The name of the site/subsite where your form is placed on.
FormViewToExclude: A view within the form that you want to exclude to be saved.

If you are using SharePoint 2010/2013:


Include before the  </head> tag  the following:

<script language="javascript" type="text/javascript">

var timer_is_on = 0;
var t;

function doTimer()
{
clearTimeout(t);
if (document.URL.indexOf("SiteName") != -1)
if (g_objCurrentFormData_FormControl[9] != "FormViewToExclude" && PostbackBody.intPostbacksInProgress == 0)
if (!timer_is_on)
{
timer_is_on = 1;
timedCount();
}
else {clearTimeout(t);
timer_is_on = 0;
}
}

function timedCount() {
if (timer_is_on != 1 && PostbackBody.intPostbacksInProgress == 0 && g_objCurrentFormData_FormControl[9] != "FormViewToExclude")
{
var elem = document.getElementById('FormControl');
elem.focus();
elem.childNodes[0].focus();
t = setTimeout("save()", 500);
}
timer_is_on = 2;
t = setTimeout("timedCount()", 540000);
}
function save()
{
if (timer_is_on != 1 && PostbackBody.intPostbacksInProgress == 0 && g_objCurrentFormData_FormControl[9] != "FormViewToExclude")
EventLog_Add('FormControl', 14, null, "", false, false, true, false, false, 10, 0);
}
</script>

Now, we will call the function when the form is loading:

Look for <body runat="server" id="PageBody"> and replace with:
<body  id="PageBody" onload="return doTimer();">

SiteName: The name of the site/subsite where your form is placed on.
FormViewToExclude: A view within the form that you want to exclude to be saved

Brief Explanation:

The doTimer() function will start when the form is loading if there are no savings in progress (PostbackBody.intPostbacksInProgress == 0 ) and only for the views which are not specifically excluded (g_objCurrentFormData[9] != "FormViewToExclude" ) in the specified site (document.URL.indexOf("SiteName") != -1).
 If the above conditions are met the counter will start (TimedCount()) and will call the Save() function every 540000ms(9 minutes). You can change the miliseconds according to your preferences.
The EventLog_Add(14, null""falsefalsetruefalsefalse, 10, 0) will save the form as the Toolbar.ExecuteAction does. You can find more details about how the EventLog_Add() is using within info path forms in the core.js file (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\1XX\TEMPLATE\LAYOUTS\INC)





Note: If you need to check in the form after auto save and close the form, just include the following before the <script language="javascript" type="text/javascript"> line code:

<script type="text/javascript" language="javascript" src="/Javascript/jquery-1.4.3.min.js"></script >
    <script type="text/javascript" language="javascript" src="/Javascript/jquery.SPServices-0.6.2.min.js"></script >

Then include the following after the EventLog_Add('FormControl', 14, null, "", false, false, true, false, false, 10, 0); line code:
                     
var filename = g_objCurrentFormData_FormControl[12];
$().SPServices({
operation: "GetListItems",
async: false,
listName: "ListName",
CAMLViewFields: "<ViewFields Properties='True' />",
CAMLQuery: "<Query><Where><Eq><FieldRef Name='InternalFieldName' /><Value Type='FieldType'>" +
filename + "</Value></Eq></Where></Query>",
completefunc: function(xData, Status) {
$(xData.responseXML).find("[nodeName='z:row']").each(function() {
var docURL = $(this).attr("ows_EncodedAbsUrl");                 
$().SPServices({
operation: "CheckInFile",
async: false,
pageUrl: docURL,
comment: "YourComment",
CheckinType: 1                          
});
});
}
});

window.location = 'RedirectLocation';     

ListName: The name of the list where your form is saved.
InternalFieldName: The internal name field within the form library that displays the name of your form.
FieldType: The field type of the InternalFiedlName
YourComment: Comment you want to include in your check in comments box.
RedirectLocation: The location to be redirected after the form is closed (exclude the server name). i.e:
/Subsite/Home.aspx

Now, go to the SharePoint root and create a Document Library called Javascript (if you already do not have it), and put there the "jquery-1.4.3.min" and the "jquery.SPServices-0.6.2.min".

You can download them form here (select minified):
http://blog.jquery.com/2010/10/16/jquery-143-released/
http://cdiscportal.digitalinfuzion.com/CT/Scripts/Forms/AllItems.aspx

Take into account that if you have more than one front-end server, you will have to do it in all the servers.







Comments

  1. Hi Leire , can you please help me to implement the same for InfoPath 2010 form

    ReplyDelete
  2. Hi Ashok, I have just updated the Post, so this solution should apply now to Info Path 2007 and 2010. Notice that no message will appear, the form will just save without any confirmation message

    ReplyDelete
    Replies
    1. Hi Leire thanks for updating the post. It is working for me. Thanks a ton !!!

      I have another question is it possible to check in the saved form to document library? If yes can you please guide me for the same ?

      Delete
    2. Yes, it is possible, I will update the post the next week, so do you want to check in the form after the auto save? and also close the form?

      Delete
    3. yes this is what I am looking for. It should work for both new form as well as existing form

      Delete
  3. It worked!! Thank you!! Thank you!!

    ReplyDelete
  4. Hi Leire, If I change it's going to effect entire form, Is it possible to modify the FormServer.aspx only for one web app ?

    ReplyDelete
  5. No, but using the "if" condition (if (document.URL.indexOf("SiteName") != -1)) you discriminate by site and by form view (if (g_objCurrentFormData_FormControl[9] != "FormViewToExclude" ) if you need it.

    ReplyDelete
    Replies
    1. Thanks, but am displaying info path using custom XmlFormView webpart autosave didn't work .. any suggestions ?

      Delete
    2. It worked but this timer job run for all user irrespective of permission, how to prevent this ?

      Delete
  6. Hi ,
    I am getting issue in below line while tried implementing this article.

    EventLog_Add('FormControl', 14, null, "", false, false, true, false, false, 10, 0);
    The error which system throughs is :
    Uncaught ReferenceError: EventLog_Add is not defined

    I am working in sharepoint 2013 ADFS based environment with web based infopath form.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to convert fields in lower case/upper case within Info path forms

Auto-populate the person/Group picker from the current user on InfoPath 2007/2010/2013