简体   繁体   English

如果共享点文档库中不存在目录和子目录,如何创建

[英]How to create directory and subdirectory if it does not exist in sharepoint Document Library

I have a user input as D:\\Test1\\Test2\\Test3\\Test4\\a\\b\\c\\d\\file.jpg as per the user input i need to check if folder and sub folder exist in a Document Library. 根据用户输入,我有一个用户输入为D:\\Test1\\Test2\\Test3\\Test4\\a\\b\\c\\d\\file.jpg我需要检查文档库中是否存在文件夹和子文件夹。

ie

DocLib>>Test1>>Test2....di want to replicate the folder structure in Document Library, if it exist than directly read and save the file else create directory and than subdirectory and upto the level wherin file should be saved. DocLib >> Test1 >> Test2 .... di要复制文档库中的文件夹结构,如果存在,则直接读取并保存文件,否则创建目录,然后创建子目录,直至保存级别为wherin的文件。

Can anyone help me to understand how can i go with this? 谁能帮助我了解我该如何处理? I tried with creating files in local system on hard drive 我尝试在硬盘上的本地系统中创建文件

static void CopyFolder(string sourceFolder, string destFolder)
        {
            if (!Directory.Exists(sourceFolder))
                Directory.CreateDirectory(destFolder);
            string[] files = Directory.GetFiles(sourceFolder);
            foreach (string file in files)
            {
                string name = Path.GetFileName(file);
                string dest = Path.Combine(destFolder, name);
                File.Copy(file, dest);
            }

            //check folder in the source destination
            string[] folders = Directory.GetDirectories(sourceFolder);
            foreach (string folder in folders)
            {
                string name = Path.GetFileName(folder);
                string dest = Path.Combine(destFolder, name);
                System.IO.Directory.CreateDirectory(dest);
                CopyFolder(folder, dest);
            }
        }

No idea how to check if directory exist and than check for subdirectory in sharepoint. 不知道如何检查目录是否存在,而不是如何检查sharepoint中的子目录。 ie add a file by retaining the folder structure specified. 即通过保留指定的文件夹结构来添加文件。 Kindly help 请帮助

To do this you will need to createthe structure of the tree path one by one: here is a short code how it can be done on the root site with UserDocument folder as a root folder: 为此,您将需要一个一个地创建树路径的结构:这是一个简短的代码,说明如何使用UserDocument文件夹作为根文件夹在根站点上完成它:

            // This will contain all information about the path
            DirectoryInfo infoDir = new DirectoryInfo(@"C:\Users\Administrator\Pictures2\WallPaperHD - 078.jpg");

            // Root folder passed => Default in SharePoint
            if (infoDir.Parent != null)
            {
                // All folders are stored here
                List<string> folders = new List<string>();

                // Set current folder to parent
                DirectoryInfo currentDir = infoDir.Parent;
                do
                {
                    // Add its name to array
                    folders.Add(currentDir.Name);

                    // Set parent of current as current if available
                    if (currentDir.Parent != null)
                        currentDir = currentDir.Parent;
                }
                while (currentDir.Parent != null);

                // Add SP structure)
                using (SPSite site = new SPSite("http://testsite.dev"))
                {
                    SPWeb web = site.RootWeb;
                    // Get doc library
                    SPList documentLibrary = web.GetList("/UserDocuments");
                    // If library root exists
                    if (documentLibrary != null)
                    {
                        string folderUrl = "/UserDocuments/";

                        for (int i = folders.Count - 1; i >= 0; i--)
                        {
                            string folder = folders[i];
                            SPFolder newFolder = site.RootWeb.GetFolder(folderUrl + folder);
                            if (!newFolder.Exists)
                            {
                                site.RootWeb.Folders.Add(folderUrl + folder);
                                // Save changes
                                site.RootWeb.Update();

                                folderUrl += folder + "/";
                            }
                        }
                    }
                }
            }

This will create the same structure of folders on the SharePoint side as it was specified in the path passed by user. 这将在SharePoint端创建与用户传递的路径中指定的文件夹相同的文件夹结构。

After this all you need is to save file in the specified folder. 之后,您需要将文件保存在指定的文件夹中。

Hope it helps, 希望能帮助到你,

Andrew 安德鲁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何隐藏sharepoint文档库 - How to hide sharepoint document library 使用SharePoint工作流在文档库中创建文件夹 - Create folder in document library using SharePoint workflow 如何在共享点Web应用程序的虚拟目录中获取子目录? - How to get the subdirectory in virtual directory of a sharepoint web application? 如何将文件上传到sharepoint文档库 - How to upload file to sharepoint document library 如何将文件上传到Sharepoint中的文档库? - How to upload a file to a document library in sharepoint? 如何检查Sharepoint文档库上的文件夹出口 - How to Check Folder Exits on Sharepoint Document Library 如何创建指向共享文档库的快捷方式文件,以便可以通过电子邮件将其发送给其他人 - How can i create a shortcut file to a sharepoint document library so that i can email it to people 如果 File.AppendAllText 不存在,如何创建子目录? - How to File.AppendAllText create subdirectory if doesn't exist? 使用客户端对象模型在SharePoint文档库上创建文件夹 - Create folder on SharePoint document library using client object model sharepoint 2013 使用列表模板 C# 创建文档库 - sharepoint 2013 create document library using List Template C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM