Friday, January 4, 2019

Get Folders under Subfolder

 public string sMenuString = "";
        [HttpGet]
        [Route("api/GetFolderTreeView")]
        public string GetFolderTreeView()
        {
            string sharePointUrl = ConfigurationManager.AppSettings["SharePointURL"];
            string _spUserName = ConfigurationManager.AppSettings["userName"];
            string _spPwd = ConfigurationManager.AppSettings["Password"];
            try
            {
                using (ClientContext clientContext = new ClientContext(sharePointUrl))
                {
                    var passWord = new SecureString();
                    foreach (char c in _spPwd.ToCharArray()) passWord.AppendChar(c);
                    clientContext.Credentials = new SharePointOnlineCredentials(_spUserName, passWord);
                    Web web = clientContext.Web;
                    // We want to retrieve the web's properties.
                    clientContext.Load(web);
                    clientContext.Load(web.Lists);
                    clientContext.Load(web, wb => wb.ServerRelativeUrl);
                    clientContext.ExecuteQuery();

                    List list = web.Lists.GetByTitle("SampleDocuments");
                    clientContext.Load(list);
                    clientContext.ExecuteQuery();

                    Microsoft.SharePoint.Client.Folder folder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/SampleDocuments/0019E00000hQCs4QAG");
                    clientContext.Load(folder);
                    clientContext.ExecuteQuery();

                    CamlQuery camlQuery = new CamlQuery();
                    camlQuery.ViewXml = @"<View Scope='RecursiveAll'>
                                     <Query>
                                     </Query>
                                 </View>";
                    camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
                    ListItemCollection listItems = list.GetItems(camlQuery);
                    clientContext.Load(listItems);
                    clientContext.ExecuteQuery();

                    clientContext.Load(list.RootFolder);
                    clientContext.Load(list.RootFolder.Folders);
                    clientContext.ExecuteQuery();

                    foreach (var item in listItems)
                    {
                        if (item.FileSystemObjectType == FileSystemObjectType.Folder)
                        {
                            // This is a  Folder
                            clientContext.Load(item.Folder);
                            clientContext.ExecuteQuery();
                            sMenuString += "Folder" + item.Folder.Name;

                        }
                        else if (item.FileSystemObjectType == FileSystemObjectType.File)
                        {
                            // This is a File
                            clientContext.Load(item.File);
                            clientContext.ExecuteQuery();
                            //sMenuString += "File : "+item.File.Name;
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                var errmsg = ex.Message;
            }

            return sMenuString;
        }
           

No comments:

Post a Comment

Building Secure Azure AI Foundry Agents with Managed Identity

  The credential problem nobody wants to own Every team that connects an Azure AI Foundry agent to Microsoft Graph, SharePoint, Outlook, or...