To update Created, Created By, Modified, Modified in SharePoint document library/list using Client Object Model -CSOM
The changes to the "Author" and "Editor" metadata are done under an user account with "site owner" privileges. That is the app pool's custom user account.
<script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
// When the body is loaded, the onload event handler executes each function whose name is contained in this array.
_spBodyOnLoadFunctionNames.push("callCSOM");
function callCSOM()
{
$("#Button1").click(function()
{
// Make sure the SharePoint script file 'sp.js' is loaded before your code runs.
ExecuteOrDelayUntilScriptLoaded(updateUser, "sp.js");
});
}
function updateUser()
{
var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
var currentUser = web.ensureUser('User Name here');
clientContext.load(web);
clientContext.load(currentUser);
clientContext.executeQueryAsync(
function () {
// now you have the user id and correct login name, update author
var userField = currentUser.get_id() + ";#" + currentUser.get_loginName();
updateListItem(userField);
//alert(currentUser.get_title());
},
function (sender, args) {
console.log('Error retrieving user properties');
}
);
}
function updateListItem(userField) {
var siteUrl = 'https://sharepoint2013/sites/PnP';
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('List/library Name');
//Item ID
var oListItem = oList.getItemById(4);
//oListItem.set_item('TriageLookup', '');
oListItem.set_item('Deleted', 'No');
oListItem.set_item('Modified', '05-30-2019');
oListItem.set_item('Editor', userField);
oListItem.update();
clientContext.executeQueryAsync(
function (sender, args) {
alert('Item updated!');
},
function (sender, args) {
alert('Request failed, Updating new list item');
}
);
}
</script>
<input id="Button1" type="button" value="Run Code"/>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
// When the body is loaded, the onload event handler executes each function whose name is contained in this array.
_spBodyOnLoadFunctionNames.push("callCSOM");
function callCSOM()
{
$("#Button1").click(function()
{
// Make sure the SharePoint script file 'sp.js' is loaded before your code runs.
ExecuteOrDelayUntilScriptLoaded(updateUser, "sp.js");
});
}
function updateUser()
{
var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
var currentUser = web.ensureUser('User Name here');
clientContext.load(web);
clientContext.load(currentUser);
clientContext.executeQueryAsync(
function () {
// now you have the user id and correct login name, update author
var userField = currentUser.get_id() + ";#" + currentUser.get_loginName();
updateListItem(userField);
//alert(currentUser.get_title());
},
function (sender, args) {
console.log('Error retrieving user properties');
}
);
}
function updateListItem(userField) {
var siteUrl = 'https://sharepoint2013/sites/PnP';
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('List/library Name');
//Item ID
var oListItem = oList.getItemById(4);
//oListItem.set_item('TriageLookup', '');
oListItem.set_item('Deleted', 'No');
oListItem.set_item('Modified', '05-30-2019');
oListItem.set_item('Editor', userField);
oListItem.update();
clientContext.executeQueryAsync(
function (sender, args) {
alert('Item updated!');
},
function (sender, args) {
alert('Request failed, Updating new list item');
}
);
}
</script>
<input id="Button1" type="button" value="Run Code"/>
Comments
Post a Comment