EXTRACT FILES AND IMAGES FROM A SHAREPOINT CONTENT DATABASE
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的更多相关文章
- SharePoint Content Database简介
SharePoint作为微软主打的企业Portal平台,功能强大,使用简单,非常的方便.对于很多关系数据,我们可以使用自定义列表来维护,如果是非关系数据,可以使用文档库来维护.另外还可以在上面进行版本 ...
- 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 ...
- 如何从SharePoint Content DB中查询List数据
SharePoint用来维护基础数据非常方便,只需要建立自定义列表,然后使用InfoPath自定义一下维护界面,就可以实现在线的增删改查,开发效率很高.如果维护的数据需要进行审批,还可以加入工作流功能 ...
- 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 ...
- 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 ...
- sharepoint content type publishing
1. Create 1 Project Team sites (Site1) on SharePoint(可以用普通site)2. Go to http://<PCName>:8080/_ ...
- [SharePoint] SharePoint 错误集 2
1 Run command “New-SPConfigurationDatabase" Feature Description: error message popup after run ...
- SharePoint 错误集 2
1 Run command “New-SPConfigurationDatabase" Feature Description: error message popup after run ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q92-Q94)
Question 92 You are designing a SharePoint 2010 application. You need to make sure the application ...
随机推荐
- [转][南京米联ZYNQ深入浅出]第二季更新完毕课程共计16节课
[南京米联]ZYNQ第二季更新完毕课程共计16节课 [第二季ZYNQ] ...
- Jmeter(三十)_TimeShift函数在JSR223中的使用
今天学习一下TimeShift函数在JSR223中的使用方法. 关联之前的一篇时间戳文章:Jmeter(十二)_打印时间戳 首先,创建线程组,在线程组下面创建一个JSR223采样器 选择Groovy语 ...
- Ceph常规操作及常见问题梳理
Ceph集群管理 每次用命令启动.重启.停止Ceph守护进程(或整个集群)时,必须指定至少一个选项和一个命令,还可能要指定守护进程类型或具体例程. **命令格式如 {commandline} [opt ...
- 【CV】ICCV2015_Unsupervised Learning of Spatiotemporally Coherent Metrics
Unsupervised Learning of Spatiotemporally Coherent Metrics Note here: it's a learning note on the to ...
- 《Linux内核设计》第17章学习笔记
- Linux内核分析第一次学习报告
Linux内核分析第一次学习报告 学生 黎静 学习内容 1.存储程序计算机工作模型 冯诺依曼体系结构:核心思想为存储程序计算机. CPU抽象为for循环,总是执行下一条指令,内存保存指令和数据,CPU ...
- html5制作导航条
(1)background-repeat:no-repeat;图片不平铺 (2)使用<ul>和<li>便签,代码简介有序.易于编排. (3)在引入外部css文件时,<li ...
- GIthub地址
https://github.com/cuibaoxue/Text1
- Xshell连接到centos提示Could not connect to (port 22): Connection failed
关于XShell连接虚拟机中的centos系统的问题,在连接的时候报错如下: 一开始以为是系统的问题,但是搞了很久,才发现是虚拟机这个软件本身的问题,的确坑啊!所以解决方法也很简单.在编辑菜单那里打开 ...
- shell脚本--shift参数左移
参数左移什么意思呢?这个参数指的是在运行脚本时,跟在脚本名后面的参数,前面已经讲过,可以使用$#来获取参数的个数,使用$*来获取所有的参数,而参数左移的含义是这样的:有个指针指向参数列表第一个参数,左 ...