从备份中恢复被用户误删的sharepoint 2003的Doc Library。

在sharepoint 2003中因为没有回收功能,恢复被用户误删得文件相当麻烦。
一般的途径是恢复sql2000的备份,重新安装一个sharepoint 2003,用一个偷梁换柱的办法(修改GUID)把新的sharepoint2003连接回sql2000的备份。然后取回文件。

其实有一个更加简单办法。
因为所有的文件均存放在docs表中,使用以下的代码就可以把文件取回本地

string connectionString = @"Data Source=<SQL2000 Server Name>;Initial Catalog=<Database Name>;Integrated Security=True";
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            conn.Open();

            //This section is to obtain the total number of files
            //SQL query string: dirname = folder from which you would like to retrieve from
            //extension<>'' = must have extension = must be a file
            //size >0 = size of file must not be NULL
            SqlCommand cmd0 = new SqlCommand(@"select count(*) from docs where dirname like '%lrts/shared%' and extension <>'' and size >0 ", conn);
            SqlDataReader reader0 = cmd0.ExecuteReader();
            reader0.Read();
            string totalItems = reader0[0].ToString();
            reader0.Close();
            int itemNums = Int32.Parse(totalItems);

            //This section retrieves all the Id of the files and places them in a ArrayList
            SqlCommand cmd1 = new SqlCommand(@"select Id, DirName, LeafName, Size from docs where dirname like '%<DocLib Path>%' and extension <>'' and size >0 ", conn);
            SqlDataReader reader1 = cmd1.ExecuteReader();
            ArrayList allID = new ArrayList();

            for (int i = 0; i < itemNums; i++)
            {
                reader1.Read();

                string IDNumber = reader1["Id"].ToString();
                allID.Add(IDNumber);
            }
            reader1.Close();

            //This section retrieves all files
            for (int i = 0; i < itemNums; i++)
            {
                string IDNumber = (string)allID[i];     //getting Id

                SqlCommand cmd = new SqlCommand(@"select Id, DirName, LeafName, Size, Content from docs where dirname like '%lrts/shared%' and extension <>''  and size >0 and Id='" + IDNumber + "'", conn);

                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);

                if (reader.HasRows)
                {
                    reader.Read();

                    //set the content length to the stored content length in the DB
                    String ContentLength = reader["Size"].ToString();

                    String dirName = reader["DirName"].ToString();

                    //creating directory structure from dirName
                    Directory.CreateDirectory(@"C:\retrieve docs\" + dirName);

                    //place the file in this dir
                    string pathName = @"C:\retrieve docs\" + dirName + @"\";

                    //getting the filename
                    String fileName = reader["LeafName"].ToString();

                    //read out the binary data from the db
                    byte[] data = reader["Content"] as byte[];

                    MemoryStream memstream = new MemoryStream();

                    memstream.Write(data, 0, Int32.Parse(ContentLength));

                    byte[] byteArray = memstream.ToArray();

                    memstream.Flush();
                    memstream.Close();

                    FileStream fs = File.Create(pathName + fileName);
                    BinaryWriter bw = new BinaryWriter(fs);

                    bw.Write(byteArray);
                    bw.Close();
                    fs.Close();

                    reader.Close();

 

                }
            }
        }


这个方法很快就拿到了文件,当然,用户的权限就没有办法取回了。

你可能感兴趣的:(SharePoint)