question

AmitRawat-3636 avatar image
0 Votes"
AmitRawat-3636 asked RaytheonXie-MSFT commented

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

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.


office-sharepoint-onlineoffice-sharepoint-server-development
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RaytheonXie-MSFT avatar image
0 Votes"
RaytheonXie-MSFT answered RaytheonXie-MSFT commented

Hi @AmitRawat-3636 ,
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.




· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @RaytheonXie-MSFT ,
SPList is a part of SSOM. Can't we get list owner by CSOM while targeting SP On Premise 2019.

0 Votes 0 ·

Hi @AmitRawat-3636 ,
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();
1 Vote 1 ·

Add updates to the solution so that users who encounter the similar issue can quickly find the answer

0 Votes 0 ·