In SharePoint CSOM ClientObject.IsPropertyAvailable method accepts property name as a string only, but you could utilize the following method that accepts property as an expression:
/// <summary>
/// Determines if Client Object property is loaded
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="clientObject"></param>
/// <param name="property"></param>
/// <returns></returns>
private static bool IsPropertyAvailable<T>(T clientObject, Expression<Func<T, object>> property) where T : ClientObject
{
var expression = (MemberExpression)property.Body;
string propName = expression.Member.Name;
return clientObject.IsPropertyAvailable(propName);
}
Usage
if(!IsPropertyAvailable(web, w => w.Title))
{
context.Load(context.Web, w => w.Title);
context.ExecuteQuery();
Console.WriteLine(web.Title);
}
Update
Since both ClientObject.IsPropertyAvailable and ClientObject.IsObjectPropertyInstantiated methods are used to identify whether the specified property has been retrieved or not (in first case for scalar
properties, in the second one for client object collection
properties), below is provided a more generic method:
public static class ClientObjectExtensions
{
/// <summary>
/// Determines whether Client Object property is loaded
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="clientObject"></param>
/// <param name="property"></param>
/// <returns></returns>
public static bool IsPropertyAvailableOrInstantiated<T>(this T clientObject, Expression<Func<T, object>> property)
where T : ClientObject
{
var expression = (MemberExpression)property.Body;
var propName = expression.Member.Name;
var isCollection = typeof(ClientObjectCollection).IsAssignableFrom(property.Body.Type);
return isCollection ? clientObject.IsObjectPropertyInstantiated(propName) : clientObject.IsPropertyAvailable(propName);
}
}
Usage
using (var ctx = new ClientContext(webUri))
{
ctx.Load(ctx.Web, w => w.Lists, w => w.Title);
ctx.ExecuteQuery();
if (ctx.Web.IsPropertyAvailableOrInstantiated(w => w.Title))
{
//...
}
if (ctx.Web.IsPropertyAvailableOrInstantiated(w => w.Lists))
{
//...
}
}