Search code examples
c#sharepointcsomoffice-dev-pnp

how to get modifiedby property of a file for showing file details sharepoint C#


I want to display files from a sharepoint folder that were modified by username. please help me for this. Also tell me how to show this file with sorting order by datetime.

I tried using docName.Add(file.ModifiedBy); property but its not available, here is the code:

public List<string> getFiles(ClientContext CContext,string INVOICENO)
        {
            List list = CContext.Web.Lists.GetByTitle("Documents");


            CContext.Load(list);
            CContext.Load(list.RootFolder);
            CContext.Load(list.RootFolder.Folders);
            CContext.Load(list.RootFolder.Files);
            CContext.ExecuteQuery();
            FolderCollection fcol = list.RootFolder.Folders;
            List<string> docName = new List<string>();
            foreach (Folder f in fcol)
            {
                if(INVOICENO==null)
                {
                    INVOICENO = "";
                }
                string foldername = INVOICENO.ToString();
                if (f.Name == foldername)
                {
                    CContext.Load(f.Files);
                    CContext.ExecuteQuery();
                    FileCollection fileCol = f.Files;
                    foreach (File file in fileCol)
                    {
                        docName.Add(file.Name);
                        docName.Add(file.TimeLastModified.ToShortDateString());
                       

                    }
                }
            }
            return docName.ToList();

        }

Solution

  • According to my testing and research, you can use CAML Query to display the files modified by the username from the SharePoint folder.

    Here is an example you can refer to:(sorted by modification time in ascending order)

        static void Main(string[] args)
        {
            var clientContext = GetonlineContext();
            Web site = clientContext.Web;
    
            List list = clientContext.Web.Lists.GetByTitle("Library Name");
            CamlQuery query = new CamlQuery();
            query.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Editor' /><Value Type='User'>UserName</Value></Eq></Where><OrderBy><FieldRef Name='Modified' Ascending='True' /></OrderBy></Query><ViewFields /><QueryOptions /></View>";
            
            ListItemCollection collListItem = list.GetItems(query);
    
            clientContext.Load(collListItem);
    
            clientContext.ExecuteQuery();
            foreach (ListItem item in collListItem)
            {
                
                Debug.WriteLine("Name:"+item["FileLeafRef"]+"\n"+"Modified"+item["Modified"]);
            }
            clientContext.ExecuteQuery();
    
        }
    

    enter image description here