public static void SearchBySeek(Folder folder) { var rnd = new Random(); var view = new ItemView(1, 0); view.OrderBy.Add(MyNamedProp, SortDirection.Ascending); view.PropertySet = new PropertySet(BasePropertySet.IdOnly, MyNamedProp); var sw = new Stopwatch(); var filter = new SearchFilter.Exists(MyNamedProp); sw.Start(); for (var x = 0; x < NumberOfSearchesToPerform; x++) { var numberToSearchFor = rnd.Next(0, NumberOfMessagesToCreate); int high = folder.FindItems(filter, view).TotalCount - 1; var low = 0; var found = false; while (!found) { view.Offset = ((high - low) / 2) + low; var resultItem = folder.FindItems(filter, view); if (resultItem.Items.Count < 1) { break; } long myId; resultItem.Items[0].TryGetProperty(MyNamedProp, out myId); if (myId == numberToSearchFor) { found = true; } else { if (myId > numberToSearchFor) { high = (int)myId - 1; } else { low = (int)myId + 1; } } } if (!found) { Console.WriteLine("Warning! No item found!"); } } sw.Stop(); Console.WriteLine("SearchBySeek finished after: " + sw.ElapsedMilliseconds + " milliseconds."); }
/// <summary> /// Access a named property by identifier in a default named property set. Ths sample shows how to get /// the location property. /// </summary> /// <param name="service">An ExchangeService object with credentials and the EWS URL.</param> private static void AccessNamedMAPIPropertyById(ExchangeService service) { // Define the meeting location property. ExtendedPropertyDefinition PidLidLocation = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Appointment, 0x00008208, MapiPropertyType.String); // Retrieve a collection of 10 items. ItemView view = new ItemView(10); // Create a search filter that filters email based on the existence of the extended property. SearchFilter.Exists filterPidLidLocation = new SearchFilter.Exists(PidLidLocation); // Create a property set that includes the extended property definition. view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, PidLidLocation); FindItemsResults <Item> findResults; // Search the default calendar folder with the defined view and search filter. This results in a call to EWS by // using the FindItem operation. try { findResults = service.FindItems(WellKnownFolderName.Calendar, filterPidLidLocation, view); // Search the item collection returned in the results for the extended property. foreach (Item item in findResults.Items) { Console.WriteLine("Item subject: {0}", item.Subject); // Determine whether the item has the custom extended property set. if (item.ExtendedProperties.Count > 0) { // Display the extended name and value of the extended property. foreach (ExtendedProperty extendedProperty in item.ExtendedProperties) { Console.WriteLine("\tExtended Property Value: " + extendedProperty.Value); } } else { Console.WriteLine("Did not find the PidLidLocation property."); } } } catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message); } }
/// <summary> /// Access a property by its property set identified by its GUID and property identifier. /// This sample shows how to get the task due date property. /// </summary> /// <param name="service">An ExchangeService object with credentials and the EWS URL.</param> private static void AccessMAPIPropertyInPropertySetByGuidAndName(ExchangeService service) { // Define the normalized subject property. ExtendedPropertyDefinition PidLidTaskDueDate = new ExtendedPropertyDefinition(new Guid("00062003-0000-0000-C000-000000000046"), 0x00008105, MapiPropertyType.SystemTime); // Retrieve a collection of 10 items. ItemView view = new ItemView(10); // Create a search filter that filters email based on the existence of the extended property. SearchFilter.Exists filterPidLidTaskDueDate = new SearchFilter.Exists(PidLidTaskDueDate); // Create a property set that includes the extended property definition. view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, PidLidTaskDueDate); FindItemsResults <Item> findResults; try { // Search the default Tasks folder with the defined view and search filter. This results in a FindItem operation call to EWS. findResults = service.FindItems(WellKnownFolderName.Tasks, filterPidLidTaskDueDate, view); // Search the item collection returned in the results for the extended property. foreach (Item item in findResults.Items) { Console.WriteLine("Item subject: {0}", item.Subject); // Determine whether the item has the custom extended property set. if (item.ExtendedProperties.Count > 0) { // Display the extended name and value of the extended property. foreach (ExtendedProperty extendedProperty in item.ExtendedProperties) { Console.WriteLine("\Extended Property Value: " + extendedProperty.Value); } } else { Console.WriteLine("\Did not find the PidLidTaskDueDate property."); } } } catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message); } }
/// <summary> /// This method gets the contacts that have the special DisplayName and property /// definitions in the special folder. /// </summary> private static List <Contact> GetContacts(Folder contactsFolder, String name, params PropertyDefinition[] schemas) { if (contactsFolder == null) { return(null); } List <Contact> contacts = new List <Contact>(); SearchFilter.SearchFilterCollection filters = new SearchFilter.SearchFilterCollection(LogicalOperator.And); if (!String.IsNullOrWhiteSpace(name)) { SearchFilter searchFilter = new SearchFilter.ContainsSubstring(ContactSchema.DisplayName, name); filters.Add(searchFilter); } if (schemas != null) { foreach (PropertyDefinition schema in schemas) { SearchFilter searchFilter = new SearchFilter.Exists(schema); filters.Add(searchFilter); } } const Int32 pageSize = 10; ItemView itemView = new ItemView(pageSize); PropertySet propertySet = new PropertySet(BasePropertySet.IdOnly, schemas); propertySet.Add(ContactSchema.DisplayName); itemView.PropertySet = propertySet; FindItemsResults <Item> findResults = null; do { findResults = contactsFolder.FindItems(filters, itemView); itemView.Offset += pageSize; contacts.AddRange(findResults.Cast <Contact>()); } while (findResults.MoreAvailable); return(contacts); }
/// <summary> /// Create a search filter for filtering items based on whether a property exists for an item. /// </summary> /// <param name="service">An ExchangeService object with credentials and the EWS URL.</param> private static void UseNotExistsSearchFilter(ExchangeService service) { // Create a definition for an extended property that represents a custom X-Header. ExtendedPropertyDefinition xExperimentalHeader = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.InternetHeaders, "X-Experimental", MapiPropertyType.String); // The Exists filter determines whether the a property exists on an item. // This filter indicates whether the custom X-Experimental property exists // on an item. If the Exists condition is used, then items with the // X-Experimental property are returned in the search results. The Exists // filter will return results even if the value of the property is null // or empty. SearchFilter.Exists exists = new SearchFilter.Exists(xExperimentalHeader); // The Not filter negates the result of another filter. // This filter indicates that the results of the Exists filter are negated. SearchFilter.Not not = new SearchFilter.Not(exists); // Create a nonpaged view that returns items with the Subject and X-Experimental // properties. ItemView view = new ItemView(10); view.PropertySet = new PropertySet(EmailMessageSchema.Subject, xExperimentalHeader); try { // Find items where the X-Experimental property is present on the item. This results // in a FindItem operation call to EWS. FindItemsResults <Item> results = service.FindItems(WellKnownFolderName.MsgFolderRoot, exists, view); // Process the results for finding items that contain the X-Experimental property. Console.WriteLine("Sent a request to find items where the X-Experimental property exists on the item."); UseNotExistsHelper(results, xExperimentalHeader); // Find items where the X-Experimental property is not present on the item. This results // in a FindItem operation call to EWS. FindItemsResults <Item> results2 = service.FindItems(WellKnownFolderName.MsgFolderRoot, not, view); // Process the results for finding items that do not contain the X-Experimental property. Console.WriteLine("Sent a request to find items where the X-Experimental property does not exist on the item."); UseNotExistsHelper(results2, xExperimentalHeader); } catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message); } }
/// <summary> /// Find and view a custom extended property on an email message. /// </summary> /// <param name="service">An ExchangeService object with credentials and the EWS URL.</param> private static void ViewCustomExtendedProperties(ExchangeService service) { // Define a view of 10 items in the Drafts folder. ItemView view = new ItemView(10); // Get the GUID for the property set. Guid MyPropertySetId = new Guid("{C11FF724-AA03-4555-9952-8FA248A11C3E}"); // Create a definition for the extended property. ExtendedPropertyDefinition extendedPropertyDefinition = new ExtendedPropertyDefinition(MyPropertySetId, "Expiration Date", MapiPropertyType.String); // Create a search filter that filters email based on the existence of the extended property. SearchFilter.Exists customPropertyExistsFilter = new SearchFilter.Exists(extendedPropertyDefinition); // Create a property set that includes the extended property definition. view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, extendedPropertyDefinition); // Search the Drafts folder by using the defined view and search filter. This results in a FindItem operation call to EWS. FindItemsResults <Item> findResults = service.FindItems(WellKnownFolderName.Drafts, customPropertyExistsFilter, view); // Search the email collection returned in the results for the extended property. foreach (Item item in findResults.Items) { Console.WriteLine(item.Subject); // Determine whether the item has the custom extended property set. if (item.ExtendedProperties.Count > 0) { // Display the extended name and value of the extended property. foreach (ExtendedProperty extendedProperty in item.ExtendedProperties) { Console.WriteLine(" Extended Property Name: " + extendedProperty.PropertyDefinition.Name); Console.WriteLine(" Extended Property Value: " + extendedProperty.Value); } } else { Console.WriteLine(" This email message does not have the 'Expiration Date' extended property set on it"); } } }
/// <summary> /// Access a named property by name in a default named property set. Ths sample shows how to get /// the junk email move stamp property. /// </summary> /// <param name="service">An ExchangeService object with credentials and the EWS URL.</param> private static void AccessNamedMAPIPropertyByName(ExchangeService service) { // Define the http://schemas.microsoft.com/exchange/junkemailmovestamp secure messaging property. ExtendedPropertyDefinition PidNameExchangeJunkEmailMoveStamp = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "http://schemas.microsoft.com/exchange/junkemailmovestamp", MapiPropertyType.Integer); // Retrieve a collection of 10 items. ItemView view = new ItemView(10); // Create a search filter that filters email based on the existence of the extended property. SearchFilter.Exists exchangeJunkEmailMoveStampFilter = new SearchFilter.Exists(PidNameExchangeJunkEmailMoveStamp); // Create a property set that includes the extended property definition. view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, PidNameExchangeJunkEmailMoveStamp); // Search the Junk Email folder with the defined view and search filter. This results in a FindItem operation call to EWS. FindItemsResults <Item> findResults = service.FindItems(WellKnownFolderName.JunkEmail, exchangeJunkEmailMoveStampFilter, view); // Search the item collection returned in the results for the extended property. foreach (Item item in findResults.Items) { Console.WriteLine(item.Subject); // Determine whether the item has the custom extended property set. if (item.ExtendedProperties.Count > 0) { // Display the extended name and value of the extended property. foreach (ExtendedProperty extendedProperty in item.ExtendedProperties) { Console.WriteLine("\tExtended Property Name: " + extendedProperty.PropertyDefinition.Name); Console.WriteLine("\tExtended Property Value: " + extendedProperty.Value); } } else { Console.WriteLine("\tThis"); } } }