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

时间: 2024-11-03 22:45:21

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

小议:SharePoint Content Database Status: Read Only与Offline

我们知道SharePoint依赖于SQL Server的Content Database来做数据存储的,但有些企业在管理数据时会提出很多需求:比如Content Database 1只存储客户信息相关的Site Collection:存储在某些Content Database下的Site Collection里的数据只能读不能写等等需求,那么面对这些需求,就需要我们精准的了解Content Database的Status以及如何设置? Content Database 的Status分2钟:Re

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 wrapped inside setup.exe to a folder named unpacked inside the current directory. >innounp.exe -x -dunpacked setup.exe For more information about innou

Restore content database in sharepoint

sometimes, we need backup content database about sharepoint site, then restore it one day. of course, we can backup the contetn database in SQL, then restore it in Sharepoint Central Admin, but it often failed to restore DB using Central Admin, I did

SharePoint Content Deployment prerequisite——Error features deactivate automation

对于Content Deployment的过程简而言之就是: General Application Settings -> Content Deployment ->1) Configure content deployment :Connection Security ->Do not require encryptionSource Status Check ->Disable Source Status Check->OK2) Check deployment of

SharePoint 2013的限制(一)Web Application 和Content Database

Web Application 的限制 SharePoint 2013 Farm最大支持20个WebApplication.这个限制和Application Pool的限制无关,是对于整个Farm来讲的.微软建议把Web Application的数目控制的越小越好.如果想要多个不同名字的site,可以用创建host named 网站集来代替添加新的web application. 一个We Application最多支持5个Zone.这个是SharePoint定义死的,不可修改. 一个Web A

SharePoint Content Approval 功能简介

SharePoint 的Content Approval 是一个很基本的功能,其作用就是让一个文件被审核通过之后才能被某些用户看到,但是其实这个流程理解起来还是需要一些时间的,本文主要帮大家梳理一下相关的流程. 可以在List Setting中开启Content Approval 开启这个设置需要有以下几点注意 对于开启Content Approval之前就已经存在的文件,并且没有对该文件进行过操作,则开启后状态为Approved 对于开启Content Approval之前就已经存在的文件,且

Encrypt Trail and Extract files

在源端,我们需要在 EXTRACT进程中设置ENCRYPTTRAIL 参数,例如: RMTHOST 192.168.1.168, MGRPORT 7809, ENCRYPT BLOWFISH, KEYNAME encryptkey1 ENCRYPTTRAIL RMTTRAIL /opt/ogg/dirdat/la 在目标端需要在replicat进程中加入DECRYPTTRAIL参数,例如: DECRYPTTRAIL 注意encryptkey1是自定义生成的加密文件

sharepoint ECMA

在使用SharePoint ECMAScript之前,我们需要引入几个JS文件: SP.js SP.Core.js SP.Runtime.js SP.js:包含主要的能够用来获取sharepoint数据的对象,如ClientContext.Web.List.Listitem等 SP.Core.js:包含一些实用对象和帮助对象,能够用来完成基本的任务,如HTML编码.URL处理.XML字符串操作等 SP.Runtime.js:包含了核心的ECMAScript运行时的对象,封装了客户端对象模型和服务

SharePoint 2010 升级到2013时间 为了确保用户可以连接,但无法改变升级数据

SharePoint 2010 升级到2013时间 为了确保用户可以连接,但无法改变升级数据 我总结的步骤 红色请注意它们的含义. 步骤1:连接到SQL DBS 上的SharePoint 2010数据库的内容. 步骤2:设置SharePoint内容数据库的仅仅读状态为True. 步骤3:把SharePoint 2010内容数据库备份文件恢复到SharePoint 2013场. 步骤4:SharePoint内容数据库的仅仅读状态设置为False(读写). 步骤5:分离SharePoint 2010