public static void WaitForWorkflowToComplete(string asyncoperationid, string callbackFunction, string errorCallback, DateTime startTime) { if (startTime == null) { startTime = DateTime.Now; } OrganizationServiceProxy.BeginRetrieve("asyncoperation", asyncoperationid, new string[] { "statecode", "statuscode" }, delegate(object state) { Entity response = OrganizationServiceProxy.EndRetrieve(state, typeof(Entity)); OptionSetValue statuscode = response.GetAttributeValueOptionSet("statuscode"); // Check if completed/failed/cancelled (30/31/32) if (statuscode != null && statuscode.Value == 30) { // Call the callbackfunction Script.Eval(callbackFunction); } else if ((statuscode != null && statuscode.Value >= 31) || ((DateTime.Now - startTime) > 180000)) { // Timeout after 1 minute // Call the error callbackfunction Script.Eval(errorCallback); } else { // Poll again in another second Window.SetTimeout(delegate() { WaitForWorkflowToComplete(asyncoperationid, callbackFunction, errorCallback, startTime); }, 1000); } }); }