Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ExecuteQueryAsync for Microsoft SharePoint CSOM 15/16 (On Premise)
private static async Task CSOM15ExecuteAsync(ClientRuntimeContext clientContext)
{
try
{
//ScriptTypeMap.EnsureInited();
Assembly csomRuntime = Assembly.GetAssembly(typeof(ClientRuntimeContext));
Type scriptTypeMap = csomRuntime.GetType("Microsoft.SharePoint.Client.ScriptTypeMap");
InvokePrivateStaticMethod(scriptTypeMap, "EnsureInited");
//ClientRequest pendingRequest = PendingRequest;
ClientRequest pendingRequest = clientContext.PendingRequest;
//m_request = null;
SetPrivateMember(typeof(ClientRuntimeContext), clientContext, "m_request", null);
//pendingRequest.ExecuteQuery():
{
//m_requestStatus = ClientRequestStatus.InProgress;
SetPrivateMember(typeof(ClientRequest), pendingRequest, "m_requestStatus", ClientRequestStatus.InProgress);
//ChunkStringBuilder sb = BuildQuery();
var sb = InvokePrivateMethod(typeof(ClientRequest), pendingRequest, "BuildQuery");
//CleanupQuery();
InvokePrivateMethod(typeof(ClientRequest), pendingRequest, "CleanupQuery");
//ExecuteQueryToServer(sb):
{
//m_context.FireExecutingWebRequestEvent(new WebRequestEventArgs(pendingRequest.RequestExecutor));
var m_context = GetPrivateMember(typeof(ClientRequest), pendingRequest, "m_context") as ClientRuntimeContext;
InvokePrivateMethod(typeof(ClientRuntimeContext), m_context, "FireExecutingWebRequestEvent", new WebRequestEventArgs(pendingRequest.RequestExecutor));
pendingRequest.RequestExecutor.RequestContentType = "text/xml";
if (m_context.AuthenticationMode == ClientAuthenticationMode.Default)
{
pendingRequest.RequestExecutor.RequestHeaders["X-RequestForceAuthentication"] = "true";
}
object executeQueryMimeInfo = null;
Type ExecuteQueryMimeInfoType = csomRuntime.GetType("Microsoft.SharePoint.Client.ClientRequest+ExecuteQueryMimeInfo");
var SerializationContext = GetPrivateProperty(typeof(ClientRequest), pendingRequest, "SerializationContext") as SerializationContext;
var streams = GetPrivateProperty(typeof(SerializationContext), SerializationContext, "Streams");
if (streams != null /* && SerializationContext.Streams.Count > 0*/)
{
//executeQueryMimeInfo = new ExecuteQueryMimeInfo();
var constructor = ExecuteQueryMimeInfoType.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new Type[0], null);
executeQueryMimeInfo = constructor.Invoke(null);
var contentType = (string)ExecuteQueryMimeInfoType.GetProperty("ContentType").GetValue(executeQueryMimeInfo);
pendingRequest.RequestExecutor.RequestContentType = contentType;
}
var requestStream = pendingRequest.RequestExecutor.GetRequestStream();
if (executeQueryMimeInfo != null)
{
//WriteMimeStream(executeQueryMimeInfo, sb, requestStream);
InvokePrivateMethod(typeof(ClientRequest), pendingRequest, "WriteMimeStream", executeQueryMimeInfo, sb, requestStream);
}
else
{
Type ChunkStringBuilderType = csomRuntime.GetType("Microsoft.SharePoint.Client.ChunkStringBuilder");
//sb.WriteContentAsUTF8(requestStream);
InvokePublicMethod(ChunkStringBuilderType, sb, "WriteContentAsUTF8", requestStream);
}
await requestStream.FlushAsync();
requestStream.Close();
var spWebRequestExecutorType = csomRuntime.GetType("Microsoft.SharePoint.Client.SPWebRequestExecutor");
if (pendingRequest.RequestExecutor.GetType() == spWebRequestExecutorType)
{
//m_webRequest.GetRequestStream().Close();
var m_webRequest = GetPrivateMember(spWebRequestExecutorType, pendingRequest.RequestExecutor, "m_webRequest") as System.Net.HttpWebRequest;
var stream = await m_webRequest.GetRequestStreamAsync();
stream.Close();
//m_webResponse = (HttpWebResponse)m_webRequest.GetResponse();
var m_webResponse = await m_webRequest.GetResponseAsync();
SetPrivateMember(spWebRequestExecutorType, pendingRequest.RequestExecutor, "m_webResponse", m_webResponse);
}
else
{
pendingRequest.RequestExecutor.Execute();
}
InvokePrivateMethod(typeof(ClientRequest), pendingRequest, "ProcessResponse");
}
}
}
catch(Exception e)
{
if(e.InnerException != null)
{
throw e.InnerException;
}
else
{
throw e;
}
}
}
private static object InvokePrivateMethod(Type type, object obj, string methodName, params object[] arguments)
{
MethodInfo dynMethod = type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
return dynMethod.Invoke(obj, arguments);
}
private static object InvokePublicMethod(Type type, object obj, string methodName, params object[] arguments)
{
MethodInfo dynMethod = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance);
return dynMethod.Invoke(obj, arguments);
}
private static object InvokePrivateStaticMethod(Type type, string methodName, params object[] arguments)
{
MethodInfo dynMethod = type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Static);
return dynMethod.Invoke(type, arguments);
}
private static object GetPrivateMember(Type type, object obj, string memberName)
{
var fieldInfo = type.GetField(memberName, BindingFlags.Instance | BindingFlags.NonPublic);
return fieldInfo.GetValue(obj);
}
private static object GetPrivateProperty(Type type, object obj, string memberName)
{
var fieldInfo = type.GetProperty(memberName, BindingFlags.Instance | BindingFlags.NonPublic);
return fieldInfo.GetValue(obj);
}
private static void SetPrivateMember(Type type, object obj, string memberName, object value)
{
var fieldInfo = type.GetField(memberName, BindingFlags.Instance | BindingFlags.NonPublic);
fieldInfo.SetValue(obj, value);
}
private static object GetStaticPropertyValue(Type type, string propertyName)
{
var prop = type.GetProperty("Test", BindingFlags.Static | BindingFlags.NonPublic);
return prop.GetValue(type);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.