Hi,
Yes it is possible, please refer to the similar post for sample code.
Please don't forget to mark it as answered, if your problem resolved or helpful.
Hi,
Please refer to the following article.
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
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?
Hi,
Please refer to the following article.
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
Hi,
Please refer to the following article.
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
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?
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.
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.
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.
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.
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?