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)




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






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))





                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))





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


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









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;




                sda.SelectCommand = cmd;


                return dt;




                return null;











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

Friday, June 1, 2012

Shortcut to "Save As Template" in SharePoint 2010

In SharePoint 2010, the shortcut of "Save As Template" option is URL /_layouts/SaveTmpl.aspx

Hide Left-hand Navigation in SharePoint 2010 Page Layout

In SharePoint Home page we don't need Left hand navigation, to hide that left hand navigation there is lots of way.

I found a simple way, just open your page layout in SharePoint designer and add following code.

<asp:Content ID="PageHead" ContentPlaceholderID="PlaceHolderAdditionalPageHead" runat="server">
<style type="text/css">
         margin-left: 0px;
         background: transparent;

It will hide Left hand Navigation from your Page layout.