Sharepoint 2010 - Modify Documents Properties with ECMA Script
Finally I found the way to modify document properties with ECMA Script!
It's not an hard work for Document Libraries with no mandatory check out.
Here the script:
But when we set check out mandatory, we have message that we cannot modify document not checked out.
So, I have to check out document in this script.
I change script as shown below:
And it works fine!
It's not an hard work for Document Libraries with no mandatory check out.
Here the script:
function updateDocument(action, docLibId, docId, fieldName, fieldValue) {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var docLibGuid = new SP.Guid(docLibId);
var docLib = web.get_lists().getById(docLibGuid);
var doc = docLib.getItemById(docId);
doc.set_item(fieldName, fieldValue);
doc.update();
context.executeQueryAsync(Function.createDelegate(this, this.successUpdateDocument), Function.createDelegate(this, this.failed));
}
function successUpdateDocument(sender, args) {
//reload page if success
location.reload(true);
}
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var docLibGuid = new SP.Guid(docLibId);
var docLib = web.get_lists().getById(docLibGuid);
var doc = docLib.getItemById(docId);
doc.set_item(fieldName, fieldValue);
doc.update();
context.executeQueryAsync(Function.createDelegate(this, this.successUpdateDocument), Function.createDelegate(this, this.failed));
}
function successUpdateDocument(sender, args) {
//reload page if success
location.reload(true);
}
But when we set check out mandatory, we have message that we cannot modify document not checked out.
So, I have to check out document in this script.
I change script as shown below:
function updateDocument(action, docLibId, docId, fieldName, fieldValue) {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var docLibGuid = new SP.Guid(docLibId);
var docLib = web.get_lists().getById(docLibGuid);
var doc = docLib.getItemById(docId);
//perform check out
var file = doc.get_file();
context.load(file);
file.checkOut();
//set values
doc.set_item(fieldName, fieldValue);
doc.update();
//then check in document
file.checkIn();
context.executeQueryAsync(Function.createDelegate(this, this.successUpdateDocument), Function.createDelegate(this, this.failed));
}
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var docLibGuid = new SP.Guid(docLibId);
var docLib = web.get_lists().getById(docLibGuid);
var doc = docLib.getItemById(docId);
//perform check out
var file = doc.get_file();
context.load(file);
file.checkOut();
//set values
doc.set_item(fieldName, fieldValue);
doc.update();
//then check in document
file.checkIn();
context.executeQueryAsync(Function.createDelegate(this, this.successUpdateDocument), Function.createDelegate(this, this.failed));
}
And it works fine!
Comments
Post a Comment