Created
April 17, 2018 21:47
-
-
Save tiagoduarte/066653f4d0560981d43cc34c8a0c5f56 to your computer and use it in GitHub Desktop.
Check for document lock and release the lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="locked-doc"> | |
<p> | |
<label>Document URL</label> | |
<br/> | |
<input id="docUrl" type="text" value="http://" style="width:500px" /> | |
</p> | |
<p> | |
<label>Website URL</label> | |
<br/> | |
<input id="webUrl" type="text" value="http://" style="width:500px" /> | |
</p> | |
<p> | |
<input type="button" value="UNLOCK" onclick="getLockForDoc();" /> | |
</p> | |
</div> | |
<script> | |
//https://pholpar.wordpress.com/2014/04/07/how-to-use-javascript-to-delete-short-term-locks-from-documents-opened-from-sharepoint | |
//https://sharepoint.stackexchange.com/questions/165970/can-we-unlock-a-shortterm-lock-via-csom-for-sp2013 | |
/* | |
Unlock document lock through javascript | |
USAGE: | |
UnlockFile.aspx?docUrl=http://contoso.com/lib/test.docx&webUrl=http://contoso.com | |
or | |
UnlockFile.aspx | |
*/ | |
//get a parameter from querystring by its name, empty if fails | |
function getParameterByName(name) { | |
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); | |
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), | |
results = regex.exec(location.search); | |
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); | |
} | |
// define String.format function | |
if (!String.prototype.format) { | |
String.prototype.format = function () { | |
var args = arguments; | |
return this.replace(/{(\d+)}/g, function (match, number) { | |
return typeof args[number] != 'undefined' | |
? args[number] | |
: match | |
; | |
}); | |
}; | |
} | |
function Properties(source) { | |
this.source = source; | |
this.offspring = []; | |
this.getPropValue = function (propName) { | |
var propValue = ""; | |
var propNameFullLine = '<li>' + propName + '\n'; | |
var startPos = this.source.indexOf(propNameFullLine); | |
if (startPos > -1) { | |
var endPos = this.source.substr(startPos + propNameFullLine.length).indexOf('\n'); | |
if (endPos > 0) { | |
propValue = this.source.substr(startPos + propNameFullLine.length, endPos); | |
// trim leading chars | |
// http://msdn.microsoft.com/en-us/library/office/ms460937(v=office.14).aspx | |
var strDummy = '<li>SW|'; | |
propValue = unescapeEx(propValue.substr(strDummy.length)); | |
} | |
} | |
return propValue; | |
} | |
// this function is intended to be accessible from the object itself (e.g. private) | |
var unescapeEx = function (value) { | |
var result = value.replace(/&#([0-9]|[1-9][0-9]|[[01][0-9][0-9]|2[0-4][0-9]|25[0-5]);/g, function (str, match) { return String.fromCharCode(match); }); | |
return result; | |
} | |
} | |
function getLockForDoc() { | |
var docUrl = $("#docUrl").val(); | |
var webUrl = $("#webUrl").val(); | |
if(docUrl === "" || webUrl === "") | |
{ | |
alert("Invalid parameters!"); | |
return; | |
} | |
var escapedDocUrl = encodeURI(docUrl); | |
$.ajax({ | |
url: webUrl + '/_vti_bin/_vti_aut/author.dll', | |
type: 'POST', | |
contentType: 'application/x-www-form-urlencoded', | |
headers: { | |
'MIME-Version': '1.0', | |
'User-Agent': 'MSFrontPage/14.0', | |
'Accept': 'auth/sicily', | |
'X-Vermeer-Content-Type': 'application/x-www-form-urlencoded' | |
}, | |
data: 'method=getDocsMetaInfo%3a14%2e0%2e0%2e6009&url%5flist=%5b' + escapedDocUrl + '%5d&listHiddenDocs=false&listLinkInfo=false', | |
complete: function (result) { | |
if ((result.readyState == 4) && (result.status == 200)) { | |
var rawResponse = result.responseText; | |
var startPos = rawResponse.indexOf('<li>meta_info=\n<ul>'); | |
if (startPos > 0) { | |
var endPos = rawResponse.substr(startPos).indexOf('</ul>'); | |
if (endPos > 0) { | |
var props = new Properties(rawResponse.substr(startPos, endPos)); | |
var lockId = props.getPropValue('vti_sourcecontrollockid'); | |
var checkedOutBy = props.getPropValue('vti_sourcecontrolcheckedoutby'); | |
var lockExpires = props.getPropValue('vti_sourcecontrollockexpires'); | |
if (lockId == "") { | |
alert("File is not locked."); | |
} | |
else { | |
if (confirm(String.format("File is locked by '{0}' until '{1}', LockId = '{2}'.\r\nUnlocking the file can cause problems if the user is still editing the file and would like to save it later.\r\nDo you want to clear the lock?", checkedOutBy, lockExpires, lockId))) { | |
releaseLock(escapedDocUrl, lockId); | |
} | |
} | |
} | |
} | |
} | |
} | |
}); | |
} | |
// message template to unlock a document | |
var releseLockReq = '\r\n'; | |
releseLockReq += '–urn:uuid:8cfcbb22-dd52-4889-b29d-9ff2dcf909b2\r\n'; | |
releseLockReq += 'Content-ID: <f13ad06d-8530-4af1-8cf3-d6d75c1635d4@tempuri.org>\r\n'; | |
releseLockReq += 'Content-Transfer-Encoding: 8bit\r\n'; | |
releseLockReq += 'Content-Type: application/xop+xml;charset=utf-8;type="text/xml; charset=utf-8"\r\n'; | |
releseLockReq += '\r\n'; | |
releseLockReq += '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><RequestVersion Version="2" MinorVersion="0" xmlns="http://schemas.microsoft.com/sharepoint/soap/"/><RequestCollection CorrelationId="{35E42C96-FE02-41FE-B4D8-F7DEC43AF784}" xmlns="http://schemas.microsoft.com/sharepoint/soap/"><Request Url="{0}" RequestToken="1"><SubRequest Type="ExclusiveLock" SubRequestToken="1"><SubRequestData ExclusiveLockRequestType="ReleaseLock" ExclusiveLockID="{1}"/></SubRequest></Request></RequestCollection></s:Body></s:Envelope>\r\n'; | |
releseLockReq += '–urn:uuid:8cfcbb22-dd52-4889-b29d-9ff2dcf909b2–\r\n'; | |
function releaseLock(escapedDocUrl, lockId) { | |
$.ajax({ | |
url: webUrl + '/_vti_bin/cellstorage.svc/CellStorageService', | |
type: 'POST', | |
contentType: 'multipart/related; type="application/xop+xml"; boundary="urn:uuid:8cfcbb22-dd52-4889-b29d-9ff2dcf909b2"; start="<f13ad06d-8530-4af1-8cf3-d6d75c1635d4@tempuri.org>"; start-Info="text/xml; charset=utf-8"', | |
headers: { | |
'MIME-Version': '1.0', | |
'User-Agent': 'Microsoft Office Upload Center 2010 (14.0.6124) Windows NT 6.1', | |
'SOAPAction': 'http://schemas.microsoft.com/sharepoint/soap/ICellStorages/ExecuteCellStorageRequest' | |
}, | |
data: String.format(releseLockReq, escapedDocUrl, lockId), | |
complete: function (result) { | |
var succeeded = false; | |
if ((result.readyState == 4) && (result.status == 200)) { | |
var rawResponse = result.responseText; | |
// check the result the primitive way | |
succeeded = (rawResponse.indexOf('ErrorCode="Success"') > -1); | |
} | |
if (succeeded) { | |
alert('Lock is released.'); | |
} else { | |
alert('An error has occured during the request. Lock is not released, try again later.'); | |
} | |
} | |
}); | |
} | |
//lets work | |
var webUrl = getParameterByName("webUrl"); | |
if(webUrl !== "") | |
$("#webUrl").val(webUrl); | |
var docUrl = getParameterByName("docUrl"); | |
if(docUrl !== "") | |
$("#docUrl").val(docUrl); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment