Monday, June 4, 2012

Get Documents from SharePoint Content Database in C# console application

If the SharePoint's Database is not attached with any  web application and you want to get the documents from that content db, for that purpose here is option to do the same

 

Create console application in Visual studio to get the document from Content DB, to access the documents use following Query.

 

 

SELECT AllLists.tp_Title AS [ListName],AllDocs.LeafName AS [FileName],AllDocs.DirName AS [URL],AllDocStreams.Content  AS [DocumentContent] FROM AllDocs JOIN AllDocStreams ON AllDocs.Id=AllDocStreams.Id JOIN AllLists ON AllLists.tp_id = AllDocs.ListId where AllLists.tp_Title='Documents';

 

 

In console application create following methods to get the documents.

 

 

Main method of console app to create data table object and download documents from SQL.

 

      public static void Main(string[] args)

        {

            Console.WriteLine("Application Started...");

            string strQuery = "SELECT AllLists.tp_Title AS [ListName],AllDocs.LeafName AS [FileName],AllDocs.DirName AS [URL],AllDocStreams.Content" +

                " AS [DocumentContent] FROM AllDocs JOIN AllDocStreams ON AllDocs.Id=AllDocStreams.Id JOIN AllLists ON AllLists.tp_id = AllDocs.ListId where AllLists.tp_Title='Documents'";

            SqlCommand cmd = new SqlCommand(strQuery);

            //cmd.Parameters.Add("@id", SqlDbType.Int).Value = 1;

            DataTable dt = GetData(cmd);

            if (dt != null)

            {

                download(dt);

            }

            Console.WriteLine("Press any key to complete.");

            Console.ReadKey();

        }

 

 

 

This download method will create folder hierarchy as in Document library and then it will create documents in particular folder.

 

        private static void download(DataTable dt)

        {

            int counter = 0;

            foreach (DataRow row in dt.Rows)

            {

                Byte[] bytes = (Byte[])row["DocumentContent"];

                string FileURL = Convert.ToString(row["URL"]);

                string ListName = Convert.ToString(row["ListName"]);

                string FileName = Convert.ToString(row["FileName"]);

 

                string documentFolderName = "Documents";

                if (!Directory.Exists(documentFolderName))

                {

                    Directory.CreateDirectory(documentFolderName);

                }

 

                string folderstructure = FileURL.Substring(FileURL.IndexOf(documentFolderName) + documentFolderName.Length+1);

 

                string[] folderstrArray = folderstructure.Split('/');

                string folderName = "Documents";

                for (int i = 0; i < folderstrArray.Length; i++)

                {

                    folderName =folderName +"\\"+ folderstrArray[i];

                    if (!Directory.Exists(folderName))

                    {

                        Directory.CreateDirectory(folderName);

                    }

                }

                using (Stream output = File.OpenWrite(folderName + "\\" + FileName))

                {

                   output.Write(bytes, 0, bytes.Length);

                }

 

               counter++;

            }

        }

 

 

 

Get the data table object from sql command.

 

        private static DataTable GetData(SqlCommand cmd)

        {

            DataTable dt = new DataTable();

            String strConnString = "----------------Add your connection string here.-----------------------------------";

            SqlConnection con = new SqlConnection(strConnString);

            SqlDataAdapter sda = new SqlDataAdapter();

            cmd.CommandType = CommandType.Text;

            cmd.Connection = con;

            try

            {

                con.Open();

                sda.SelectCommand = cmd;

                sda.Fill(dt);

                return dt;

            }

            catch

            {

                return null;

            }

            finally

            {

                con.Close();

                sda.Dispose();

                con.Dispose();

            }

        }

 

 

Once you execute this it will create folder hierarchy in your output folder with all documents.



No comments:

Post a Comment