none
In O365, Get the all doc url and custom fields based on the folder name using CSOM code? RRS feed

  • Question

  • Requirement is if i provide a Document Library folder name as input,

    then get doc url(fileref),Proj Name fields as the ouptput from folder level,sub folder level  in a json format.

    I have developed code.but its taking 7 to 10 min of time(if doc's are more tan 5000) to get the output in JSON format. Can you please look into the below code and provide better modification to improve the performance and get the output  faster.

     private List<DocumentProperties> GetItemsInFolder(string url, List<DocumentProperties> items)
            {

                string username = "uname";
                string password = "pwd";
                var securestring = new SecureString();
                foreach (char pass in password)
                {
                    securestring.AppendChar(pass);
                }
                string siteUrl = "<site ur>";
                string listName = "<custom document library>";
                var clientContext = new ClientContext(siteUrl);
                clientContext.Credentials = new SharePointOnlineCredentials(username, securestring);
                Web web = clientContext.Web;
                clientContext.Load(web);
                clientContext.Load(web.Lists);
                clientContext.Load(web, wb => wb.ServerRelativeUrl);
                clientContext.ExecuteQuery();

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

                Folder folder = web.GetFolderByServerRelativeUrl(url);
                clientContext.Load(folder, f => f.ServerRelativeUrl);
                clientContext.ExecuteQuery();
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
                ListItemCollection listItems = list.GetItems(camlQuery);
                clientContext.Load(listItems);
                clientContext.ExecuteQuery();
                if (listItems.Count > 0)
                {
                    for (int i = 0; i < listItems.Count; i++)
                    {
                        if (listItems[i].FieldValues["FSObjType"].ToString() == "0")
                        {
                            DocumentProperties properties = new DocumentProperties();
                            TaxonomyFieldValueCollection taxFieldValue = listItems[i].FieldValues["Project_x0020_Name"] as TaxonomyFieldValueCollection;
                            properties.DocUrl = listItems[i].FieldValues["FileRef"].ToString();
                            properties.ProjectName = string.Join(",", taxFieldValue.ToList().Select(values => values.Label).ToArray());

                            items.Add(properties);
                        }
                        else
                        {
                            items = GetItemsInFolder(listItems[i].FieldValues["FileRef"].ToString(), items);
                        }
                    }
                }
                return items;
            }
           
            [WebMethod]
            //Web Method to get the links of Documtnts from my Document Library
            public string getDocumentLink(string projectName)
            {
                l
                string username = "uname";
                string password = "pwd";
                var securestring = new SecureString();
                foreach (char pass in password)
                {
                    securestring.AppendChar(pass);
                }
                string siteUrl = "<site url";
                string listName ="List name";
                var clientContext = new ClientContext(siteUrl);
                clientContext.Credentials = new SharePointOnlineCredentials(username, securestring);
                Web web = clientContext.Web;
                clientContext.Load(web);
                clientContext.Load(web.Lists);
                clientContext.Load(web, wb => wb.ServerRelativeUrl);
                clientContext.ExecuteQuery();

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

                Folder folder = web.GetFolderByServerRelativeUrl(siteUrl + "Project Files/" + projectName + "/");
                clientContext.Load(folder);
                clientContext.ExecuteQuery();

                CamlQuery camlQuery = new CamlQuery();
                camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
                ListItemCollection listItems = list.GetItems(camlQuery);
                clientContext.Load(listItems);
                clientContext.ExecuteQuery();
                List<DocumentProperties> spItems = new List<DocumentProperties>();

                if (listItems.Count > 0)
                {
                    for (int i = 0; i < listItems.Count; i++)
                    {
                        if (listItems[i].FieldValues["FSObjType"].ToString() == "0")
                        {
                            DocumentProperties properties = new DocumentProperties();
                            TaxonomyFieldValueCollection taxFieldValue = listItems[i].FieldValues["Project_x0020_Name"] as TaxonomyFieldValueCollection;
                            properties.DocUrl =listItems[i].FieldValues["FileRef"].ToString();
                            properties.ProjectName = string.Join(",", taxFieldValue.ToList().Select(values => values.Label).ToArray());

                            spItems.Add(properties);
                        }
                        else
                        {
                            spItems = GetItemsInFolder(listItems[i].FieldValues["FileRef"].ToString(), spItems);
                        }

                      //  spItems = GetItemsInFolder(listItems[i].FieldValues["FileRef"].ToString(), spItems);

                    }
                }


                return JsonConvert.SerializeObject(spItems, Formatting.Indented, new JsonSerializerSettings
                {
                    PreserveReferencesHandling = PreserveReferencesHandling.Objects
                });

            }

        }
        public class DocumentProperties
        {
            public string DocUrl { set; get; }
            public string ProjectName { set; get; }
        }
        public class listvalues
        {
            public string ID { get; set; }
            public string projectName { get; set; }     
            public string Title { get; set; }
            public string Location { get; set; }
            public string Category { get; set; }       
            public string Description { get; set; }

        }

    Please help interns of improve the performance using CSOM Code?

    Monday, May 21, 2018 10:05 AM

Answers

  • Hi,

    Here is updated code, you may try it.

    private List<DocumentProperties> GetItemsInFolder(ClientContext clientContext,List list,string url, List<DocumentProperties> items)
            {
    
                //string username = "uname";
                //string password = "pwd";
                //var securestring = new SecureString();
                //foreach (char pass in password)
                //{
                //    securestring.AppendChar(pass);
                //}
                //string siteUrl = "<site ur>";
                //string listName = "<custom document library>";
                //var clientContext = new ClientContext(siteUrl);
                //clientContext.Credentials = new SharePointOnlineCredentials(username, securestring);
                Web web = clientContext.Web;
                //clientContext.Load(web);
                //clientContext.Load(web.Lists);
                //clientContext.Load(web, wb => wb.ServerRelativeUrl);
                //clientContext.ExecuteQuery();
    
                //List list = web.Lists.GetByTitle(listName);
                //clientContext.Load(list);
                //clientContext.ExecuteQuery();
    
                Folder folder = web.GetFolderByServerRelativeUrl(url);
                clientContext.Load(folder, f => f.ServerRelativeUrl);
                clientContext.ExecuteQuery();
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
                ListItemCollection listItems = list.GetItems(camlQuery);
                clientContext.Load(listItems, _Items => _Items.Include(item => item["FSObjType"], item => item["Project_x0020_Name"], item => item["FileRef"]));
                clientContext.ExecuteQuery();
                if (listItems.Count > 0)
                {
                    for (int i = 0; i < listItems.Count; i++)
                    {
                        if (listItems[i].FieldValues["FSObjType"].ToString() == "0")
                        {
                            DocumentProperties properties = new DocumentProperties();
                            TaxonomyFieldValueCollection taxFieldValue = listItems[i].FieldValues["Project_x0020_Name"] as TaxonomyFieldValueCollection;
                            properties.DocUrl = listItems[i].FieldValues["FileRef"].ToString();
                            properties.ProjectName = string.Join(",", taxFieldValue.ToList().Select(values => values.Label).ToArray());
    
                            items.Add(properties);
                        }
                        else
                        {
                            items = GetItemsInFolder(clientContext, list, listItems[i].FieldValues["FileRef"].ToString(), items);
                        }
                    }
                }
                return items;
            }
    
            [WebMethod]
            //Web Method to get the links of Documtnts from my Document Library
            public string getDocumentLink(string projectName)
            {
                string username = "uname";
                string password = "pwd";
                var securestring = new SecureString();
                foreach (char pass in password)
                {
                    securestring.AppendChar(pass);
                }
                string siteUrl = "<site url";
                string listName = "List name";
                var clientContext = new ClientContext(siteUrl);
                clientContext.Credentials = new SharePointOnlineCredentials(username, securestring);
                Web web = clientContext.Web;
                //don't load unused objects
                //clientContext.Load(web);
                //clientContext.Load(web.Lists);
                //clientContext.Load(web, wb => wb.ServerRelativeUrl);
                //clientContext.ExecuteQuery();
    
                List list = web.Lists.GetByTitle(listName);
                //clientContext.Load(list);
                //clientContext.ExecuteQuery();
    
                Folder folder = web.GetFolderByServerRelativeUrl(siteUrl + "Project Files/" + projectName + "/");
                clientContext.Load(folder);
                clientContext.ExecuteQuery();
    
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
                ListItemCollection listItems = list.GetItems(camlQuery);
                clientContext.Load(listItems, _Items => _Items.Include(item => item["FSObjType"], item => item["Project_x0020_Name"], item => item["FileRef"]));
                clientContext.ExecuteQuery();
                List<DocumentProperties> spItems = new List<DocumentProperties>();
    
                if (listItems.Count > 0)
                {
                    for (int i = 0; i < listItems.Count; i++)
                    {
                        if (listItems[i].FieldValues["FSObjType"].ToString() == "0")
                        {
                            DocumentProperties properties = new DocumentProperties();
                            TaxonomyFieldValueCollection taxFieldValue = listItems[i].FieldValues["Project_x0020_Name"] as TaxonomyFieldValueCollection;
                            properties.DocUrl = listItems[i].FieldValues["FileRef"].ToString();
                            properties.ProjectName = string.Join(",", taxFieldValue.ToList().Select(values => values.Label).ToArray());
    
                            spItems.Add(properties);
                        }
                        else
                        {
                            spItems = GetItemsInFolder(clientContext, list,listItems[i].FieldValues["FileRef"].ToString(), spItems);
                        }
    
                        //  spItems = GetItemsInFolder(listItems[i].FieldValues["FileRef"].ToString(), spItems);
    
                    }
                }
    
    
                return JsonConvert.SerializeObject(spItems, Formatting.Indented, new JsonSerializerSettings
                {
                    PreserveReferencesHandling = PreserveReferencesHandling.Objects
                });
    
            }

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by sudhir muvva Wednesday, May 23, 2018 3:21 PM
    Tuesday, May 22, 2018 3:58 AM