I'm running into an issue where I'm trying to modify a metadata field on a document within a document library. My code, will check out the document, update the metadata field and then check in the document. When I execute this code for the first time everything works fine, but when I run the code after that I get the version conflict error.
Please help...
Here is my code:
function SaveDocumentRequiredSignatureChange(ListItemId, AttachmentLibraryName, ckbControl, FileUrl) { //debugger; var SignatureRequired = (ckbControl.checked == true) ? '1' : '0'; spCtx = new SP.ClientContext.get_current(); spList = spCtx.get_web().get_lists().getByTitle(AttachmentLibraryName); attachedDocumentUrl = FileUrl; attachDocument = spCtx.get_web().getFileByServerRelativeUrl(attachedDocumentUrl); attachDocument.checkOut(); spCtx.load(attachDocument); spCtx.executeQueryAsync(); var spListItem = spList.getItemById(ListItemId); spListItem.refreshLoad(); spCtx.load(spListItem); spCtx.executeQueryAsync(); var workItem = spListItem; workItem.set_item('SignatureRequired', SignatureRequired); workItem.update(); spCtx.load(workItem) //attachDocument.checkIn("File Update: Signature Required was modified.", 2); //spCtx.load(attachDocument); spCtx.executeQueryAsync(Function.createDelegate(this, this.onRequireSignatureQuerySucceeded), Function.createDelegate(this, this.onRequireSignatureQueryFailed)) } /* onRequireSignatureQuerySucceeded() */ function onRequireSignatureQuerySucceeded() { spCtx = new SP.ClientContext.get_current(); var attachDocument = spCtx.get_web().getFileByServerRelativeUrl(attachedDocumentUrl); attachDocument.checkIn(); spCtx.load(attachDocument); spCtx.executeQueryAsync(); } function onRequireSignatureQueryFailed(sender, args) { alert("Your request failed. Why? \n" + args.get_message() + "\n More Info: " + args.get_stackTrace()); }
I'm still getting the error and a new one...The file is checked out to the current logged in user. I added a rest call to get the hidden version number and added one to the count.Here is my code updates:
function SaveDocumentRequiredSignatureChange(ListItemId, AttachmentLibraryName, ckbControl, FileUrl) { debugger; var SignatureRequired = (ckbControl.checked == true) ? '1' : '0'; spCtx = new SP.ClientContext.get_current(); spList = spCtx.get_web().get_lists().getByTitle(AttachmentLibraryName); attachedDocumentUrl = FileUrl; attachDocument = spCtx.get_web().getFileByServerRelativeUrl(attachedDocumentUrl); attachDocument.checkOut(); spCtx.load(attachDocument); spCtx.executeQueryAsync(); spListItem = spList.getItemById(ListItemId); spCtx.load(spListItem); spCtx.executeQueryAsync(); spListItem.set_item('SignatureRequired', SignatureRequired); spListItem.set_item('owshiddenversion', GetCurrentVersion(ListItemId)); spListItem.update(); spCtx.executeQueryAsync(Function.createDelegate(this, this.onRequireSignatureQuerySucceeded), Function.createDelegate(this, this.onRequireSignatureQueryFailed))
}
/* onRequireSignatureQuerySucceeded() */ function onRequireSignatureQuerySucceeded() { spCtx = new SP.ClientContext.get_current(); attachDocument = spCtx.get_web().getFileByServerRelativeUrl(attachedDocumentUrl); attachDocument.checkIn("File Update: Signature Required was modified.", 0); spCtx.load(attachDocument); spCtx.executeQueryAsync(); } function onRequireSignatureQueryFailed(sender, args) { alert("Your request failed. Why? \n" + args.get_message() + "\n More Info: " + args.get_stackTrace()); }
function GetCurrentVersion(ListItemId) { var versionCount = 0; var listServiceUrl = _spPageContextInfo.webServerRelativeUrl + "/_vti_bin/ListData.svc/DocLib?&$filter=Id eq " + ListItemId + "";
$.getJSON(listServiceUrl, { format: "json" }) .done(function (data) { $.each(data.d.results, function (i, result) { versionCount = result.Owshiddenversion; }); } ) .fail(function () { alert("error") }); return versionCount;
}
I've updated the code, but I'm still getting error. I thought by calling the getItemById a second time I hope that issue would go away, but it didn't. Please help:
var attachedDocumentUrl = null; function SaveDocumentRequiredSignatureChange(FileUrl, ListItemId, LibraryName, CkbCtrl) {
var SignatureRequired = (CkbCtrl.checked == true) ? '1' : '0'; spCtx = new SP.ClientContext.get_current(); debugger; attachedDocumentUrl = FileUrl attachDocument = spCtx.get_web().getFileByServerRelativeUrl(attachedDocumentUrl); attachDocument.checkOut(); spCtx.load(attachDocument); spCtx.executeQueryAsync(); var spList = spCtx.get_web().get_lists().getByTitle(LibraryName); var spListItem = spList.getItemById(ListItemId); spCtx.load(spListItem); spCtx.executeQueryAsync(); //var versionCount = spListItem.get_item('owshiddenversion'); //spCtx.executeQueryAsync(); //this.spListItem.set_item('owshiddenversion', versionCount + 1); spListItem.set_item('SignatureRequired', SignatureRequired); spListItem.update(); spCtx.executeQueryAsync(); spListItem = spList.getItemById(ListItemId); spCtx.load(spListItem); spCtx.executeQueryAsync(Function.createDelegate(this, this.onRequireSignatureQuerySucceeded), Function.createDelegate(this, this.onRequireSignatureQueryFailed)) } /* onRequireSignatureQuerySucceeded() */ function onRequireSignatureQuerySucceeded() { debugger; spCtx = new SP.ClientContext.get_current(); attachDocument = spCtx.get_web().getFileByServerRelativeUrl(attachedDocumentUrl); attachDocument.checkIn("File Update: Signature Required was modified.", 0); spCtx.load(attachDocument); spCtx.executeQueryAsync(); } function onRequireSignatureQueryFailed(sender, args) { alert("Your request failed. Why? \n" + args.get_message() + "\n More Info: " + args.get_stackTrace()); }