/// <summary> /// This function inserts three items into the index with generic Key, Name, Keywords, and Comment properties. /// </summary> public async static Task <string> AddItemsToIndex(Windows.Storage.Search.ContentIndexer indexer) { var content = new Windows.Storage.Search.IndexableContent(); const int numberOfItemsToIndex = 3; for (int i = 0; i < numberOfItemsToIndex; i++) { content.Properties[Windows.Storage.SystemProperties.ItemNameDisplay] = "Sample Item Name " + i; content.Properties[Windows.Storage.SystemProperties.Keywords] = "Sample keyword " + i; content.Properties[Windows.Storage.SystemProperties.Comment] = "Sample comment " + i; content.Id = "SampleKey" + i; OnIndexerOperationBegin(); await indexer.AddAsync(content); OnIndexerOperationComplete(indexer); } string[] propertiesToQuery = { Windows.Storage.SystemProperties.ItemNameDisplay, Windows.Storage.SystemProperties.Keywords, Windows.Storage.SystemProperties.Comment }; var query = indexer.CreateQuery("*", propertiesToQuery); uint count = await query.GetCountAsync(); return("Number of items currently in the index: " + count); }
private async void UpdatePropertyHelper(string itemKey, string propertyKey, string propertyName, object newValue) { var indexer = Windows.Storage.Search.ContentIndexer.GetIndexer(); string[] propertiesToRetrieve = { propertyKey }; try { // Log the original value of the property var retrievedProperties = await indexer.RetrievePropertiesAsync(itemKey, propertiesToRetrieve); var outputString = "Original " + propertyName + ": " + Helpers.StringifyProperty(retrievedProperties[propertyKey]); // Create an indexable content item to update the property var content = new Windows.Storage.Search.IndexableContent(); content.Properties.Add(propertyKey, newValue); content.Id = itemKey; // Update the indexer Helpers.OnIndexerOperationBegin(); await indexer.UpdateAsync(content); Helpers.OnIndexerOperationComplete(indexer); // Log the new value of the property retrievedProperties = await indexer.RetrievePropertiesAsync(itemKey, propertiesToRetrieve); outputString += "\nNew " + propertyName + ": " + Helpers.StringifyProperty(retrievedProperties[propertyKey]); rootPage.NotifyUser(outputString, NotifyType.StatusMessage); } catch (Exception err) { rootPage.NotifyUser("Exception thrown! Did you try to retrieve or update a property on a non-existent item?" + "\nMessage: " + err.Message + "\nHRESULT: " + String.Format("{0,8:X}", err.HResult), NotifyType.ErrorMessage); } }
private async void AddToIndex_Click(object sender, RoutedEventArgs e) { if (ItemKeyInput.Text == "") { rootPage.NotifyUser("You must add an item key to insert an item into the index.", NotifyType.ErrorMessage); } else { // Write the content property to a stream var contentStream = new Windows.Storage.Streams.InMemoryRandomAccessStream(); var contentWriter = new Windows.Storage.Streams.DataWriter(contentStream); contentWriter.WriteString(ContentInput.Text); await contentWriter.StoreAsync(); contentStream.Seek(0); // Get the name, keywords, and comment properties, and assign a language to them if provided object itemNameValue = NameInput.Text; object keywordsValue = KeywordsInput.Text; object commentValue = CommentInput.Text; if (LanguageInput.Text != "") { var itemNameValueAndLanguage = new Windows.Storage.Search.ValueAndLanguage(); itemNameValueAndLanguage.Language = LanguageInput.Text; itemNameValueAndLanguage.Value = itemNameValue; itemNameValue = itemNameValueAndLanguage; var keywordsValueAndLanguage = new Windows.Storage.Search.ValueAndLanguage(); keywordsValueAndLanguage.Language = LanguageInput.Text; keywordsValueAndLanguage.Value = keywordsValue; keywordsValue = keywordsValueAndLanguage; var commentValueAndLanguage = new Windows.Storage.Search.ValueAndLanguage(); commentValueAndLanguage.Language = LanguageInput.Text; commentValueAndLanguage.Value = commentValue; commentValue = commentValueAndLanguage; } // Create the item to add to the indexer var content = new Windows.Storage.Search.IndexableContent(); content.Id = ItemKeyInput.Text; content.Properties.Add(Windows.Storage.SystemProperties.ItemNameDisplay, itemNameValue); content.Properties.Add(Windows.Storage.SystemProperties.Keywords, keywordsValue); content.Properties.Add(Windows.Storage.SystemProperties.Comment, commentValue); content.Stream = contentStream; content.StreamContentType = "text/plain"; // Add the item to the indexer Helpers.OnIndexerOperationBegin(); var indexer = Windows.Storage.Search.ContentIndexer.GetIndexer(); await indexer.AddAsync(content); Helpers.OnIndexerOperationComplete(indexer); // Retrieve the item from the indexer and output its properties var retrievedProperties = await indexer.RetrievePropertiesAsync(ItemKeyInput.Text, content.Properties.Keys); rootPage.NotifyUser(Helpers.CreateItemString(ItemKeyInput.Text, content.Properties.Keys, retrievedProperties), NotifyType.StatusMessage); } }
/// <summary> /// This function inserts three items into the index with generic Key, Name, Keywords, and Comment properties. /// </summary> public async static Task<string> AddItemsToIndex(Windows.Storage.Search.ContentIndexer indexer) { var content = new Windows.Storage.Search.IndexableContent(); const int numberOfItemsToIndex = 3; for (int i = 0; i < numberOfItemsToIndex; i++) { content.Properties[Windows.Storage.SystemProperties.ItemNameDisplay] = "Sample Item Name " + i; content.Properties[Windows.Storage.SystemProperties.Keywords] = "Sample keyword " + i; content.Properties[Windows.Storage.SystemProperties.Comment] = "Sample comment " + i; content.Id = "SampleKey" + i; OnIndexerOperationBegin(); await indexer.AddAsync(content); OnIndexerOperationComplete(indexer); } string[] propertiesToQuery = { Windows.Storage.SystemProperties.ItemNameDisplay, Windows.Storage.SystemProperties.Keywords, Windows.Storage.SystemProperties.Comment }; var query = indexer.CreateQuery("*", propertiesToQuery); uint count = await query.GetCountAsync(); return "Number of items currently in the index: " + count; }