Sharepoint: Calculate Today in Sharepoint 2010 using Javascript
How to calculate Today's Date in SharePoint 2010
Problem: Need a column in sharepoint that has updated today's date? This is an issue with sharepoint, there are couple of way going around this issue, its even more limited if you only have client side access to sharepoint.
Solution: Use client side (javascript) to update a column with today's date.
Note** javaScript is runtime script and so what this means, it will execute only when a user loads the page where the code exists.
Implementation: Refer to other blog post -- > Link
---------------------------------------------- Code ---------------------------------------------------------------
<script type="text/javascript">
var myListItemColelction;
var myListcontext;
//Delays function from running until sp.js loads
$(document).ready(function() {
ExecuteOrDelayUntilScriptLoaded(getMyListItems, "sp.js");
});
function getMyListItems()
{
try
{
myListcontext = new SP.ClientContext.get_current();
var web = myListcontext.get_web();
var myList = web.get_lists().getByTitle('[LIST NAME]');
var camlQuery = new SP.CamlQuery();
//this query is pulling all items in this list, if you want to pull specific items, please change query
camlQuery.set_viewXml("<view><query></Query></View>");
this.myListItemCollection = myList.getItems(camlQuery);
myListcontext.load(myListItemCollection);
myListcontext.executeQueryAsync(updateItems,failureMethod);
}
catch(exc)
{ console.log(exc.message);}
}
function updateItems(sender, args)
{
try
{
var listItemEnumerator = myListItemCollection.getEnumerator();
while(listItemEnumerator.moveNext())
{
var myListItem = listItemEnumerator.get_current();
var date = new Date();
var day = date.getDate();
day = day + 1;
var month = date.getMonth();
var year = date.getFullYear();
var fulldate = month+1 +'/'+ day + '/' + year;
//console.log(fulldate);
myListItem.set_item('Today', fulldate);
myListItem.update();
}
myListcontext.executeQueryAsync(ItemsUpdated, failureMethod);
}
catch (exc)
{ alert(exc.message);}
}
function ItemsUpdated(sender, args)
{
console.log('Items updated successfully');
}
function failureMethod(sender, args)
{
alert ('Request failed' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
---------------------------------------------------------------------------------------------------------------------
Solution: Use client side (javascript) to update a column with today's date.
Note** javaScript is runtime script and so what this means, it will execute only when a user loads the page where the code exists.
Implementation: Refer to other blog post -- > Link
---------------------------------------------- Code ---------------------------------------------------------------
<script type="text/javascript">
var myListItemColelction;
var myListcontext;
//Delays function from running until sp.js loads
$(document).ready(function() {
ExecuteOrDelayUntilScriptLoaded(getMyListItems, "sp.js");
});
function getMyListItems()
{
try
{
myListcontext = new SP.ClientContext.get_current();
var web = myListcontext.get_web();
var myList = web.get_lists().getByTitle('[LIST NAME]');
var camlQuery = new SP.CamlQuery();
//this query is pulling all items in this list, if you want to pull specific items, please change query
camlQuery.set_viewXml("<view><query></Query></View>");
this.myListItemCollection = myList.getItems(camlQuery);
myListcontext.load(myListItemCollection);
myListcontext.executeQueryAsync(updateItems,failureMethod);
}
catch(exc)
{ console.log(exc.message);}
}
function updateItems(sender, args)
{
try
{
var listItemEnumerator = myListItemCollection.getEnumerator();
while(listItemEnumerator.moveNext())
{
var myListItem = listItemEnumerator.get_current();
var date = new Date();
var day = date.getDate();
day = day + 1;
var month = date.getMonth();
var year = date.getFullYear();
var fulldate = month+1 +'/'+ day + '/' + year;
//console.log(fulldate);
myListItem.set_item('Today', fulldate);
myListItem.update();
}
myListcontext.executeQueryAsync(ItemsUpdated, failureMethod);
}
catch (exc)
{ alert(exc.message);}
}
function ItemsUpdated(sender, args)
{
console.log('Items updated successfully');
}
function failureMethod(sender, args)
{
alert ('Request failed' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
Comments
Post a Comment