How to get file owner of List in SharePoint 2016 and 2019 via CSOM C#.

ASR 671 Reputation points
2021-10-21T10:46:02.06+00:00

Hi, I need to fetch file owner of list. Below is my code.

clientContext.Load(list, l => l.Id, l => l.Title, l => l.BaseType, l => l.BaseTemplate, l => l.LastItemModifiedDate, l => l.Created,
l => l.DefaultViewUrl, l => l.Author.UserPrincipalName, l => l.Author.Title, l => l.ParentWebUrl, l => l.ParentWeb);
clientContext.ExecuteQueryWithIncrementalRetry();

Above code giving error stating "Field or property "Author" does not exist."
Able to fetch author property when targeting SP Online via CSOM. Also able to fetch when targeting SP OnPrem via SSOM.
But when doing it via CSOM - SP 2019 OnPrem, it is throwing error.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,680 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,575 questions
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 31,526 Reputation points Microsoft Vendor
    2021-10-22T06:20:10.413+00:00

    Hi @ASR ,
    Per my test, We can get file author by CSOM. Please refer to following code

    List targetList = clientcontext.Web.Lists.GetByTitle("Documents");  
    ListItem oItem = targetList.GetItemById(2);  
    CamlQuery oQuery = new CamlQuery();  
    oQuery.ViewXml = @"<View><Query><Where>  
    <Eq>  
    <FieldRef Name='LinkFilename' />  
    <Value Type='Text'>Document.docx</Value>  
    </Eq>  
    </Where></Query></View>";  
       
    ListItemCollection oItems = targetList.GetItems(oQuery);  
    clientcontext.Load(oItems);  
    clientcontext.ExecuteQuery();  
       
    oItem = oItems.FirstOrDefault();   
    File targetFile = oItem.File;   
    clientcontext.Load(targetFile);   
    User oUser = targetFile.Author;   
    clientcontext.Load(oUser);  
    clientcontext.ExecuteQuery();  
       
    Console.WriteLine("Author: " + oUser.LoginName);  
    

    If we need list owner. We need to use SPList.Author property returns the SPUser field.

            using(SPSite site = newSPSite(strURL))    
            {    
                using(SPWeb web = site.OpenWeb())    
                {    
                    SPList list = web.Lists.TryGetList(listName);    
                    if (list != null) {    
                        SPUser listAuthor = list.Author;    
                        string authorName = listAuthor.Name;    
                        Console.WriteLine("List Author Name: {0}", authorName);    
                        retry = false;    
                    } else    
                    {    
                        Console.WriteLine("List with the name \"{0}\" not exists Please try some other name.", listName);    
                        retry = true;    
                    }    
                }    
            }    
    

    ---------update---------

    We can get author.Title but it seems there is no UserPrincipalName property in SharePoint 2019. Here is the sample for reference

         Web web = clientContext.Web;  
         List list = web.Lists.GetByTitle("TestList");  
         clientContext.Load(list, l => l.SchemaXml);  
         clientContext.ExecuteQuery();  
         string schemaxml = list.SchemaXml;  
         XmlDocument xmlDoc = new XmlDocument();  
         xmlDoc.LoadXml(schemaxml);  
         XmlNode listElement = xmlDoc.GetElementsByTagName("List")[0];  
         string authorid = listElement.Attributes["Author"].Value;  
         User author = web.GetUserById(Convert.ToInt32(authorid));  
         clientContext.Load(author);  
         clientContext.ExecuteQuery();  
         Console.WriteLine(author.Title);  
         Console.ReadLine();  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



0 additional answers

Sort by: Most helpful