If you ever had the problem where you need to extract files from a SharePoint Content Database or normal SQL Database stored as binary, this post will help you.

The script I have included will export all the content from the SharePoint Content Database to the file structure set by SharePoint. This script should work as it is on your SharePoint Database. If you modify this script a little you can use it to extract and binary data from SQL to files.
Problem: The SharePoint Content Database got corrupted because of a third-party add-on. This caused all kinds of problems as the files could not be found anymore. The Content Database was still accessible through SQL Server.
Solution: To fix this problem we had to extract all the current images and documents to a file system. The first thing you will have to do is enable the Ole Automation Procedures. This will allow SQL Server to write to the file system. You will also have to check that you have the correct permissions and access to write to the file system from SQL Server.
Enabling file writing: Run the following script in SQL:

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO

Now that you have access to the file system you can run the following script. Extracting files from database:

--DECLARING THE CURSOR, THIS IS THE ITEMS YOU WANT TO RUN THE EXTRACTING ON
DECLARE CURSOR_Images CURSOR FOR (SELECT Id FROM [dbo].[AllDocs])   --DECLARE THE TYPE OF THE COLUMN YOU SELECTED ABOVE
DECLARE @ImageID uniqueidentifier;   --START THE CURSOR AND RUN THROUGH ALL THE ITEMS IN CURSOR_Images
OPEN CURSOR_Images
FETCH NEXT FROM CURSOR_Images INTO @ImageID
WHILE (@@FETCH_STATUS <> -1)
BEGIN
--DECLARE THE VARIABLE THAT WILL KEEP THE BINARY DATA
DECLARE @ImageData varbinary(MAX);
--SELECT THE BINARY DATA AND SET IT TO @ImageData. THE BINARY DATA FOR ALLDOCS ARE LOCATED IN ALLDOCSTREAMS
--AND THE ID IS THE SAME AS IN ALLDOCS
SELECT @ImageData = (SELECT TOP 1 CONVERT(varbinary(MAX), Content, 1) FROM [dbo].[AllDocStreams] WHERE Id = @ImageID ORDER BY InternalVersion ASC);   --GET THE LOCATION OF THE DIRECTORY THE FILES WAS SAVED IN AND CHANGE REPLACE THE / WITH \ TO BE USED IN FILESYSTEM
DECLARE @DIRPATH NVARCHAR(MAX);
SET @DIRPATH = REPLACE((SELECT DirName FROM [dbo].[AllDocs] WHERE Id = @ImageID),'/','\');   --SET THE PATH
DECLARE @Path nvarchar(1024);
SELECT @Path = 'C:\Export\' + @DIRPATH + '\';   --CREATE THE DIRECTORIES
EXEC master.dbo.xp_create_subdir @Path;   --GET THE FILE NAME OF THE FILE FROM LEAFNAME
DECLARE @Filename NVARCHAR(1024);
SELECT @Filename = (SELECT LeafName FROM [dbo].[AllDocs] WHERE id = @ImageID);   --SET THE FULL PATH FOR WHERE THE FILES WILL BE STORED
DECLARE @FullPathToOutputFile NVARCHAR(2048);
SELECT @FullPathToOutputFile = @Path + '\' + @Filename;   --SAVE THE FILE TO THE FILE SYSTEM
DECLARE @ObjectToken INT
EXEC sp_OACreate 'ADODB.Stream', @ObjectToken OUTPUT;
EXEC sp_OASetProperty @ObjectToken, 'TYPE', 1;
EXEC sp_OAMethod @ObjectToken, 'OPEN';
EXEC sp_OAMethod @ObjectToken, 'WRITE', NULL, @ImageData;
EXEC sp_OAMethod @ObjectToken, 'SaveToFile', NULL, @FullPathToOutputFile, 2;
EXEC sp_OAMethod @ObjectToken, 'Close';
EXEC sp_OADestroy @ObjectToken;   --LOOP TO THE NEXT ENTRY IN THE CURSOR
FETCH NEXT FROM CURSOR_Images INTO @ImageID
END
CLOSE CURSOR_Images
DEALLOCATE CURSOR_Images

After running this script it could take some time depending on the size of the SharePoint Content Database. You will see some errors that you can ignore. When done have a look in the folder you extracted the files and you will find all your files in the directory. If you have any questions don’t hesitate to ask. I will be glad to help where I can.  You can also find me on Skype: Corvitech

EXTRACT FILES AND IMAGES FROM A SHAREPOINT CONTENT DATABASE的更多相关文章

  1. SharePoint Content Database简介

    SharePoint作为微软主打的企业Portal平台,功能强大,使用简单,非常的方便.对于很多关系数据,我们可以使用自定义列表来维护,如果是非关系数据,可以使用文档库来维护.另外还可以在上面进行版本 ...

  2. How to: Extract files from a compiled setup.exe created by Inno setup

    Use innounp.exe to unpack setup.exe created by using Inno setup: for example, unpack all the files w ...

  3. 如何从SharePoint Content DB中查询List数据

    SharePoint用来维护基础数据非常方便,只需要建立自定义列表,然后使用InfoPath自定义一下维护界面,就可以实现在线的增删改查,开发效率很高.如果维护的数据需要进行审批,还可以加入工作流功能 ...

  4. SharePoint Config database Log file too big – reduce it!

    SharePoint Config database logs are one thing to keep an eye on since they do have a tendency to gro ...

  5. This content database has a schema version which is not supported in this farm.

          I want to move the website to another server. The new server has reinstall Sharepoint2013 serv ...

  6. sharepoint content type publishing

    1. Create 1 Project Team sites (Site1) on SharePoint(可以用普通site)2. Go to http://<PCName>:8080/_ ...

  7. [SharePoint] SharePoint 错误集 2

    1 Run command “New-SPConfigurationDatabase" Feature Description: error message popup after run ...

  8. SharePoint 错误集 2

    1 Run command “New-SPConfigurationDatabase" Feature Description: error message popup after run ...

  9. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q92-Q94)

    Question  92  You are designing a SharePoint 2010 application. You need to make sure the application ...

随机推荐

  1. 将WinForm程序(含多个非托管Dll)合并成一个exe的方法

    原文:将WinForm程序(含多个非托管Dll)合并成一个exe的方法 开发程序的时候经常会引用一些第三方的DLL,然后编译生成的exe文件就不能脱离这些DLL独立运行了. ILMerge能把托管dl ...

  2. odoo订餐系统之订单设计

    订餐系统的主要功能便是用户下单部分,这里我们分为表头mylunch_order和表体mylunch_order_line两张主要的数据表,表头主要记录订单的一些通用信息,比如下单的操作人员 下单日期 ...

  3. Ionic App中嵌入外部网页的问题

    在app中不可避免的要引用第三方的页面,那么在Ionic中是如何实现呢? 1.设计引用外部页面的html框架页面,分3部分,表头有2个按钮,中间是引用的页面,底部隐藏分享相关按钮,具体页面如下: &l ...

  4. eclipse取消自动输入提示

    在设置Eclipse自动提示后,按a-z都会显示提示,但是我们需要键入Enter才会输入,而默认的所有都键入,非常弱智,可采用下面方法设置. 1,先找到相关的插件: window -> show ...

  5. 我的物联网项目专题移到网站:http://51jdk.com

    我的物联网项目专题移到网站:http://51jdk.com

  6. 【JVM.4】调优案例分析与实战

    之前已经介绍过处理Java虚拟机内存问题的知识与工具,在处理实际项目的问题时,除了知识与工具外,经验同样是一个很重要的因素.本章会介绍一些具有代表性的案例. 本章的内容推荐还是原文全篇看完的好,实在不 ...

  7. mybaits拦截器+自定义注解

    实现目的:为了存储了公共字典表主键的其他表在查询的时候不用关联查询(所以拦截位置位于mybaits语句查询得出结果集后) 项目环境 :springboot+mybaits 实现步骤:自定义注解——自定 ...

  8. Unigine mesh顶点坐标转换精度问题

    本问题虽然与Unigine引擎相关,但对其他精度问题也有参考价值. 问题: 将精细模型顶点从自身参考系的相对坐标(类似4378.95020,4561.00000,31.3887463) 转到椭球面世界 ...

  9. python函数基础用法

    函数是通过赋值传递的,参数通过赋值传递给参数,def语句将创建的一个函数对象 并将其赋值给一个变量名! 函数定义的对象属局部作用域,只在函数内部有效,不会影响全局作用域中的对象! 赋值对象属于局部作用 ...

  10. [系统软件]Ubuntu 18.04中的Shutter禁用了“编辑”选项解决

    本文引用自linux公社, 原文请点击 : https://www.linuxidc.com/Linux/2018-04/151911.htm   在Ubuntu 18.04中安装了我最喜欢的截图工具 ...