{"id":1862,"date":"2017-11-20T11:37:03","date_gmt":"2017-11-20T10:37:03","guid":{"rendered":"https:\/\/blog.kodono.info\/wordpress\/?p=1862"},"modified":"2018-12-03T12:24:45","modified_gmt":"2018-12-03T11:24:45","slug":"how-to-delete-a-document-locked-by-another-user-in-sharepoint-using-javascript","status":"publish","type":"post","link":"https:\/\/blog.kodono.info\/wordpress\/2017\/11\/20\/how-to-delete-a-document-locked-by-another-user-in-sharepoint-using-javascript\/","title":{"rendered":"How to delete a document locked by another user in Sharepoint using JavaScript"},"content":{"rendered":"<p>When you open a file from Sharepoint, it will receive a short term lock that will prevent others to change some properties on the file.<\/p>\n<p>This protection can be useful, but also very annoying, for example when the file is not closed properly, then the lock could stay &#8220;forever&#8221;.<\/p>\n<p>There are <a href=\"https:\/\/sharepoint.stackexchange.com\/questions\/42999\/clearing-short-term-file-lock\">many<\/a> <a href=\"https:\/\/sharepoint.stackexchange.com\/questions\/120246\/how-to-delete-a-document-locked-by-another-user-im-a-super-admin\">posts<\/a> <a href=\"https:\/\/sharepoint.stackexchange.com\/questions\/165970\/can-we-unlock-a-shortterm-lock-via-csom-for-sp2013\">on<\/a> <a href=\"https:\/\/sharepoint.stackexchange.com\/questions\/202665\/the-file-is-locked-for-exclusive-use-by-same-person-sharepoint-online\">the web<\/a> about it.<\/p>\n<p>I found one that has been super useful: <a href=\"https:\/\/pholpar.wordpress.com\/2014\/04\/07\/how-to-use-javascript-to-delete-short-term-locks-from-documents-opened-from-sharepoint\/\">https:\/\/pholpar.wordpress.com\/2014\/04\/07\/how-to-use-javascript-to-delete-short-term-locks-from-documents-opened-from-sharepoint\/<\/a><br \/>\nThe author explains very well the different steps what I&#8217;m trying to summarize:<\/p>\n<ol>\n<li>Send a request to <code>_vti_bin\/_vti_aut\/author.dll<\/code> with special headers\/body<\/li>\n<li>Auth.dll will provide the <code>lockid<\/code><\/li>\n<li>Send a request to <code>_vti_bin\/cellstorage.svc\/CellStorageService<\/code> with special headers\/body, included the <code>lockid<\/code><\/li>\n<li>The file is unlocked<\/li>\n<\/ol>\n<p>The code to send to CellStorageService, and provided by the author, didn&#8217;t work for me. I&#8217;ve had to use Fiddler and open the document into Office on my computer to see the kind of requests send by it to unlock a document. Based on it, I&#8217;ve re-built the code and you can find my solution below.<\/p>\n<p>Tested on Sharepoint 2013 On-Promise only. I don&#8217;t know if this solution works for Sharepoint Online or other version.<br \/>\nAlso note that I use <code>$SP().ajax()<\/code> from <a href=\"https:\/\/aymkdn.github.io\/SharepointPlus\/\">SharepointPlus<\/a>, but it&#8217;s equivalent to the <code>$.ajax<\/code> from jQuery.<\/p>\n<pre class=\"brush:javascript\">\r\n\/\/ full path to the document\r\nvar docUrl = \"https:\/\/website.com\/website\/Doc_Library\/Test.docx\";\r\n\r\n\/\/ start by querying author.dll to find the lockid and the user who locked it\r\n$SP().ajax({\r\n  url: 'https:\/\/website.com\/website\/_vti_bin\/_vti_aut\/author.dll',\r\n  headers:{\r\n    \"Content-Type\": \"application\/x-www-form-urlencoded\",\r\n    \"MIME-Version\": \"1.0\",\r\n    \"Accept\": \"auth\/sicily\",\r\n    \"X-Vermeer-Content-Type\": \"application\/x-www-form-urlencoded\"\r\n  },\r\n  body: 'method=getDocsMetaInfo%3a14%2e0%2e0%2e6009&#038;url%5flist=%5b' + encodeURIComponent(docUrl) + '%5d&#038;listHiddenDocs=false&#038;listLinkInfo=false',\r\n}).then(function(source) {\r\n  \/\/ go thru the source page returned to find the lockid and current user\r\n  var nextLine = false;\r\n  var ret = { \"lockid\":\"\", \"user\":\"\", when:\"\" };\r\n  source.split(\"\\n\").forEach(function(line) {\r\n    if (line.indexOf(\"vti_sourcecontrollockid\") !== -1) nextLine=\"lockid\"; \/\/ vti_sourcecontrollockid -> the lockid to use later\r\n    else if (line.indexOf(\"vti_sourcecontrolcheckedoutby\") !== -1) nextLine=\"user\"; \/\/ vti_sourcecontrolcheckedoutby -> username of the user who locked it\r\n    else if (line.indexOf(\"vti_sourcecontrollockexpires\") !== -1) nextLine=\"when\"; \/\/ vti_sourcecontrollockexpires -> when the server is supposed to unlock it\r\n    else if (nextLine !== false) {\r\n      ret[nextLine] = line.slice(7).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); });\r\n      nextLine = false;\r\n    }\r\n  });\r\n\r\n  if (!ret.lockid) { alert(\"Not Locked\") }\r\n  else {\r\n    \/\/ compose a request based on what Microsoft Office sends to the Sharepoint server\r\n    \/\/ found using Fiddler\r\n    var releaseLockReq = '&lt;s:Envelope xmlns:s=\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\">&lt;s:Body>&lt;RequestVersion Version=\"2\" MinorVersion=\"2\" xmlns=\"http:\/\/schemas.microsoft.com\/sharepoint\/soap\/\"\/>&lt;RequestCollection CorrelationId=\"{96A244BD-D13B-4696-9355-231FB673BC4F}\" xmlns=\"http:\/\/schemas.microsoft.com\/sharepoint\/soap\/\">&lt;Request Url=\"'+docUrl+'\" UseResourceID=\"true\" UserAgent=\"{1984108C-4B93-4EEB-B320-919432D6E593}\" UserAgentClient=\"msword\" UserAgentPlatform=\"win\" Build=\"16.0.8201.2102\" MetaData=\"1031\" RequestToken=\"1\">&lt;SubRequest Type=\"ExclusiveLock\" SubRequestToken=\"1\">&lt;SubRequestData ExclusiveLockRequestType=\"ReleaseLock\" ExclusiveLockID=\"'+ret.lockid+'\"\/>&lt;\/SubRequest>&lt;\/Request>&lt;\/RequestCollection>&lt;\/s:Body>&lt;\/s:Envelope>';\r\n\r\n    \/\/ we send it to the webservice cellstorage.svc\r\n    $SP().ajax({\r\n      url:'https:\/\/website.com\/website\/_vti_bin\/cellstorage.svc\/CellStorageService',\r\n      body:releaseLockReq,\r\n      headers:{\r\n        'Content-Type':'text\/xml; charset=UTF-8',\r\n        'SOAPAction': \"http:\/\/schemas.microsoft.com\/sharepoint\/soap\/ICellStorages\/ExecuteCellStorageRequest\"\r\n      }\r\n    })\r\n    .then(function(res) {\r\n      if (res.indexOf('ErrorCode=\"Success\"') !== -1) alert(\"Success\") \/\/ the file has been unlocked\r\n      else alert(\"Failed\")\r\n    })\r\n  }\r\n})\r\n<\/pre>\n<p>I hope it will be useful to someone else!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you open a file from Sharepoint, it will receive a short term lock that will prevent others to change some properties on the file. This protection can be useful, but also very annoying, for example when the file is not closed properly, then the lock could stay &#8220;forever&#8221;. There are many posts on the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","hide_page_title":"","footnotes":""},"categories":[170,20,33],"tags":[123,24,155,158,117],"class_list":["post-1862","post","type-post","status-publish","format-standard","hentry","category-english","category-niveau-expert","category-programmation","tag-english","tag-javascript","tag-niveau-expert","tag-programmation","tag-sharepoint"],"_links":{"self":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/comments?post=1862"}],"version-history":[{"count":7,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1862\/revisions"}],"predecessor-version":[{"id":1922,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1862\/revisions\/1922"}],"wp:attachment":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/media?parent=1862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/categories?post=1862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/tags?post=1862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}