4

We can check if Web.Title is available this way:

if (!_clientContext.Web.IsPropertyAvailable("Title"))
{
  _clientContext.Load(_clientContext.Web, w => w.Title);
  _clientContext.ExecuteQuery()
}

but is this generic, meaning that property name always can be checked by finding the corresponding string name? Can we somehow check this without passing string as a parameter?

1

2 Answers 2

5

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))
     {
         //...
     }
} 
2
  • 2
    this seems great :) we could also slightly modify this to create an extension method "public static bool IsPropertyAvailable<T>(this T..." so we could call it like: "if (context.Web.IsPropertyAvailable(w => w.Title))"
    – thomius
    Commented Apr 23, 2014 at 21:17
  • Good idea about extension method Commented Apr 23, 2014 at 21:20
3

Since C# 6.0, you can use nameof in order to avoid the extra work that comes with Reflection. The nameof(property) reference is converted to a string during compilation.

        if (!site.IsPropertyAvailable(nameof(site.Id)))
        {
            context.Load(site, s => s.Id);
            context.ExecuteQuery();
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.