从子文件夹中获取所有文件 [英] Fetch all files from subfolder

查看:88
本文介绍了从子文件夹中获取所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用可视化Web部件.

I am using visual web part.

我正在尝试以编程方式获取子文件夹中上载的所有文件,并在超链接中显示每个文件.我已经尝试了以下代码,但不确定如何检索文件并将其添加到超链接.超链接是在aspx中创建的.

I am trying to fetch all the files uploaded in a sub folder programmatically and show each of the files in a hyperlink . I have tried the below code, but not sure, how to retrieve the file and add it to a hyperlink. The hyperlink is created in aspx.

   private void ShowAttachmentLinks(SPWeb osubWeb)
        {
 SPQuery query = new SPQuery();                

 string foldername = "Shared Documents";

 SPDocumentLibrary docLib = osubWeb.Lists[foldername] as SPDocumentLibrary;

string SubFolderUrl = osubWeb.ServerRelativeUrl + "/" + foldername + "abc" + "/" + bcd;

SPFolder folder = osubWeb.GetFolder(SubFolderUrl);
query.Folder = folder;
              
query.Query="<View Scope=\"RecursiveAll\">" +             
 "<Where>" +
 "<Eq>" +
 "<FieldRef Name=\"FileDirRef\" />" +
 "<Value Type=\"Text\">" + folder +"</Value>" +
    "</Eq>" +
 "</Where>" +                 
"</View>";

以上代码正确吗?如何检索每个文件?

Is the above code correct? How to retrieve each of the files?

谢谢

推荐答案

您好,Venkat,

Hi Venkat,

尝试以下代码:

public string CreateListUsingCAMLAttachments(string siteURL,string ID)
        {
            List list;
            string temp = null;
            IEnumerable<Microsoft.SharePoint.Client.ListItem> listItems;
            using (ClientContext context = new ClientContext(siteURL))
            {
                list = context.Web.Lists.GetByTitle("CDP Tools");
                CamlQuery cq = new CamlQuery();
                cq.ViewXml = @"<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Text'>"+ID+"</Value></Eq></Where></Query><ViewFields><FieldRef Name='Attachments' /></ViewFields></View>";

                listItems = context.LoadQuery(list.GetItems(cq));
                context.Load(list.RootFolder);
                context.Load(list.RootFolder.Files);
                context.Load(list.RootFolder.Folders);
                context.ExecuteQuery();

                
                foreach (Folder f in list.RootFolder.Folders)
                {
               
                    context.Load(f.Folders);
                    context.ExecuteQuery();
                    foreach (Folder f1 in f.Folders)
                    {
                       
                        if (f1.Name == ID)
                        {
                            context.Load(f1.Files);
                            context.ExecuteQuery();
                            foreach (File file1 in f1.Files)
                            {
                                ListBox1.Items.Add(file1.Name);
                                temp = file1.Name;
                            }
                        }
                        else
                        {
                           //
                        }

                    }

                }

               
              

            }
            return temp;
        }

Change code as your requirement.. Hope this will help.. :)

Source Link 



这篇关于从子文件夹中获取所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆