public static void ExecuteAppOnlyClientContextQuery <TContext>(ISPContext spContext, Func <TContext, Action> action) where TContext : ClientContext { if (action == null) { throw new ArgumentNullException("action"); } if (spContext != null) { TContext clientContext = (TContext)spContext.CreateAppOnlyClientContextForSPHost(); if (clientContext != null) { using (clientContext) { Action result = action.Invoke(clientContext); clientContext.ExecuteQuery(); if (result != null) { result.Invoke(); } } } } }