private SearchResultEntryCollection PagedQuery(ref SearchRequestExtender reqStore) { SearchResultEntryCollection ret = null; SearchResponse sresponse = null; bool goon = false; try { if ((base.IsConnected == false) || (base.Connection == null)) { Connect(reqStore.DC, reqStore.ReferralChasing, connectionLess: reqStore.QueryInfo.RootDse); } sresponse = (SearchResponse)base.Connection.SendRequest(reqStore.Request); goon = true; } catch (LdapException ldapEx) { base.SetError(ldapEx.Message); reqStore.HasError = true; reqStore.ErrorMSG = ldapEx.Message; return(null); } catch (DirectoryOperationException direx) { if (direx.Response != null) { if (direx.Response.ResultCode == ResultCode.SizeLimitExceeded) { if (reqStore.QueryInfo.AutoPage) { GlobalEventHandler.RaiseErrorOccured(String.Format("Non-PagedQuery: {0} - switched to PagedQuery", direx.Response.ResultCode.ToString())); reqStore.AddMessage(String.Format("Non-PagedQuery: {0} - switched to PagedQuery", direx.Response.ResultCode.ToString())); reqStore.PageCookie(((SearchResponse)direx.Response).Entries.Count); reqStore.MoreData = true; } else { sresponse = (SearchResponse)direx.Response; GlobalEventHandler.RaiseErrorOccured(String.Format("\tNon-PagedQuery: {0} - returned first {1} entries", direx.Response.ResultCode.ToString(), sresponse.Entries.Count)); reqStore.AddMessage(String.Format("Non-PagedQuery: {0} - returned first {1} entries", direx.Response.ResultCode.ToString(), sresponse.Entries.Count)); } goon = true; } else if ((direx.Response.ResultCode == ResultCode.UnavailableCriticalExtension) && direx.Response.ErrorMessage.StartsWith("00002040") && (reqStore.PageControl != null) && (reqStore.ReferralChasing != ReferralChasingOptions.None)) { reqStore.PageCount--; string msg = "Multiple page cookies from referrals."; msg = String.Format("{0} ({1})", msg, direx.Message); if (direx.Response.ErrorMessage != null) { msg = String.Format("{0}: ({1})", msg, direx.Response.ErrorMessage); } base.SetError(msg); reqStore.HasError = true; reqStore.ErrorMSG = base.ErrorMSG; //goon = true; } else { string msg = direx.Message; if (direx.Response.ErrorMessage != null) { msg = String.Format("{0}: ({1})", msg, direx.Response.ErrorMessage); } base.SetError(msg); reqStore.HasError = true; reqStore.ErrorMSG = base.ErrorMSG; } } else // if (!goon) { base.SetError(direx.Message); reqStore.HasError = true; reqStore.ErrorMSG = base.ErrorMSG; } } catch (Exception ex) { base.SetError(ex.Message); reqStore.HasError = true; reqStore.ErrorMSG = ex.Message; return(null); } if (goon) { if (sresponse != null) { if (reqStore.RetreiveStatistics) { DirectoryControl dcStats = GetControl(sresponse.Controls, SearchRequestExtender.STATISTCS_CONTROL_OID); if (dcStats != null) { reqStore.Statistics.Add(new StatsData(dcStats.GetValue())); } else { GlobalEventHandler.RaiseErrorOccured("WARNING: No Query Statistics data returned"); } } if (reqStore.PageControl != null) { DirectoryControl pageRespControl = GetControl(sresponse.Controls, reqStore.PageControl.Type); reqStore.UpdatePagingCookie(pageRespControl, sresponse.Entries.Count); } ret = sresponse.Entries; } } return(ret); }
public List <SearchResultEntry> Query(string dc, string searchBase, string ldapFilter, string[] propertiesToLoad, SearchScope scope, ReferralChasingOptions referralChasing, QueryControl queryInfo, string[] attributesRemebered = null, bool returnResults = false) { CancelToken = false; GlobalEventHandler.ClearSearchCancelled(); GlobalEventHandler.SearchCancelled += ReceivedCancellation; List <SearchResultEntry> ret = new List <SearchResultEntry> { }; List <SearchResultEntry> fire = new List <SearchResultEntry> { }; QueryHasConstructedAttribute(propertiesToLoad, scope, ref queryInfo); if (queryInfo.MustGetSingleObjectPath) { GetSingleObjectPaths(dc, searchBase, ldapFilter, propertiesToLoad, scope, referralChasing, queryInfo); return(ret); } ForestBase.CurrentPorts.SelectedPort = queryInfo.Port; byte[] pagingCookie = null; SearchRequestExtender reqstore = new SearchRequestExtender(searchBase, ldapFilter, propertiesToLoad, scope, pagingCookie, queryInfo); reqstore.DC = dc; reqstore.ReferralChasing = referralChasing; propertiesToLoad = reqstore.Attributes; while (true) { if (!reqstore.HasError) { reqstore.PageCount++; } SearchResultEntryCollection colresult = null; if (!CancelToken) { if (!queryInfo.PerformDirSync) { DateTime starttime = DateTime.Now; colresult = PagedQuery(ref reqstore); if (reqstore.CurrentPageSize > 0) { GlobalEventHandler.RaiseMessageOccured(String.Format("Page: {0} ({1}) [{2}] ms]", reqstore.PageCount, reqstore.CurrentPageSize, DateTime.Now.Subtract(starttime).TotalMilliseconds)); } } else { colresult = DirSyncQuery(ref reqstore); } } else { break; } if (reqstore.HasError) { break; } if ((colresult != null) && (colresult.Count > 0) && ((colresult[0].DistinguishedName.Length != 0) || (searchBase == ""))) { SearchResultEntry[] temp = new SearchResultEntry[colresult.Count]; colresult.CopyTo(temp, 0); fire = temp.ToList(); if (returnResults) { ret.AddRange(fire); } } if ((queryInfo.CurrentResultEventType == QUERY_RESULT_EVENT_TYPE.FROM_SINGLE_OBJECT_PATH) && (attributesRemebered != null)) { reqstore.HandleAttributes(attributesRemebered); } if (reqstore.MoreData) { if ((fire.Count > 0) && !returnResults) { GlobalEventHandler.RaiseQueryCompleted(fire, reqstore, QUERY_RESULT_EVENT_TYPE.IS_PARTIAL | queryInfo.CurrentResultEventType); } if ((queryInfo.CurrentResultEventType == QUERY_RESULT_EVENT_TYPE.FROM_SINGLE_OBJECT_PATH) && (attributesRemebered != null)) { reqstore.HandleAttributes(propertiesToLoad); } } else { break; } } if (reqstore.DoPaging) { reqstore.AddMessage(String.Format("PageSize: {0}", reqstore.CurrentPageSize)); reqstore.AddMessage(String.Format("Pages: {0}", reqstore.PageCount)); } if (!returnResults) { GlobalEventHandler.RaiseQueryCompleted(fire, reqstore, QUERY_RESULT_EVENT_TYPE.IS_COMPLETED | queryInfo.CurrentResultEventType); } Disconnect(); return(ret); }