How to Extract PowerPoint documents from SharePoint ”SQL Database”
What do you do when your SharePoint (WFE) for whatever reason
decides to go bunkers? Your users cannot retrieve their documents because they
cannot get to it. In the mean time your System Administrators are working diligently
to bring the site back online.
To make matters worse, your marketing department has a
presentation to potential investors and all the PowerPoint slides are dead in
SharePoint world.
Well, do not despair. This little tool will enable you to retrieve
your PowerPoint slides or any other document stored in SharePoint.
Warning: This assumes that your SQL database is intact and
not corrupted.
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;
// replace this string with your Sharepoint content DB connection string
string DBConnString = “Server=YOURSHAREPOINTSERVER;Database=CONTENTDATABASE;Trusted_Connection=True;”;
// create a DB connection
SqlConnection con = new SqlConnection(DBConnString);
con.Open();
// the query to grab all the files.
// Note: Feel free to alter the LeafName like ‘%.extension’ arguments to suit your purpose
SqlCommand com = con.CreateCommand();
com.CommandText = “select DirName, LeafName, Content from Docs where (LeafName like ‘%.doc’ or LeafName like ‘%.xls’ or LeafName like ‘%.pdf’ or LeafName like ‘%.ppt’) and Content is not NULL”;
// execute query
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
// grab the file’s directory and name
string DirName = (string)reader["DirName"];
string LeafName = (string)reader["LeafName"];
// create directory for the file if it doesn’t yet exist
if (!Directory.Exists(DirName))
{
Directory.CreateDirectory(DirName);
Console.WriteLine(“Creating directory: “ + DirName);
}
// create a filestream to spit out the file
FileStream fs = new FileStream(DirName + “/” + LeafName, FileMode.Create, FileAccess.Write);
BinaryWriter writer = new BinaryWriter(fs);
// depending on the speed of your network, you may want to change the buffer size (it’s in bytes)
int bufferSize = 1000000;
long startIndex = 0;
long retval = 0;
byte[] outByte = new byte[bufferSize];
// grab the file out of the db one chunk (of size bufferSize) at a time
do
{
retval = reader.GetBytes(2, startIndex, outByte, 0, bufferSize);
startIndex += bufferSize;
writer.Write(outByte, 0, (int)retval);
writer.Flush();
} while (retval == bufferSize);
// finish writing the file
writer.Close();
fs.Close();
Console.WriteLine(“Finished writing file: “ + LeafName);
}
// close the DB connection and whatnots
reader.Close();
con.Close();
No comments:
Post a Comment