Saber
Saber

Reputation: 2648

Sharepoint data retrieval

I have a Database (WSS_Content) of Sharepoint but no sharepoint is installed and I need its data. What is your solution to retreive data? Should I code a converter to extract files/links/sites data from binary arrays to data or there is simpler way? Can I install a a fresh sharepoint and use this database?

Upvotes: 0

Views: 241

Answers (2)

Nigel Whatling
Nigel Whatling

Reputation: 2391

I dug up an old app I had a little while back that does a really basic extraction of all documents from a content database. It's not selective in any way, it just grabs whatever is there. You can then pick through the output to get what you needed.

I believe the original code came from someone else (I can't remember where so can't credit them). I just hacked it a little bit. Feel free to give it a shot.

It just accesses the database directly, so you just need it mounted in SQL Server. No SharePoint server required.

using System;
using System.Data.SqlClient;
using System.IO;

namespace ContentDump
{
    class Program
    {
        // Usage: ContentDump {server} {database}
        //
        static void Main(string[] args)
        {
            string server = args[0];
            string database = args[1];
            string dbConnString = String.Format("Server={0};Database={1};Trusted_Connection=True;", server, database);

            // create a DB connection
            SqlConnection con = new SqlConnection(dbConnString);
            con.Open();

            // the query to grab all the files.
            SqlCommand com = con.CreateCommand();
            com.CommandText = "SELECT ad.SiteId, ad.Id, ad.DirName," +
                " ad.LeafName, ads.Content" +
                " FROM AllDocs ad, AllDocStreams ads" +
                " WHERE ad.SiteId = ads.SiteId" +
                " AND ad.Id = ads.Id" +
                " AND ads.Content IS NOT NULL" +
                " Order by DirName";

            // execute query
            SqlDataReader reader = com.ExecuteReader();

            while (reader.Read())
            {
                // grab the file’s directory and name
                string DirName = (database + "/" + (string)reader["DirName"]).Replace("//", "/");
                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);

                int bufferSize = 1024;
                long startIndex = 0;
                long retval = 0;
                byte[] outByte = new byte[bufferSize];

                // grab the file out of the db
                do
                {
                    retval = reader.GetBytes(4, 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();
        }
    }
}

Upvotes: 2

alrama
alrama

Reputation: 648

You could attempt to attach your database in a new sharepoint environment and webapp with command stsadm.exe -o addcontentdb -url -databasename . This way is used to migrate database from sharepoint 2007 to 2010 Farms too. Then you should see the content in url of webapp

Upvotes: 1

Related Questions