Is there a CSOM equivalent of SPFile.TakeOffline - Unpublish last Published Major version when there is a Draft version

sharepoint.stackexchange https://sharepoint.stackexchange.com/questions/222143

Question

I'm trying to unpublish the last published version of a file(Page) when there is a draft version of it.

It is possible with OOTB UI.

SPFile.TakeOffline (Server Object Model) method can be used to do this. I'm looking for a CSOM (Client Object Model) equivalent for this method or fill the requirement.

Below is the OOTB behavior.

Unpublish 1.0 when there is a 1.1

1.0 becomes 0.2 when unpublished.

enter image description here

Requirement: Unpublish the last Major/Published file Version when there is a Draft version of it.

Update:

<site url>/_api/web/getfilebyserverrelativeurl(<relative file  path>)/unpublish(comment='done')

gives below error because there is a draft version at last.

“You can only Unpublish a major version or approved document. Or Cancel publish for a pending document.”

Even File.UnPublish CSOM gave the same error.

Was it helpful?

Solution 2

This is what finally worked for me, let me know if there is any alternative. After failing all CSOM approach, used fiddler and monitored the click action of "Unpublish this version" in view version history dialog window.(Click on the down arrow in the last published major version)

This is the POST request

<siteUrl>/_layouts/15/versions.aspx?FileName=%2Fsites%2<subSite>%2FPages%2F<myFile>%2Easpx&list={73076BC8-EF62-44DD-AA6A-2004B56998C9}&ID=44&col=Number&order=d&Source=http%3A%2F%2F<siteUrl>%2Fsites%2F<subSite>%2F<library>%2FForms%2F<viewName>%2Easpx&op=TakeOffline&IsDlg=1

It is passing some query string parameters to

<siteUrl>/_layouts/15/versions.aspx

Query string params

  • FileName = file name (URL Encoded)
  • list => list Id
  • ID => Item Id
  • col
  • order
  • Source => Request page (I was on 'All Documents' view)
  • op => Operation (TakeOffline)
  • IsDlg => is a dialog

I triggered this HTTP POST request from my code(JavaScript) it worked as I expected. It Unpublished the last published major version when there is a draft version.

Before

Before Execution

After Unpublishing 1.0 it became 0.2

After Unpublishing 1.0 it became 0.2

OTHER TIPS

You can try the following solution.

CSOM: File.UnPublish

JS CSOM: SP.File.unPublish

REST Endpoint:

/_api/web/getfilebyserverrelativeurl('/Shared Documents/filename.docx')/unpublish(comment='done')
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top