Change 'Created' column/field value
I have a business requirement of changing the date of 'created' column/field of selected document/s in a document library(it has to be changed to current system date). This has to be done by adding a custom button to the ribbon and providing authorization to the button. Please let me know if this is possible?
February 12th, 2015 12:14pm

Hi,

Yes it is possible, please refer to the similar post for sample code.

https://social.technet.microsoft.com/Forums/office/en-US/f391f28f-86a0-4836-8fdd-16fb64aaf687/sharepoint-list-created-column-value-change-manually?forum=sharepointadminlegacy

Please don't forget to mark it as answered, if your problem resolved or helpful.

Free Windows Admin Tool Kit Click here and download it now
February 12th, 2015 12:18pm

Hi,

Please refer to the following article.

http://www.learningsharepoint.com/2011/06/13/using-javascript-js-file-in-commandaction-of-custom-ribbon-button-sharepoint-2010/

In the above article, command action is JavaScript function. For example.

<CommandUIHandler
Command=AboutButtonCommand
CommandAction=javascript:ChangeDate();/>

Create a script file and refer to your command action.

//Referencing the Script
<CustomAction
Id=AboutFilesButton.Script
Location=ScriptLink
ScriptSrc =/_layouts/RibbonScripts/ChangeDate.js/>

You can add the following function to the "ChangeDate.js" file.

function ChangeDate()
{
  var ctx = new SP.ClientContext.get_current();
      var list = ctx.get_web().get_lists().getByTitle("CM");
var items = SP.ListOperation.Selection.getSelectedItems(ctx);
      var item = list.getItemById(items[0].id); 
      item.set_item("Created", "2/13/2015"); //change the date
      item.update();
      ctx.executeQueryAsync(
         function () {alert("Created date changed") },
      function () {alert ("Error")}
         );
}

Hope this works for you.

Please don't forget to mark it as answered, if your problem resolved or helpful.

  • Proposed as answer by UravaRamesh Friday, February 13, 2015 6:03 AM
  • Marked as answer by SP_M Wednesday, February 18, 2015 11:12 AM
February 13th, 2015 5:56am

This code cannot be implemented on a Custom button, right?

I have added a custom button to the ribbon (element.xml) which uses javascript.

I don't want to change the created date of all the documents in the document library, only the one which are checked.

are there any different ways to implement this?

Free Windows Admin Tool Kit Click here and download it now
February 13th, 2015 8:37am

Hi,

Please refer to the following article.

http://www.learningsharepoint.com/2011/06/13/using-javascript-js-file-in-commandaction-of-custom-ribbon-button-sharepoint-2010/

In the above article, command action is JavaScript function. For example.

<CommandUIHandler
Command=AboutButtonCommand
CommandAction=javascript:ChangeDate();/>

Create a script file and refer to your command action.

//Referencing the Script
<CustomAction
Id=AboutFilesButton.Script
Location=ScriptLink
ScriptSrc =/_layouts/RibbonScripts/ChangeDate.js/>

You can add the following function to the "ChangeDate.js" file.

function ChangeDate()
{
  var ctx = new SP.ClientContext.get_current();
      var list = ctx.get_web().get_lists().getByTitle("CM");
var items = SP.ListOperation.Selection.getSelectedItems(ctx);
      var item = list.getItemById(items[0].id); 
      item.set_item("Created", "2/13/2015"); //change the date
      item.update();
      ctx.executeQueryAsync(
         function () {alert("Created date changed") },
      function () {alert ("Error")}
         );
}

Hope this works for you.

Please don't forget to mark it as answered, if your problem resolved or helpful.

  • Proposed as answer by UravaRamesh Friday, February 13, 2015 6:03 AM
February 13th, 2015 8:56am

Hi,

Please refer to the following article.

http://www.learningsharepoint.com/2011/06/13/using-javascript-js-file-in-commandaction-of-custom-ribbon-button-sharepoint-2010/

In the above article, command action is JavaScript function. For example.

<CommandUIHandler
Command=AboutButtonCommand
CommandAction=javascript:ChangeDate();/>

Create a script file and refer to your command action.

//Referencing the Script
<CustomAction
Id=AboutFilesButton.Script
Location=ScriptLink
ScriptSrc =/_layouts/RibbonScripts/ChangeDate.js/>

You can add the following function to the "ChangeDate.js" file.

function ChangeDate()
{
  var ctx = new SP.ClientContext.get_current();
      var list = ctx.get_web().get_lists().getByTitle("CM");
var items = SP.ListOperation.Selection.getSelectedItems(ctx);
      var item = list.getItemById(items[0].id); 
      item.set_item("Created", "2/13/2015"); //change the date
      item.update();
      ctx.executeQueryAsync(
         function () {alert("Created date changed") },
      function () {alert ("Error")}
         );
}

Hope this works for you.

Please don't forget to mark it as answered, if your problem resolved or helpful.

  • Proposed as answer by UravaRamesh Friday, February 13, 2015 6:03 AM
Free Windows Admin Tool Kit Click here and download it now
February 13th, 2015 8:56am

Thanks for your reply.

I was not able to run this code on Ribbon Control button. However, it ran perfectly on a CEWP.

1: can you tell me how to make it run on the Ribbon Button?

2: in this code, the date is hard coded. I want the code the take the system date and time and same has to be applied to the 'Created' field.

To do that I added the below code snippet: and updated " item.set_item("Created", "datetime") ".

var currentdate = new Date(); 
var datetime =    (currentdate.getMonth()+1) + "/"
                + currentdate.getDate()  + "/" 
                + currentdate.getFullYear() + "  "  
                + currentdate.getHours() + ":"  
                + currentdate.getMinutes() 

But it doesn't work. Any Suggestions?

February 16th, 2015 5:52am

Hi,

I think you are adding quotes to the following code. Please verify.

item.set_item("Created", "datetime")

it should be variable name instead of string.

item.set_item("Created", datetime);

Please don't forget to mark it as answered, if your problem resolved or helpful.

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

Opps! you were correct.

It updated the the date, but it didn't update the time.

the getHour() method returns the value in 24 hours time format and SharePoint shows the time is 12 hours format. 


February 16th, 2015 6:18am

Hi,

Yes, SharePoint stores the time in 12 hour format, it uses AM and PM.

So, you need to write some custom function to convert the time in 12 hour format with AM/PM.

Please don't forget to mark it as answered, if your problem resolved or helpful.

Free Windows Admin Tool Kit Click here and download it now
February 16th, 2015 7:07am

Thanks for your Help!

No need to write a custom code to convert the date and time to 12 hours format.

" item.set_item("Created", currentdate) ". this will do the trick.

Javascript's Date() Method gets the system time as per the time zone and SharePoint automatically converts it to 12 hours format.

February 18th, 2015 11:20am

Hi,

Over here, the list name is hard codded.

var list = ctx.get_web().get_lists().getByTitle("CM");

Is there a way where the code can take the list name dynamically?

Free Windows Admin Tool Kit Click here and download it now
March 18th, 2015 3:25am

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

Other recent topics Other recent topics