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
- Access the List based on given name.
- SPList.Author property returns the SPUser field.
- From the SPUser field, get the display name of the user.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint;
-
- namespacesomexamples.GetListAuthor
- {
- classProgram
- {
- staticvoid Main(string[] args)
- {
- bool retry = false;
- RETRY_ENTRY:
-
- Console.Write("Enter Site URL: ");
- string strURL = Console.ReadLine();
-
- Console.Write("Enter List / Library Name: ");
- string listName = Console.ReadLine();
- Console.WriteLine();
-
- try {
- 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;
- }
- }
- }
-
- } catch (Exception ex)
- {
- Console.WriteLine("Error: " + ex.Message);
- retry = true;
- }
-
- if (retry)
- {
- Console.Write("Do you want to continue (y / n):");
- ConsoleKeyInfo boolContinue = Console.ReadKey();
- Console.WriteLine();
- if (boolContinue.KeyChar == 'y')
- goto RETRY_ENTRY;
- }
- Console.Write("Press any key to exit.");
- Console.Read();
- }
- }
- }
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 - Get the context of the url based on the given credentials.
- Get the List object based on the given title.
- Get the SchemaXml property of the List by sending the request to server.
- Get the author id from the schemaXml.
- Send the request to server to get the author name based in the id.
- using System;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Xml;
- using System.Collections.Generic;
- using System.Threading.Tasks;
-
- namespacecsomexamples.GetListAuthor
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- Console.Write("Enter Site URL: ");
- string strURL = Console.ReadLine();
-
-
- Console.Write("Enter UserName (domain/userid): ");
- string strUserName = Console.ReadLine();
-
- Console.Write("Enter your password: ");
- string pass = getPassword();
- Console.WriteLine();
- RETRY_ENTRY:
- Console.Write("Enter List / Library Name: ");
- string listName = Console.ReadLine();
- Console.WriteLine();
-
- try
- {
- ClientContext ctx = new ClientContext(strURL);
- ctx.Credentials = newNetworkCredential(strUserName, pass);
- Web web = ctx.Web;
-
- List list = web.Lists.GetByTitle(listName);
-
-
- ctx.Load(list, l => l.SchemaXml);
- ctx.ExecuteQuery();
- string schemaxml = list.SchemaXml;
-
- XmlDocument xmlDoc = newXmlDocument();
- xmlDoc.LoadXml(schemaxml);
- XmlNode listElement = xmlDoc.GetElementsByTagName("List")[0];
-
- string authorid = listElement.Attributes["Author"].Value;
- User listAuthor = web.GetUserById(Convert.ToInt32(authorid));
- ctx.Load(listAuthor);
- ctx.ExecuteQuery();
-
- Console.WriteLine(listAuthor.Title);
- } catch (Exception ex)
- {
- Console.WriteLine("Error: " + ex.Message);
- Console.Write("Do you want to continue (y / n):");
- ConsoleKeyInfo boolContinue = Console.ReadKey();
- Console.WriteLine();
- if (boolContinue.KeyChar == 'y')
- goto RETRY_ENTRY;
- else
- Console.Write("Enter to Exit.");
- }
-
- Console.Read();
- }
-
- privatestaticstring getPassword()
- {
- ConsoleKeyInfo key;
- string pass = "";
- do
- {
- key = Console.ReadKey(true);
-
- if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter) {
- pass += key.KeyChar;
- Console.Write("*");
- } else
- {
- if (key.Key == ConsoleKey.Backspace && pass.Length > 0) {
- pass = pass.Substring(0, (pass.Length - 1));
- Console.Write("\b \b");
- }
- }
- }
-
- while (key.Key != ConsoleKey.Enter);
- return pass;
- }
- }
- }
Summary
This article explains the missing property of author property in Client object model and how we can overcome that by using another property.