Retrieve The Author Of SharePoint List

SharePoint provides many options to access its objects. Here options refers to api’s, which has its own properties and methods. But all are similar and returns the same value when accessing the SharePoint.

Still SharePoint lacks in lot of areas across all API’s, because not all properties and methods are available in all object models. Always the SharePoint Server Object Model is top amongst all APIs when compared to other object models with respect to object members.

Here, we are going to see how to retrieve the author who creates the SharePoint list. There is a Author property available in Server Object Model, but other API’s doesn’t have this property. Maybe SharePoint team thinks this is not much used property, so they have didn’t given much importance for this one.

The following example Server Object Model code used to retrieve the author name of the SharePoint list or library.

DLL Reference: Microsoft.SharePoint

  1. Access the List based on given name.
  2. SPList.Author property returns the SPUser field.
  3. From the SPUser field, get the display name of the user.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint;  
  7.   
  8. namespacesomexamples.GetListAuthor  
  9. {  
  10.     classProgram  
  11.     {  
  12.         staticvoid Main(string[] args)   
  13.         {  
  14.             bool retry = false;  
  15.             RETRY_ENTRY:  
  16.                 //Get Site Url from user   
  17.                 Console.Write("Enter Site URL: ");  
  18.             string strURL = Console.ReadLine();  
  19.   
  20.             Console.Write("Enter List / Library Name: ");  
  21.             string listName = Console.ReadLine();  
  22.             Console.WriteLine();  
  23.   
  24.             try {  
  25.                 using(SPSite site = newSPSite(strURL))  
  26.                 {  
  27.                     using(SPWeb web = site.OpenWeb())  
  28.                     {  
  29.                         SPList list = web.Lists.TryGetList(listName);  
  30.                         if (list != null) {  
  31.                             SPUser listAuthor = list.Author;  
  32.                             string authorName = listAuthor.Name;  
  33.                             Console.WriteLine("List Author Name: {0}", authorName);  
  34.                             retry = false;  
  35.                         } else  
  36.                         {  
  37.                             Console.WriteLine("List with the name \"{0}\" not exists Please try some other name.", listName);  
  38.                             retry = true;  
  39.                         }  
  40.                     }  
  41.                 }  
  42.   
  43.             } catch (Exception ex)  
  44.             {  
  45.                 Console.WriteLine("Error: " + ex.Message);  
  46.                 retry = true;  
  47.             }  
  48.   
  49.             if (retry)  
  50.             {  
  51.                 Console.Write("Do you want to continue (y / n):");  
  52.                 ConsoleKeyInfo boolContinue = Console.ReadKey();  
  53.                 Console.WriteLine();  
  54.                 if (boolContinue.KeyChar == 'y')  
  55.                     goto RETRY_ENTRY;  
  56.             }  
  57.             Console.Write("Press any key to exit.");  
  58.             Console.Read();  
  59.         }  
  60.     }  
  61. }  
In Client Object Model, we will not have Author property for List object. We have to recall that, the SharePoint team creates the Server Object Model at first before all models. So SOM areas seems bigger than others. But there is a work around available in Client Object Model to get author for the SharePoint List.

We can use List.SchemaXML to retrieve all list properties associated to List object. It contains the id of the user who created the list. The following example from managed Client Object Model retrieves the author name from schemaXml property as a work around,

DLL Reference: Microsoft.SharePoint.Client, Microsoft.SharePoint.Client.Runtime
  1. Get the context of the url based on the given credentials.
  2. Get the List object based on the given title.
  3. Get the SchemaXml property of the List by sending the request to server.
  4. Get the author id from the schemaXml.
  5. Send the request to server to get the author name based in the id.
  1. using System;  
  2. using System.Linq;  
  3. using System.Net;  
  4. using System.Text;  
  5. using System.Xml;  
  6. using System.Collections.Generic;  
  7. using System.Threading.Tasks;  
  8.   
  9. namespacecsomexamples.GetListAuthor  
  10. {  
  11.     class Program  
  12.     {  
  13.         static void Main(string[] args)  
  14.         {  
  15.             //Get Site Url fro user   
  16.             Console.Write("Enter Site URL: ");  
  17.             string strURL = Console.ReadLine();  
  18.   
  19.             //Get Username from user in the format of (Domain/Login ID)   
  20.             Console.Write("Enter UserName (domain/userid): ");  
  21.             string strUserName = Console.ReadLine();  
  22.   
  23.             Console.Write("Enter your password: ");  
  24.             string pass = getPassword();  
  25.             Console.WriteLine();  
  26.             RETRY_ENTRY:  
  27.                 Console.Write("Enter List / Library Name: ");  
  28.             string listName = Console.ReadLine();  
  29.             Console.WriteLine();  
  30.   
  31.             try  
  32.             {  
  33.                 ClientContext ctx = new ClientContext(strURL);  
  34.                 ctx.Credentials = newNetworkCredential(strUserName, pass);  
  35.                 Web web = ctx.Web;  
  36.   
  37.                 List list = web.Lists.GetByTitle(listName);  
  38.   
  39.                 //Parameters to receive response from the server   
  40.                 ctx.Load(list, l => l.SchemaXml);  
  41.                 ctx.ExecuteQuery();  
  42.                 string schemaxml = list.SchemaXml;  
  43.   
  44.                 XmlDocument xmlDoc = newXmlDocument();  
  45.                 xmlDoc.LoadXml(schemaxml);  
  46.                 XmlNode listElement = xmlDoc.GetElementsByTagName("List")[0];  
  47.   
  48.                 string authorid = listElement.Attributes["Author"].Value;  
  49.                 User listAuthor = web.GetUserById(Convert.ToInt32(authorid));  
  50.                 ctx.Load(listAuthor);  
  51.                 ctx.ExecuteQuery();  
  52.   
  53.                 Console.WriteLine(listAuthor.Title);  
  54.             } catch (Exception ex)  
  55.             {  
  56.                 Console.WriteLine("Error: " + ex.Message);  
  57.                 Console.Write("Do you want to continue (y / n):");  
  58.                 ConsoleKeyInfo boolContinue = Console.ReadKey();  
  59.                 Console.WriteLine();  
  60.                 if (boolContinue.KeyChar == 'y')  
  61.                     goto RETRY_ENTRY;  
  62.                 else  
  63.                     Console.Write("Enter to Exit.");  
  64.             }  
  65.   
  66.             Console.Read();  
  67.         }  
  68.   
  69.         privatestaticstring getPassword()  
  70.         {  
  71.             ConsoleKeyInfo key;  
  72.             string pass = "";  
  73.             do   
  74.             {  
  75.                 key = Console.ReadKey(true);  
  76.                 // Backspace Should Not Work   
  77.                 if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter) {  
  78.                     pass += key.KeyChar;  
  79.                     Console.Write("*");  
  80.                 } else  
  81.                 {  
  82.                     if (key.Key == ConsoleKey.Backspace && pass.Length > 0) {  
  83.                         pass = pass.Substring(0, (pass.Length - 1));  
  84.                         Console.Write("\b \b");  
  85.                     }  
  86.                 }  
  87.             }  
  88.             // Stops Receving Keys Once Enter is Pressed   
  89.             while (key.Key != ConsoleKey.Enter);  
  90.             return pass;  
  91.         }  
  92.     }  
  93. }  

Summary

This article explains the missing property of author property in Client object model and how we can overcome that by using another property.

Up Next
    Ebook Download
    View all
    Learn
    View all