SharePoint中Document Library文档库数据备份到本地

1.得到SharePoint的Site和RootWeb,以及想要文档库,并且在指定目录文件下面创建一些文件夹 。   

#region       
        /// 
        /// 在指定文件目录下面备份已知SiteUrL下面的Shared Documents
        /// 
        /// SiteUrl
        /// 备份文件的地址
        public void CreateDirectoryTree(string siteUrl, string destinationPath)
        {
            using (SPSite mySite = new SPSite(siteUrl))
            {
                using (SPWeb myWeb = mySite.RootWeb)
                {
                    string tempDirectory = destinationPath;
                    SPList myList = myWeb.Lists["Documents"];
                    //这里如果存在同名的可能会有问题
                    SPFolder myFolder = myList.RootFolder;
                    DirectoryInfo myDirectory = new DirectoryInfo(tempDirectory);
                    if (myDirectory.Exists)
                    {
                        tempDirectory += "\\" + "Web";
                        if (!Directory.Exists(tempDirectory))
                        {
                            myDirectory.CreateSubdirectory("Web");
                        }
                        tempDirectory += "\\" + "Library";
                        if (!Directory.Exists(tempDirectory))
                        {
                            Directory.CreateDirectory(tempDirectory);
                        }
                        tempDirectory += "\\" + myFolder.Name.ToString();
                        if (!Directory.Exists(tempDirectory))
                        {
                            Directory.CreateDirectory(tempDirectory);
                        }
                        CreateSubDirectory(myFolder, tempDirectory);
                    }
                    // Console.WriteLine(myFolder.Name);
                }
            }
        }
#endregion

2.先遍历文档库下面的第一层的子文件夹和子文件,这里要过滤掉一个名为Forms的隐藏文件,然后再迭代遍历出所有的子文件夹和子文件,另外还需要考虑大文件备份的情况(判断文件夹和文件存在与否都做了判定和处理)。

        /// 
        /// 递归遍历所有的子文件夹和子文件
        /// 
        /// 一个文件,从而遍历它下面的所有文件夹和文件
        /// 需要备份到的地址
        public void CreateSubDirectory(SPFolder myFolder, string destinationPath)
        {
            SPList myList = myFolder.DocumentLibrary;
            int myListLength = myList.Items.Count;
            SPListItem myListItem = myList.Items[0];
            string tempPath = destinationPath;
            SPFolderCollection myFolderCollection = myFolder.SubFolders;
            SPFileCollection myFileCollection = myFolder.Files;
            int subFolderLength = myFolderCollection.Count;
            int subFileLength = myFileCollection.Count;
            if (subFolderLength > 0 || subFileLength > 0)
            {
                if (subFolderLength > 0)
                {
                    for (int i = 0; i < subFolderLength; i++)
                    {
                        if (myFolderCollection[i].Name != "Forms")
                        {
                            tempPath += "\\" + myFolderCollection[i].Name;
                            if (!Directory.Exists(tempPath))
                            {
                                Directory.CreateDirectory(tempPath);
                            }
                            CreateSubDirectory(myFolderCollection[i], tempPath);
                        }
                    }
                }
                foreach (SPFile item in myFileCollection)
                {
                    DirectoryInfo myDirectoryInfo = new DirectoryInfo(destinationPath);
                    string tempFilePath = destinationPath;
                    tempFilePath += "\\" + item.Name;
                    if (File.Exists(tempFilePath))
                    {
                        File.Delete(tempFilePath);
                    }
                    using (FileStream myFileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write))
                    {
                        const int bufferSize = 4 * 1024 * 1024;
                        //byte[] fileContentBuffer = new byte[bufferSize];
                        byte[] fileContent = item.OpenBinary();
                        int num = fileContent.Length / bufferSize;
                        int data = fileContent.Length % bufferSize;
                        int start = 0;
                        while (start < num)
                        {
                            myFileStream.Write(fileContent, start * bufferSize, bufferSize);
                            start++;
                        }
                        myFileStream.Write(fileContent, start * bufferSize, data);
                    }
                    for (int a = 0; a < myListLength; a++)
                    {
                        if (myList.Items[a].Name == item.Name)
                        {
                            myListItem = myList.Items[a];
                            break;
                        }
                    }

                    myListItem["Source Path"] = tempFilePath;
                    myListItem.Update();
                    tempFilePath = destinationPath;
                }
            }
        }

3.另外,就是需要一些异常处理,这里没有加,自己用的时候可以加上。以上纯是个人刚开始自学SharePoint一点浅显的理解,欢迎讨论和指导

你可能感兴趣的:(SharePoint)