Aidan Garnish

Collaboration Not Competition

Using JQuery to round averages in a SharePoint view

I had a question from a user asking whether it was possible to round the Average that is displayed on SharePoint views to 2.d.p.

Out of the box this is not possible but with a little bit of JQuery we can find the average and round it to the required d.p.

<script src="jquery.js"></script>
<script>
$(document).ready(function(){
   $('b').each(function(index){
   var value = $(this).html();
   var num = value.replace('Average = ','');
   var result = Math.round(num*Math.pow(10,2))/Math.pow(10,2);
   $(this).replaceWith('<b>Average = '+result+'</b>');
   });
});
</script>

Dynamically change InfoPath submit connection properties

If you want to change the properties of the data connection that your InfoPath form is using on submit you can do this in code. You might want to do this if you have lots of sites that use the same structure and each site contains a library that holds a certain type of form. Eg. A project site contains a library for project changes that are submitted using an InfoPath form. Each project site has a project changes library but you don't want to have to create a new project change form with a different submit connection for every site.

In the submit event use the following for SharePoint 2007

SPWeb web = SPContext.Current.Web;
FileSubmitConnection dc = (FileSubmitConnection)DataConnections["Submit"];
dc.FolderUrl = web.Url + "/[library name]";
dc.Execute();

e.CancelableArgs.Cancel = false;

In SharePoint 2010 there is also the option of using the ServerInfo class that has a property called SharePointListUrl to provide the current list context which can be used as follows

FileSubmitConnection dc = (FileSubmitConnection)DataConnections["Submit"];
dc.FolderUrl = ServerInfo.SharePointListUrl;
dc.Execute();
e.CancelableArgs.Cancel = false;