private void ProcessSearch(FolderId oFolderId, int iPageSize) { int iLines = 0; bool bError = false; int iCount = 0; FindFoldersResults oFindFoldersResults = null; if (oFolderId != null) { //FindItemsResults<Item> oFindFoldersResults = null; if (this.cmboSearchType.Text == "Direct") { List <SearchFilter> searchFilterCollection = new List <SearchFilter>(); FolderView oFolderView = new FolderView(iPageSize); oFolderView.PropertySet = new PropertySet(BasePropertySet.IdOnly, FolderSchema.DisplayName, FolderSchema.FolderClass, FolderSchema.TotalCount, FolderSchema.UnreadCount, Prop_PR_FOLDER_PATH, Prop_PR_IS_HIDDEN ); //oFolderView.OrderBy.Add(FolderSchema.DisplayName SortDirection.Descending); SetSearchDepth(ref oFolderView); if (this.chkClass.Checked == true) { AddCondition(ref searchFilterCollection, FolderSchema.FolderClass, this.txtClass.Text, cmboClassConditional.Text); } if (this.chkDisplayName.Checked == true) { AddCondition(ref searchFilterCollection, FolderSchema.DisplayName, this.txtDisplayName.Text, cmboDisplayNameConditional.Text); } SearchFilter searchFilter = null; if (searchFilterCollection.Count == 0) { try { _CurrentService.ClientRequestId = Guid.NewGuid().ToString(); // Set a new GUID. oFindFoldersResults = _CurrentService.FindFolders(oFolderId, oFolderView); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error calling FindFolders"); bError = true; } } else { if (cmboLogicalOperation.Text == "And") { searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection.ToArray()); } if (cmboLogicalOperation.Text == "Or") { searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray()); } try { _CurrentService.ClientRequestId = Guid.NewGuid().ToString(); // Set a new GUID. oFindFoldersResults = _CurrentService.FindFolders(oFolderId, searchFilter, oFolderView); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error calling FindFolders"); bError = true; } } if (bError == true) { return; } SetHeaders(this.cmboSearchType.Text); // Driect ListViewItem oListItem = null; iCount = 0; foreach (Folder oFolder in oFindFoldersResults.Folders) { iCount++; oListItem = new ListViewItem(iCount.ToString(), 0); oListItem.SubItems.Add(oFolder.DisplayName); oListItem.SubItems.Add(oFolder.FolderClass); oListItem.SubItems.Add(oFolder.TotalCount.ToString()); oListItem.SubItems.Add(oFolder.UnreadCount.ToString()); Object oHidden = null; bool bHidden = false; if (oFolder.TryGetProperty(Prop_PR_IS_HIDDEN, out oHidden)) { bHidden = (bool)oHidden; oListItem.SubItems.Add(bHidden.ToString()); } else { oListItem.SubItems.Add(""); } string sPath = string.Empty; EwsFolderHelper.GetFolderPath(oFolder, ref sPath); oListItem.SubItems.Add(sPath); oListItem.SubItems.Add(oFolder.Id.UniqueId); oListItem.Tag = new FolderTag(oFolder.Id, oFolder.FolderClass); lvItems.Items.AddRange(new ListViewItem[] { oListItem });; oListItem = null; //} iLines++; } oListItem = null; } if (this.cmboSearchType.Text == "More Available") { // http://msdn.microsoft.com/en-us/library/exchange/dd633698(v=exchg.80).aspx int offset = 0; List <SearchFilter> searchFilterCollection = new List <SearchFilter>(); FolderView oFolderView = null; bool MoreItems = true; ListViewItem oListItem = null; SetHeaders(this.cmboSearchType.Text); // more availible int iCountMore = 0; while (MoreItems) { iCountMore++; oFolderView = new FolderView(iPageSize, offset, OffsetBasePoint.Beginning); oFolderView.PropertySet = new PropertySet(BasePropertySet.IdOnly, FolderSchema.DisplayName, FolderSchema.FolderClass, FolderSchema.TotalCount, FolderSchema.UnreadCount, Prop_PR_FOLDER_PATH, Prop_PR_IS_HIDDEN ); SetSearchDepth(ref oFolderView); if (this.chkClass.Checked == true) { AddCondition(ref searchFilterCollection, FolderSchema.FolderClass, this.txtClass.Text, cmboClassConditional.Text); } if (this.chkDisplayName.Checked == true) { AddCondition(ref searchFilterCollection, FolderSchema.DisplayName, this.txtDisplayName.Text, cmboDisplayNameConditional.Text); } SearchFilter searchFilter = null; //Array arrCollection = searchFilterCollection.ToArray(); if (searchFilterCollection.Count == 0) { try { _CurrentService.ClientRequestId = Guid.NewGuid().ToString(); // Set a new GUID. oFindFoldersResults = _CurrentService.FindFolders(oFolderId, oFolderView); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error calling FindFolders"); bError = true; } } else { if (cmboLogicalOperation.Text == "And") { searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection.ToArray()); } if (cmboLogicalOperation.Text == "Or") { searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray()); } try { _CurrentService.ClientRequestId = Guid.NewGuid().ToString(); // Set a new GUID. oFindFoldersResults = _CurrentService.FindFolders(oFolderId, searchFilter, oFolderView); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error calling FindFolders"); bError = true; } } if (bError == true) { return; } iCount = 0; foreach (Folder oFolder in oFindFoldersResults.Folders) { iCount++; oListItem = new ListViewItem(iCountMore.ToString() + ":" + iCount.ToString(), 0); oListItem.SubItems.Add(oFolder.DisplayName); oListItem.SubItems.Add(oFolder.FolderClass); oListItem.SubItems.Add(oFolder.TotalCount.ToString()); oListItem.SubItems.Add(oFolder.UnreadCount.ToString()); Object oHidden = null; bool bHidden = false; if (oFolder.TryGetProperty(Prop_PR_IS_HIDDEN, out oHidden)) { bHidden = (bool)oHidden; oListItem.SubItems.Add(bHidden.ToString()); } else { oListItem.SubItems.Add(""); } string sPath = string.Empty; EwsFolderHelper.GetFolderPath(oFolder, ref sPath); oListItem.SubItems.Add(sPath); oListItem.SubItems.Add(oFolder.Id.UniqueId); oListItem.Tag = new FolderTag(oFolder.Id, oFolder.FolderClass); lvItems.Items.AddRange(new ListViewItem[] { oListItem });; oListItem = null; iLines++; } // Set the flag to discontinue paging. if (!oFindFoldersResults.MoreAvailable) { MoreItems = false; } // Update the offset if there are more items to page. if (MoreItems) { offset += iPageSize; } } } } }
static void FindFoldersWithPolicyTag(ExchangeService oExchangeService, WellKnownFolderName oParentFolder, string sPolicyTag, ref ListView oListView, FolderTraversal oFolderTraversal) { int pageSize = 5; int offset = 0; FolderView oFolderView = new FolderView(pageSize + 1, offset); //ItemView view = new ItemView(pageSize + 1, offset); PropertySet oPropertySet = new PropertySet(BasePropertySet.IdOnly); oPropertySet.Add(FolderSchema.DisplayName); oPropertySet.Add(FolderSchema.FolderClass); oPropertySet.Add(Prop_PR_POLICY_TAG); oPropertySet.Add(Prop_PR_RETENTION_FLAGS); oPropertySet.Add(Prop_PR_RETENTION_PERIOD); //oPropertySet.Add(Prop_PR_RETENTION_DATE); //oPropertySet.Add(Prop_PR_ARCHIVE_TAG); //oPropertySet.Add(Prop_PR_ARCHIVE_PERIOD); oPropertySet.Add(FolderSchema.TotalCount); oPropertySet.Add(Prop_PR_FOLDER_PATH); oPropertySet.Add(Prop_PR_IS_HIDDEN); oListView.Columns.Add("Id", 200, HorizontalAlignment.Left); oListView.Columns.Add("FolderName", 250, HorizontalAlignment.Left); oListView.Columns.Add("Folder Path", 400, HorizontalAlignment.Left); oListView.Columns.Add("PR_POLICY_TAG", 100, HorizontalAlignment.Left); oListView.Columns.Add("PR_RETENTION_FLAGS", 100, HorizontalAlignment.Left); oListView.Columns.Add("PR_RETENTION_Period", 100, HorizontalAlignment.Left); oListView.Columns.Add("PR_IS_HIDDEN", 100, HorizontalAlignment.Left); oListView.Columns.Add("TotalCount", 100, HorizontalAlignment.Left); oFolderView.PropertySet = oPropertySet; oFolderView.Traversal = oFolderTraversal; string sPath = string.Empty;; FindFoldersResults oFindFoldersResults = null; ListViewItem oListViewItem = null; bool moreItems = true; //FolderId anchorId = null; while (moreItems) { try { oExchangeService.ClientRequestId = Guid.NewGuid().ToString(); // Set a new GUID. oFindFoldersResults = oExchangeService.FindFolders(oParentFolder, oFolderView); moreItems = oFindFoldersResults.MoreAvailable; if (moreItems) { oFolderView.Offset += pageSize; } int displayCount = oFindFoldersResults.Folders.Count > pageSize ? pageSize : oFindFoldersResults.Folders.Count; string sPR_POLICY_TAG = string.Empty; string iPR_RETENTION_FLAGS = string.Empty; string iPR_RETENTION_PERIOD = string.Empty; //private static ExtendedPropertyDefinition Prop_PR_POLICY_TAG = new ExtendedPropertyDefinition(0x3019, MapiPropertyType.Binary); // PR_POLICY_TAG 0x3019 Data type: PtypBinary, 0x0102 //private static ExtendedPropertyDefinition Prop_PR_RETENTION_FLAGS = new ExtendedPropertyDefinition(0x301D, MapiPropertyType.Integer); // PR_RETENTION_FLAGS 0x301D //private static ExtendedPropertyDefinition Prop_PR_RETENTION_PERIOD = new ExtendedPropertyDefinition(0x301A, MapiPropertyType.Integer); // PR_RETENTION_PERIOD 0x301A for (int i = 0; i < displayCount; i++) { Folder oFolder = oFindFoldersResults.Folders[i]; //string sResult = EwsExtendedPropertyHelper.GetExtendedProp_Byte_AsString(oFolder, Prop_PR_POLICY_TAG); //sResult = EwsExtendedPropertyHelper.GetExtendedProp_Int_AsString(oFolder, Prop_PR_RETENTION_FLAGS); //sResult = EwsExtendedPropertyHelper.GetExtendedProp_Int_AsString(oFolder, Prop_PR_RETENTION_PERIOD); string sFrom = EwsExtendedPropertyHelper.GetExtendedProp_Byte_AsString(oFolder, Prop_PR_POLICY_TAG); if (sFrom == "") { sPR_POLICY_TAG = ""; } else { byte[] oFromBytes = System.Convert.FromBase64String(sFrom); Guid guidTemp = new Guid(oFromBytes); sPR_POLICY_TAG = guidTemp.ToString(); } string sPR_RETENTION_FLAGS = EwsExtendedPropertyHelper.GetExtendedProp_Int_AsString(oFolder, Prop_PR_RETENTION_FLAGS); string sPR_RETENTION_PERIOD = EwsExtendedPropertyHelper.GetExtendedProp_Int_AsString(oFolder, Prop_PR_RETENTION_PERIOD); if (sPR_POLICY_TAG != "") { Console.WriteLine("Folder: " + oFolder.DisplayName + " Policy: " + sPR_POLICY_TAG); } if (sPR_POLICY_TAG == sPolicyTag) { oListViewItem = oListView.Items.Add(oFolder.Id.ToString()); oListViewItem.SubItems.Add(oFolder.DisplayName); EwsFolderHelper.GetFolderPath(oFolder, ref sPath); oListViewItem.SubItems.Add(sPath); oListViewItem.SubItems.Add(sPR_POLICY_TAG); // Policy oListViewItem.SubItems.Add(sPR_RETENTION_FLAGS); oListViewItem.SubItems.Add(sPR_RETENTION_PERIOD); //oListViewItem.SubItems.Add(EwsExtendedPropertyHelper.GetExtendedProp_Byte_AsString(oFolder, Prop_PR_RETENTION_PERIOD).ToString()); oListViewItem.SubItems.Add(EwsExtendedPropertyHelper.GetExtendedProp_Bool_AsString(oFolder, Prop_PR_IS_HIDDEN).ToString()); oListViewItem.SubItems.Add(FolderSchema.TotalCount.ToString()); } } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error while paging results"); return; } } }
// LoadFolder // This will load an Folder by ID. // Test code to load a folder object with many properties - modify as needed for your code. private bool LoadFolder(ExchangeService oService, FolderId oFolderId, out Folder oFolder) { // https://blogs.msdn.microsoft.com/akashb/2013/06/13/generating-a-report-which-folders-have-a-personal-tag-applied-to-it-using-ews-managed-api-from-powershell-exchange-2010/ bool bRet = true; Folder oReturnFolder = null; oFolder = null; List <FolderId> oFolders = new List <FolderId>(); oFolders.Add(oFolderId); ExtendedPropertyDefinition Prop_IsHidden = new ExtendedPropertyDefinition(0x10f4, MapiPropertyType.Boolean); // Folder Path ExtendedPropertyDefinition Prop_PR_FOLDER_PATH = new ExtendedPropertyDefinition(26293, MapiPropertyType.String); // PR_POLICY_TAG 0x3019 Data type: PtypBinary, 0x0102 ExtendedPropertyDefinition Prop_PR_POLICY_TAG = new ExtendedPropertyDefinition(0x3019, MapiPropertyType.Binary); // PR_RETENTION_TAG 0x3019 (12313) //ExtendedPropertyDefinition Prop_Retention_Tag = new ExtendedPropertyDefinition(0x3019, MapiPropertyType.Integer); // PR_RETENTION_FLAGS 0x301D (12317) PtypInteger32 ExtendedPropertyDefinition Prop_Retention_Flags = new ExtendedPropertyDefinition(0x301D, MapiPropertyType.Integer); // PR_RETENTION_PERIOD 0x301A (12314) PtypInteger32, 0x0003 ExtendedPropertyDefinition Prop_Retention_Period = new ExtendedPropertyDefinition(0x301A, MapiPropertyType.Integer); // http://gsexdev.blogspot.com/2011/04/using-ews-to-calculate-age-of-items-and.html#!/2011/04/using-ews-to-calculate-age-of-items-and.html // PR_FOLDER_TYPE 0x3601 (13825) ExtendedPropertyDefinition Prop_PR_FOLDER_TYPE = new ExtendedPropertyDefinition(0x3601, MapiPropertyType.Integer); // PR_MESSAGE_SIZE_EXTENDED 0x0E08 (3592) PT_I8 https://msdn.microsoft.com/en-us/library/office/cc839933.aspx ExtendedPropertyDefinition Prop_PR_MESSAGE_SIZE_EXTENDED = new ExtendedPropertyDefinition(0x0E08, MapiPropertyType.Long); // PR_DELETED_MESSAGE_SIZE_EXTENDED 0x669B (26267) PtypInteger64, 0x0014 ExtendedPropertyDefinition Prop_PR_DELETED_MESSAGE_SIZE_EXTENDED = new ExtendedPropertyDefinition(0x669B, MapiPropertyType.Long); // PR_ATTACH_ON_NORMAL_MSG_COUNT 0x66B1 ExtendedPropertyDefinition Prop_PR_ATTACH_ON_NORMAL_MSG_COUNT = new ExtendedPropertyDefinition(0x66B1, MapiPropertyType.Long); // PR_ARCHIVE_TAG 0x3018 ExtendedPropertyDefinition Prop_PidTagPolicyTag = new ExtendedPropertyDefinition(0x66B1, MapiPropertyType.Long); // private static ExtendedPropertyDefinition Prop_PidTagArchiveTag = new ExtendedPropertyDefinition(0x3018, MapiPropertyType.Binary); // Guid of Archive tag - PR_ARCHIVE_TAG - PidTagArchiveTag // private static ExtendedPropertyDefinition Prop_PidTagPolicyTag = new ExtendedPropertyDefinition(0x3019, MapiPropertyType.Integer); // Item - PidTagPolicyTag - PR_POLICY_TAG // private static ExtendedPropertyDefinition Prop_PidTagRetentionPeriod = new ExtendedPropertyDefinition(0x301A, MapiPropertyType.Integer); // Message - PidTagRetentionPeriod - PR_RETENTION_PERIOD PropertySet oPropertySet = new PropertySet( BasePropertySet.IdOnly, FolderSchema.FolderClass, FolderSchema.DisplayName, Prop_IsHidden); // Add more properties? oPropertySet.Add(FolderSchema.TotalCount); oPropertySet.Add(FolderSchema.UnreadCount); //oPropertySet.Add(FolderSchema.WellKnownFolderName); oPropertySet.Add(FolderSchema.ChildFolderCount); // oPropertySet.Add(Prop_FolderPath); //oPropertySet.Add(Prop_Retention_Tag); oPropertySet.Add(Prop_Retention_Period); oPropertySet.Add(Prop_PR_POLICY_TAG); oPropertySet.Add(Prop_Retention_Flags); oPropertySet.Add(Prop_PR_FOLDER_TYPE); oPropertySet.Add(Prop_PR_FOLDER_PATH); oPropertySet.Add(Prop_PR_MESSAGE_SIZE_EXTENDED); oPropertySet.Add(Prop_PR_DELETED_MESSAGE_SIZE_EXTENDED); oPropertySet.Add(Prop_PR_ATTACH_ON_NORMAL_MSG_COUNT); // https://blogs.msdn.microsoft.com/akashb/2011/08/10/stamping-retention-policy-tag-using-ews-managed-api-1-1-from-powershellexchange-2010/ StringBuilder oSB = new StringBuilder(); int iVal = 0; long lVal = 0; string sVal = string.Empty; object oVal = null; ServiceResponseCollection <GetFolderResponse> oGetFolderResponses = oService.BindToFolders(oFolders, oPropertySet); foreach (GetFolderResponse oGetItemResponse in oGetFolderResponses) { switch (oGetItemResponse.Result) { case ServiceResult.Success: oReturnFolder = oGetItemResponse.Folder; oSB.AppendFormat("DisplayName: {0}\r\n", oReturnFolder.DisplayName); oSB.AppendFormat("FolderClass: {0}\r\n", oReturnFolder.FolderClass); if (oReturnFolder.TryGetProperty(Prop_PR_FOLDER_TYPE, out iVal)) { oSB.AppendFormat("PR_FOLDER_TYPE: {0}\r\n", iVal); } else { oSB.AppendLine("PR_FOLDER_TYPE: Not found."); } string sPath = string.Empty; if (EwsFolderHelper.GetFolderPath(oReturnFolder, ref sPath)) { oSB.AppendFormat("Prop_PR_FOLDER_PATH: {0}\r\n", sPath); } else { oSB.AppendLine("Prop_PR_FOLDER_PATH: Not found."); } if (oReturnFolder.TryGetProperty(Prop_PR_MESSAGE_SIZE_EXTENDED, out lVal)) { oSB.AppendFormat("PR_MESSAGE_SIZE_EXTENDED: {0}\r\n", lVal); } else { oSB.AppendLine("PR_MESSAGE_SIZE_EXTENDED: Not found."); } if (oReturnFolder.TryGetProperty(Prop_PR_DELETED_MESSAGE_SIZE_EXTENDED, out lVal)) { oSB.AppendFormat("PR_DELETED_MESSAGE_SIZE_EXTENDED: {0}\r\n", lVal); } //else // oSB.AppendLine("PR_DELETED_MESSAGE_SIZE_EXTENDED: Not found."); if (oReturnFolder.TryGetProperty(Prop_PR_ATTACH_ON_NORMAL_MSG_COUNT, out lVal)) { oSB.AppendFormat("PR_ATTACH_ON_NORMAL_MSG_COUNT: {0}\r\n", lVal); } else { oSB.AppendLine("PR_ATTACH_ON_NORMAL_MSG_COUNT: Not found."); } MessageBox.Show(oSB.ToString(), "ServiceResult.Success"); break; case ServiceResult.Error: string sError = "ErrorCode: " + oGetItemResponse.ErrorCode.ToString() + "\r\n" + "\r\nErrorMessage: " + oGetItemResponse.ErrorMessage + "\r\n"; if (oGetItemResponse.ErrorProperties.Count > 0) { sError += "\r\nErrorProperties: "; foreach (ExtendedPropertyDefinition x in oGetItemResponse.ErrorProperties) { if (x.Name != null) { sError += "\r\n Name: " + x.Name; } if (x.Tag != null) { //sError += "\r\n Tag: " + x.Tag; iVal = (int)x.Tag; if (iVal == 13825) { sError += string.Format("\r\n PR_FOLDER_TYPE Hex: {00:X} Int: {1} ", iVal, iVal); } if (iVal == 26293) { sError += string.Format("\r\n Prop_PR_FOLDER_PATH Hex: {00:X} Int: {1} ", iVal, iVal); } if (iVal == 3592) { sError += string.Format("\r\n PR_MESSAGE_SIZE_EXTENDED Hex: {00:X} Int: {1} ", iVal, iVal); } if (iVal == 26267) { sError += string.Format("\r\n PR_DELETED_MESSAGE_SIZE_EXTENDED Hex: {00:X} Int: {1} ", iVal, iVal); } } if (x.Type != null) { sError += "\r\n Type: " + x.Type; } // sHex = string.Format("{00:X}", iValue).PadLeft(2, '0'); } //for (int i= 0; i < oGetItemResponse.ErrorProperties.Count; i++) //{ //} } MessageBox.Show(sError, "ServiceResult.Error"); break; case ServiceResult.Warning: string sWarning = "ErrorCode: " + oGetItemResponse.ErrorCode.ToString() + "\r\n" + "\r\nErrorMessage: " + oGetItemResponse.ErrorMessage + "\r\n"; MessageBox.Show(sWarning, "ServiceResult.Warning"); break; //default: // // Should never get here. // string sSomeError = // "ErrorCode: " + oGetItemResponse.ErrorCode.ToString() + "\r\n" + // "\r\nErrorMessage: " + oGetItemResponse.ErrorMessage + "\r\n"; // MessageBox.Show(sSomeError, "Some sort of error"); // break; } } oFolder = oReturnFolder; return(bRet); }