Sharepoint Tip - Recovering Documents directly from the database

Page Title:

This came in very handy for me in trying to restore a single document. Saved me trying to connect the database to a sharepoint install in a virtual machine. Capturing it here just in case, but the original source is http://techtalkpt.blogspot.com/2006/02/sharepoint-tip-recovering-documents.html.

1) Find the right row. Everything is in the DOCS table (_SITE Database), and there is a DirName property that you can use to find it based on a relative path. Something like this will work.

SELECT *

FROM Docs

WHERE (DirName LIKE 'sites/sitename/Shared Documents%')


2) From there, grab the ID guid. Save the following to a local .vbs file, and replace the servername, database name, GUID and filename.

Set cn = WScript.CreateObject("ADODB.Connection")

cn.Open "Provider=SQLOLEDB;data Source=SERVERENAME;Initial Catalog=WSS or SPS SITE DB name;Trusted_Connection=yes"

Set rs = WScript.CreateObject("ADODB.Recordset")

rs.Open "Select content from docs where id='{guid goes here}'", cn

Set mstream = WScript.CreateObject("ADODB.Stream")

mstream.Type = 1

mstream.Open

mstream.Write rs.Fields("content").Value

mstream.SaveToFile "c:\filename goes here", 2

rs.Close

cn.Close


Relevant Tags: MS SQL | SharePoint