Run Script on SharePoint List InfoPath Form

Hello everyone,

I customized a SharePoint list form using InfoPath, and I added a CEWP to run some JQuery code. The code runs fine, but the problem is that the InfoPath form performs some partial postback, and this breaks the script.

Does anybody know if there's a way I can keep that code alive, or running, every time the form performs a postback?

Here's my code:

<script language="javascript" src="/reqs/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script language="javascript" src="/reqs/Scripts/jquery.SPServices-0.7.1a.min.js" type="text/javascript"></script>
<script language="javascript" src="/reqs/Scripts/jquery-ui-1.8.22.custom.min.js" type="text/javascript"></script>
<link href="/reqs/Scripts/jquery-ui-1.8.22.custom.css" rel="stylesheet" type="text/css"/>
<script language="javascript" type="text/javascript">
//Wait ##### seconds for form to load before calling the function.
window.onload = function(){
	window.setTimeout(readyCall, 1000);
}
function readyCall(){
	
	//Variable for Array where Title column values will be pushed to.
	var Requestors = [];
	
	//Variable for the Requestor Text Field.
	var RequestorField = "input[id$='FormControl0_V1_I1_T2']"
	
	//Call the SPServices library.
	$().SPServices({
		operation: "GetListItems",
		listName: "Requestor",
		CAMLViewFields: "",
		async: false,
		completefunc: function (xData, Status){
			$(xData.responseXML).SPFilterNode("z:row").each(function(){
				Requestors.push($(this).attr("ows_Title"));
			});
		}
	});
	
	$(RequestorField).autocomplete({
		source: Requestors,
		minLength: 2,
		focus: function(event, ui){
			$(RequestorField).val(ui.item.value);
			return false;
		}
	});
}
</script>

August 23rd, 2012 11:35pm

Hi!

I'd recommand to use the jQuery "onLoad" function:

$(document).ready(function(){
  window.setTimeout(readyCall, 1000);
});

Why are you using "setTimeout". It's only calles once!! You may use "setInterval" on the window object. It calls the specified method every x milliseconds.

http://www.w3schools.com/jsref/met_win_setinterval.asp

Regards
Ingo

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2012 2:02am

Hi Ingo,

Thanks for your suggestion, however, there's a specific issue when using the JQuery "onLoad" function. Here's the reference to that issue along with the link:

"When InfoPath forms load in the browser, they dont load with the rest of the page, but instead they are loaded slightly afterward in what amounts to an asynchronous load. Because of that, using $(document).ready(), our trusted jQuery friend, doesnt work. Instead, as you can see in lines 1-3, we simply wait for 1000 milliseconds (1 second) before we run our script. We found that this was an adequate amount of wait time for our particular form; you might need to adjust this."

Using SPServices with jQueryUIs Autocomplete Function on InfoPath Forms in SharePoint

As I mentioned above, the problem I have is the repeated postbacks made by InfoPath, which only let's my script run once. I am looking for a way to re-bind it every time InfoPath performs a postback.

Thanks

August 24th, 2012 4:33pm

Hi Fausto,

are you able to figure out what JavaScript function is used for the postback? You could override the function to activate your timer. Inside the function you call the overridden / original function.

Or you could execute your function every 1000 seconds using "setInterval" and try to find a flag that indicates a postback. 

That are ways I would try to go. I've no other ideas. - May be someone else.

Ingo

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2012 7:50pm

Hi Ingo,

The problem is that this is a SharePoint list form customized using InfoPath. This form has connections coming from the list, and it performs a few postbacks as it retrieves info from the list it serves, such as cascading dropdown controls, and a text field used to perform the AutoComplete feature shown in the code I provided.

August 24th, 2012 9:05pm

Hi Fausto,

I also had the same issue. add the "window.setTimeout(readyCall, 1000);" statement before end of the "readyCall" function. so that in case of any InfoPath partial postbacks the "readyCall" function will be called. It resolved my issue which is like your issue.

Regards,
Janaiah


  • Edited by Janaiah Putta Wednesday, September 12, 2012 2:18 PM make the solution clear
  • Marked as answer by Fausto Capellan, Jr Thursday, September 13, 2012 7:51 PM
Free Windows Admin Tool Kit Click here and download it now
September 12th, 2012 5:17pm

Hi Janaiah,

I tried your suggestion and it worked great. Thanks a lot for that.

September 13th, 2012 10:51pm

HI , i have the same issue and thanks to the solution of Janaiah it worked , but the form became very very slowly , how can we avoid this ? thanks for advance 
Free Windows Admin Tool Kit Click here and download it now
November 21st, 2012 4:34pm

Hello,

I noticed something silimar, but it was with Firefox users. You need to play around with the timeout settings within the function, not within the window.onload. I had to settle for a 15 seconds timeout. See code below:

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

//Wait ##### seconds for form to load before calling the function.
window.onload = function(){
	window.setTimeout(readyCall, 1000);
}

function readyCall(){
	
	//Variable for Array where Title column values will be pushed to.
	var Requestors = [];
	
	//Variable for the Requestor Text Field.
	var RequestorField = "input[id$='FormControl0_V1_I1_T2']"
	
	//Call the SPServices library.
	$().SPServices({
		operation: "GetListItems",
		listName: "Requestor",
		CAMLViewFields: "",
		async: false,
		completefunc: function (xData, Status){
			$(xData.responseXML).SPFilterNode("z:row").each(function(){
				Requestors.push($(this).attr("ows_Title"));
			}); //Close .each(function())
		} //Close function (xData, Status)
	}); //Close SPServices
	
	//Input field from form.
	$(RequestorField).autocomplete({
		source: Requestors,
		minLength: 3,
		focus: function(event, ui){
			$(RequestorField).val(ui.item.value);
			return false;
		}		
	}); //Close autocomplete
	
	window.setTimeout(readyCall, 15000);
} //Close readyCall function
</script>

 
November 21st, 2012 5:32pm

Merci beaucoup pour votre aide , ca marche avec 15 secondes !
Free Windows Admin Tool Kit Click here and download it now
November 21st, 2012 6:00pm

Vous tes les bienvenus, content d'avoir pu aider!!!
November 21st, 2012 6:11pm

Merci Fausto , avez vous une ide comment avoir les  valeurs "BeginsWith" n'est pas "Contains" ? j'ai ajout le filterType='BeginsWith' mais ca n'as pas march avez vous une ide ? Merci
Free Windows Admin Tool Kit Click here and download it now
December 26th, 2012 5:02pm

Est-ce pour la saisie semi-automatique ou une vue de liste?
December 26th, 2012 5:23pm

C'est pour les valeurs affiches dans la liste lorsque on tape les deux premires Lettres , il affiche les valeurs contenants les lettres alors que je veux afficher juste ceux qui dbutent par ces deux lettres . 

Merci

Free Windows Admin Tool Kit Click here and download it now
December 26th, 2012 5:38pm

J'ai regard autour de cela, mais je ne pouvais pas le trouver. Peut-tre que vous voulez en savoir plus sur jquery autocomplete, et voir si cela peut tre fait. C'est le lien pour jquery autocomplete:
JQuery AutoComplete
December 26th, 2012 6:53pm

Hi 

I am trying to retrieve some user info on a list customised with InfoPath. The problem is, it runs fine outside of InfoPath, but once customised, the script doesnt work. I have tried the fix above but no luck.

My code is:


<script src="../Shared%20Documents/jquery-1.11.2.min.js" type="text/javascript"></script>
<script src="../Shared%20Documents/jquery.SPServices-2014.02.min.js" type="text/javascript"></script>

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

    //Wait ##### seconds for form to load before calling the function.
window.onload = function(){
    window.setTimeout(readyCall, 1000);
}
    $(document).ready(function() {
 
        //  Set the 'Title' input field to the current user's "Title" property
        var userName = $().SPServices.SPGetCurrentUser({
            fieldName: "Title"
        });
        $("input[Title='Title']").val(userName);
 
        //  Set the 'User Name' input field to the current user's "UserName" property
        var userName = $().SPServices.SPGetCurrentUser({
            fieldName: "UserName"
        });
        $("input[Title='User Name']").val(userName);
 
        //  Set the 'Last Name' input field to the current user's "LastName" property
        var lastName = $().SPServices.SPGetCurrentUser({
            fieldName: "LastName"
        });
        $("input[Title='Last Name']").val(lastName);
 
        //  Set the 'First Name' input field to the current user's "FirstName" property
        var firstName = $().SPServices.SPGetCurrentUser({
            fieldName: "FirstName"
        });
        $("input[Title='First Name']").val(firstName);

        //  Set the 'Department' input field to the current user's "Department" property
        var userDepartment = $().SPServices.SPGetCurrentUser({
            fieldName: "Department"
        });
        $("input[Title='Department']").val(userDepartment);
 
    });
</script>

Thanks!

Free Windows Admin Tool Kit Click here and download it now
February 27th, 2015 5:08am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics