3

During some Read/Write/Update operations SharePoint Client Object model returns '(503) Service Unavailable' exception, reattempt solve this problem. Here, re-attempt Operation creates a new collection of return values and we are not able to assign it back to the original return value object.

Note: Return value of LoadQuery() method is present in 'ClientQueryableResult.m_data' private variable.

We come up with below reflection code. But the problem is we are not sure whether it is safe to use Reflection with SharePoint Client object module to read one of it's private variable value?

e.g. Loading SharePoint Groups we have

var groups = _ClientContext.LoadQuery(_ClientContext.Web.SiteGroups);

_ClientContext.ExecuteQuery();

Below code caches LoadQuery() parameters and use it in exception case for reattempt

object OrgResult, NewResult, Params;
    Params = clientObjects
    OrgResult = _ClientContext.LoadQuery(clientObjects);
    try   {_ClientContext.ExecuteQuery();}
    catch (WebException webEx){
    NewResult = _ClientContext.LoadQuery(Params);
    _ClientContext.ExecuteQuery();
    object data = NewResult.GetPrivateFieldValue("m_data");
    if (data != null)
        OrgResult.SetPrivateFieldValue("m_data", data);
    }

    // Reflection method to read private value
    public static object GetPrivateFieldValue(this object src, string fieldName)
    {
    object value = null;
    FieldInfo fieldInfo = src.GetType().GetField(fieldName, BindingFlags.NonPublic
                                                |BindingFlags.Instance);
    if (fieldInfo != null)
    value = fieldInfo.GetValue(src);
    return value;
    }
1
  • 1
    It depends on what you mean by "safe". Using reflection to read private variables can never be considered safe because an update (eg service pack), might change the code internals, making your code break.
    – MdMazzotti
    Commented Apr 1, 2014 at 10:50

1 Answer 1

1

Using non-Public methods is never "supported" by the SharePoint Product Group (for example, a PowerShell script that reads a value from a private property). While I'd be more comfortable running a PowerShell script to read non-public properties, doing so in a product (be it internal or external) is not something I would do as I could not get Microsoft support if I encountered an issue.

0

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.