Why SharePoint does not understand what today is?

Why SharePoint does not understand what today is?

SharePoint calculated fields can be a thing of beauty when you push it to the far reaches of what can be done. But one thing it does not do well is understand the concept of what 'Today' is. Whats that you say, 'I use it all kid of calculated fields calculations.' That is a true statement, but as many of you have found out the hard way, 'Today' is only updated when a record is created or updated. Meaning if I last updated a record back on 1 May 18, the field of 'Today' is still holding the value of 1 May 18 even though it is now 7 Sept 18. So if you need your calculation to be based off the current 'Today', then I may have a solution for you!!

So how do you get around this? Great question, now depending on your setup and can schedule a task to run each day at 1 am to update all instance of the field 'Today'. But what if you cannot do this because of the system configuration. Well good news, I wrote a nice JavaScript that can do this on page load (see below). Of course anything that cool has to have a draw back, and it does. When the code runs if a record is updated who ever loaded the page will be the person of last modified, and it will show as a version update as well.

Let me know if this helped any of you, or if you have a different solution to this problem. Please feel free to make any changes you think will make the code better and share it back to this post.

Now the code,


// First we have to load the required JS files
<script language="javascript" type="text/javascript" src="../jQuery-v3.3.1.min.js"></script>
<script language="javascript" type="text/javascript" src="../SPServices.min.js"></script>

<script type="text/javascript">
	var vDate = new Date();
	// sets to the proper date format
	var vCurrentDate = (vDate.getMonth()+1) + '/' + vDate.getDate() + '/' +  vDate.getFullYear();
	var siteUrl = '/sites/NAME';
	var listName = 'LIST_TO_UPDATE';
	var pageReloadB = false; //Will let the code know if the page needs to be reloaded to show update
	var listCount = 0;
	var loopCount = 0;

	// This well get all items in the list to update, can add a camlQuery to get a subset if needed
	function getListdata() {
		var clientContext = new SP.ClientContext(siteUrl);
	    var oList = clientContext.get_web().get_lists().getByTitle(listName);
	    var camlQuery = new SP.CamlQuery();
		camlQuery.set_viewXml(''); // empty query also works
		this.collListItem = oList.getItems(camlQuery);
		clientContext.load(collListItem);
		clientContext.executeQueryAsync(
			Function.createDelegate(this, this.onQueryLoadSucceeded),
			Function.createDelegate(this, this.onQueryLoadFailed)
		);
	}

	// This function will update the field, in this case the field is called 'CurrentDate' to today.
	function updateListItem(id) {
	    var clientContext2 = new SP.ClientContext(siteUrl);
	    var oList2 = clientContext2.get_web().get_lists().getByTitle(listName);
	    this.oListItem2 = oList2.getItemById(id);
		// Enter the column name of the field you want to update
	    oListItem2.set_item('CurrentDate', vCurrentDate);
	    oListItem2.update();
	    clientContext2.executeQueryAsync(Function.createDelegate(this, this.onQueryUpdateSucceeded), Function.createDelegate(this, this.onQueryUpdateFailed));
	}

	// This function is called after the list is loaded with results from function getListdata()
	function onQueryLoadSucceeded() {
		listCount = collListItem.get_count(); // How many items in the list

		var listItemInfo = '';
		var listItemEnumerator = collListItem.getEnumerator();
		//Loops through the entire list
		while (listItemEnumerator.moveNext()) {
		    var oListItem = listItemEnumerator.get_current();
		    // Gets item date and formats it for compare
		    var testCurrentDate = (oListItem.get_item('CurrentDate').getMonth()+1) + '/' + (oListItem.get_item('CurrentDate').getDate()+1) + '/' +  oListItem.get_item('CurrentDate').getFullYear();
		    // If date is today no need to update break loop and end
		    if(testCurrentDate == vCurrentDate){
				break;
			} else {
				// If list date is not equal to today, then update row in list
				updateListItem(oListItem.get_item('ID'));
				loopCount = loopCount + 1;
				pageReloadB = true;
		 	} //end if condition
    	}//End while loop
    	setTimeout(pageReload, 2000);

	}

	function pageReload() {
		//Will reload page if a update happened
		if (pageReloadB == true) {
			if(loopCount != listCount) {
				setTimeout(pageReload, 2000);
			} else {
				$(location).attr('href', window.location.href);
			}
		}
	}

	function onQueryLoadFailed(sender, args) {
		console.log("Load failed - " + vCurrentDate);
	}

	function onQueryUpdateSucceeded() {
		console.log("Update");
	}

	function onQueryUpdateFailed(sender, args) {
		console.log('update failed. ' + vCurrentDate + " - " + args.get_message() + '\n' + args.get_stackTrace());
	}

	SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getListdata);
</script>
 
  


To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics