Thursday, 13 December 2012

ECMA Script to update today’s date in all list items in SharePoint 2010


1)      Create a column in the list with the “Date and Time” as its field type.
2)      Add the Content Editor Web Part & copy the below script
3)      Give your list name & list column name wherever required.
<script>
var clientContext;
var web = null;
    ExecuteOrDelayUntilScriptLoaded(UpdateList, 'sp.js');
    function UpdateList()
    {
     if (SP.ClientContext != undefined && SP.ClientContext != null) {
            clientContext = new SP.ClientContext.get_current();
            if (clientContext != undefined && clientContext != null) {
                    var siteColl =                     web = siteColl.get_rootWeb();
                    var list = web.get_lists().getByTitle('List Name');
                    var query = SP.CamlQuery.createAllItemsQuery();
                    this.listItems= list.getItems(query);
                    clientContext.load(this.listItems, 'Include(Column Name)');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess), Function.createDelegate(this, this. onListItemsLoadFailed));
       }
   }
}
   function onListItemsLoadSuccess()
    {
            var listEnumerator = this.listItems.getEnumerator();
            while(listEnumerator.moveNext())
            {
                var todaysDate = new Date();
                var oListItem = listEnumerator.get_current();
                var currentdate = oListItem.get_item('Column Name');
                oListItem.set_item('DueDate',todaysDate);
                oListItem.update();
           }
           clientContext.executeQueryAsync(Function.createDelegate(this, this.ListItemsUpdateSuccess), Function.createDelegate(this, this.ListItemsUpdateFailed));
  }

    function onListItemsLoadFailed ()
    {
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
    }

function ListItemsUpdateSuccess()
{
        alert('Success ');
}
function ListItemsUpdateFailed()
{
      alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}
 

</script>

2 comments:

  1. Hi, thank you for posting this, but I can't get it to work. I believe there are a couple of errors in the posted code which i tried to correct, but still no luck. I'm on SP2010.

    Here's my code as modified, and I'd appreciate it if you can take a look at it:
    <script type="text/javascript" language="javascript">
    var tDate = null;
    allItems = null;

    ExecuteOrDelayUntilScriptLoaded(UpdateList, 'sp.js'); //this is necessary to ensure the library is loaded before function triggered

    function DateThis(){
    tDate = new Date().toJSON();
    tDate.format("m/dd/yy");
    return tDate;
    }

    function UpdateList(){
    var tContext = new SP.ClientContext.get_current();
    var tSite = tContext.get_site();
    var tWeb = tSite.get_rootWeb();
    var tList = tWeb.get_lists().getByTitle('TeamDUCOM LNO Travel');
    var tQuery = SP.CamlQuery.createAllItemsQuery();
    this.allItems = tList.getItems(tQuery);
    tContext.load(allItems, 'Include(thisDate)');
    tContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySuccess), Function.createDelegate(this, this.onQueryFailed));
    }

    function onQuerySuccess(){
    var listEnum = allItems.getEnumerator();
    var todaysDate = DateThis();
    while(listEnum.moveNext()){
    var currentItem = listEnum.get_current();
    var origDate = currentItem.get_item('thisDate');
    currentItem.set_item('thisDate',todaysDate);
    currentItem.update();
    }
    tContext.executeQueryAsync(Function.createDelegate(this, this.ListItemsUpdateSuccess), Function.createDelegate(this, this.ListItemsUpdateFailed));
    }

    function onQueryFailed (){
    alert('Query List Failed ');
    }

    function ListItemsUpdateSuccess(){
    alert('Success ');
    }

    function ListItemsUpdateFailed(){
    alert('Update List Failed ');
    }
    </script>

    I tried the JSON snippet because from what I read, the JS date doesn't work with SP due to the ISOformat, but don't yet know if this works or not because I'm erroring out at "onQueryFailed".

    TAI
    AH-C

    ReplyDelete
    Replies
    1. TAI have you had any success with this yet? I've been trying working with the script from this site

      http://social.technet.microsoft.com/Forums/sharepoint/en-US/363d118f-3421-4fd1-ada4-2994f0a2a2dc/updating-a-date-column-in-sp2010-using-javascript-in-wp?forum=sharepointgeneralprevious

      Still without luck, either receiving load errors or nothing at all.

      Delete