Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for search param to get shared link items #721

Merged
merged 4 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Box.V2.Test/BoxSearchManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,31 @@ public async Task QueryWithOpenDateRanges_ValidResponse_ValidResults()
/*** Assert ***/
Assert.AreEqual("query=test&created_at_range=1988-11-18T09%3A30%3A00Z%2C&updated_at_range=%2C2018-11-18T09%3A30%3A00Z&limit=30&offset=0", boxRequest.GetQueryString());
}

[TestMethod]
[TestCategory("CI-UNIT-TEST")]
public async Task QueryWithSharedLinks_ValidResponse_ValidResults()
{
/*** Arrange ***/
string responseString = "{\"entries\":[{\"accessible_via_shared_link\":\"https://www.box.com/s/vspke7y05sb214wjokpk\",\"item\":{\"type\":\"file\",\"id\":\"11111\",\"file_version\":{\"type\":\"file_version\",\"id\":\"111110\",\"sha1\":\"97cc02de7c356f94e3beeb1e0c63f78a6edb01fd\"},\"sequence_id\":\"9\",\"etag\":\"9\",\"sha1\":\"97cc02de7c356f94e3beeb1e0c63f78a6edb01fd\",\"name\":\"test file.txt\",\"description\":\"\",\"size\":16,\"path_collection\":{\"total_count\":1,\"entries\":[{\"type\":\"folder\",\"id\":\"0\",\"sequence_id\":null,\"etag\":null,\"name\":\"All Files\"}]},\"created_at\":\"2016-12-07T15:53:59-08:00\",\"modified_at\":\"2018-04-24T15:08:58-07:00\",\"trashed_at\":null,\"purged_at\":null,\"content_created_at\":\"2016-12-07T15:53:59-08:00\",\"content_modified_at\":\"2016-12-07T15:59:32-08:00\",\"created_by\":{\"type\":\"user\",\"id\":\"33333\",\"name\":\"Test User\",\"login\":\"[email protected]\"},\"modified_by\":{\"type\":\"user\",\"id\":\"33333\",\"name\":\"Test User\",\"login\":\"[email protected]\"},\"owned_by\":{\"type\":\"user\",\"id\":\"33333\",\"name\":\"Test User\",\"login\":\"[email protected]\"},\"shared_link\":null,\"parent\":{\"type\":\"folder\",\"id\":\"0\",\"sequence_id\":null,\"etag\":null,\"name\":\"All Files\"},\"item_status\":\"active\"},\"type\":\"search_result\"}],\"limit\":30,\"offset\":0,\"total_count\":1}";
IBoxRequest boxRequest = null;

Handler.Setup(h => h.ExecuteAsync<BoxCollection<BoxSearchResult>>(It.IsAny<IBoxRequest>()))
.Returns(Task.FromResult<IBoxResponse<BoxCollection<BoxSearchResult>>>(new BoxResponse<BoxCollection<BoxSearchResult>>()
{
Status = ResponseStatus.Success,
ContentString = responseString
}))
.Callback<IBoxRequest>(r => boxRequest = r);

/*** Act ***/
BoxCollection<BoxSearchResult> results = await _searchManager.QueryAsyncWithSharedLinks("test");

/*** Assert ***/
Assert.AreEqual("query=test&limit=30&offset=0&include_recent_shared_links=true", boxRequest.GetQueryString());
Assert.AreEqual(1, results.TotalCount);
Assert.AreEqual("file", results.Entries[0].Item.Type);
Assert.AreEqual("11111", results.Entries[0].Item.Id);
}
}
}
3 changes: 2 additions & 1 deletion Box.V2/Box.V2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<Compile Include="Models\BoxRepresentationInfo.cs" />
<Compile Include="Models\BoxRepresentationProperties.cs" />
<Compile Include="Models\BoxRepresentationStatus.cs" />
<Compile Include="Models\BoxSearchResult.cs" />
<Compile Include="Models\BoxSessionEndpoint.cs" />
<Compile Include="Models\BoxFileUploadSession.cs" />
<Compile Include="Models\BoxExpiringEmbedLink.cs" />
Expand Down Expand Up @@ -270,4 +271,4 @@
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
</Project>
82 changes: 82 additions & 0 deletions Box.V2/Managers/BoxSearchManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,88 @@ public async Task<BoxCollection<BoxItem>> QueryAsync(string query,
return response.ResponseObject;
}

/// <summary>
/// Search for items that are accessible by a single user or an entire enterprise.
/// </summary>
/// <param name="query">The string to search for. Box matches the search string against object names, descriptions, text contents of files, and other data.</param>
/// <param name="scope">The scope for which you want to limit your search to. Can be user_content for a search limited to only the current user or enterprise_content for the entire enterprise. To enable the enterprise_content scope for an administrator, please contact Box.</param>
/// <param name="fileExtensions">Limit searches to specific file extension(s).</param>
/// <param name="createdAfter">The from date for when the item was created</param>
/// <param name="createdBefore">The to date for when the item was created</param>
/// <param name="updatedAfter">The from date for when the item was last updated</param>
/// <param name="updatedBefore">The to date for when the item was last updated</param>
/// <param name="sizeLowerBound">The lower bound of the file size range in bytes</param>
/// <param name="sizeUpperBound">The upper bound of the file size range in bytes</param>
/// <param name="ownerUserIds">Search by item owners</param>
/// <param name="ancestorFolderIds">Limit searches to specific parent folders</param>
/// <param name="contentTypes">Limit searches to specific Box designated content types. Can be name, description, file_content, comments, or tags.</param>
/// <param name="type">The type you want to return in your search. Can be file, folder, or web_link</param>
/// <param name="trashContent">Allows you to search within the trash. Can be trashed_only or non_trashed_only. Searches without this parameter default to non_trashed_only</param>
/// <param name="mdFilters">Filters for a specific metadata template for files with metadata object associations. NOTE: For searches with the mdfilters param, a query string is not required. Currenly only one BoxMetadataFilterRequest element is allowed.</param>
/// <param name="limit">Number of search results to return. The default is 30 and the maximum is 200.</param>
/// <param name="offset">The search result at which to start the response. The default is 0.</param>
/// <param name="fields">Attribute(s) to include in the response.</param>
/// <param name="sort">The field to sort the search results by, e.g. "modified_at".</param>
/// <param name="direction">The direction to return the results. "ASC" for ascending and "DESC" for descending.</param>
/// <returns>A collection of search results is returned. If there are no matching search results, the collection will be empty.</returns>
public async Task<BoxCollection<BoxSearchResult>> QueryAsyncWithSharedLinks(string query,
string scope = null,
IEnumerable<string> fileExtensions = null,
DateTime? createdAfter = null,
DateTime? createdBefore = null,
DateTime? updatedAfter = null,
DateTime? updatedBefore = null,
long? sizeLowerBound = null,
long? sizeUpperBound = null,
IEnumerable<string> ownerUserIds = null,
IEnumerable<string> ancestorFolderIds = null,
IEnumerable<string> contentTypes = null,
string type = null,
string trashContent = null,
List<BoxMetadataFilterRequest> mdFilters = null,
int limit = 30,
int offset = 0,
IEnumerable<string> fields = null,
string sort = null,
BoxSortDirection? direction = null)

{

string mdFiltersString = null;
if (mdFilters != null)
{
mdFiltersString = _converter.Serialize(mdFilters);
}

var createdAtRangeString = BuildDateRangeField(createdAfter, createdBefore);
var updatedAtRangeString = BuildDateRangeField(updatedAfter, updatedBefore);
var sizeRangeString = BuildSizeRangeField(sizeLowerBound, sizeUpperBound);

BoxRequest request = new BoxRequest(_config.SearchEndpointUri)
.Param("query", query)
.Param("scope", scope)
.Param("file_extensions", fileExtensions)
.Param("created_at_range", createdAtRangeString)
.Param("updated_at_range", updatedAtRangeString)
.Param("size_range", sizeRangeString)
.Param("owner_user_ids", ownerUserIds)
.Param("ancestor_folder_ids", ancestorFolderIds)
.Param("content_types", contentTypes)
.Param("type", type)
.Param("trash_content", trashContent)
.Param("mdfilters", mdFiltersString)
.Param("limit", limit.ToString())
.Param("offset", offset.ToString())
.Param("sort", sort)
.Param("direction", direction.ToString())
.Param(ParamFields, fields)
.Param("include_recent_shared_links", "true");

IBoxResponse<BoxCollection<BoxSearchResult>> response = await ToResponseAsync<BoxCollection<BoxSearchResult>>(request).ConfigureAwait(false);

return response.ResponseObject;
}

private string BuildDateRangeField(DateTime? from, DateTime? to)
{
var fromString = from.HasValue ? from.Value.ToUniversalTime().ToString(Constants.RFC3339DateFormat_UTC) : String.Empty;
Expand Down
27 changes: 27 additions & 0 deletions Box.V2/Models/BoxSearchResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using Newtonsoft.Json;

namespace Box.V2.Models
{
public class BoxSearchResult
{
/// <summary>
/// The type of the item
/// </summary>
[JsonProperty(PropertyName = "type")]
public string Type { get; private set; }

/// <summary>
/// The optional shared link through which the user has access to this item.
/// This value is only returned for items for which the user has recently accessed the file through a shared link. For all other items this value will return null.
/// </summary>
[JsonProperty(PropertyName = "accessible_via_shared_link")]
public Uri AccessibleViaSharedLink { get; private set; }

/// <summary>
/// The Box item
/// </summary>
[JsonProperty(PropertyName = "item")]
public BoxItem Item { get; private set; }
}
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
**New Features and Enhancements:**

- Add filter fields to get file version retentions ([#717](https://github.com/box/box-windows-sdk-v2/pull/717))
- Add support for search param to get shared link items ([#721](https://github.com/box/box-windows-sdk-v2/pull/721))

## 3.25.0 [2020-10-19]

Expand Down
36 changes: 36 additions & 0 deletions docs/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Search


- [Search for Content](#search-for-content)
- [Search for Content with Shared Link Items](#search-for-content-with-shared-link-items)
- [Metadata Search](#metadata-search)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->
Expand Down Expand Up @@ -48,6 +49,41 @@ BoxCollection<BoxItem> results = await client.SearchManager
.QueryAsync("Meeting Notes", fileExtensions: new { "pdf", "docx" });
```

## Search for Content with Shared Link Items

To get a list of items matching a search query, including items that a user might have accessed recently through a shared link, call the
```
SearchManager.QueryAsyncWithSharedLinks(string query,
string scope = null,
IEnumerable<string> fileExtensions = null,
DateTime? createdAfter = null,
DateTime? createdBefore = null,
DateTime? updatedAfter = null,
DateTime? updatedBefore = null,
long? sizeLowerBound = null,
long? sizeUpperBound = null,
IEnumerable<string> ownerUserIds = null,
IEnumerable<string> ancestorFolderIds = null,
IEnumerable<string> contentTypes = null,
string type = null,
string trashContent = null,
List<BoxMetadataFilterRequest> mdFilters = null,
int limit = 30,
int offset = 0,
IEnumerable<string> fields = null,
string sort = null,
BoxSortDirection? direction = null)
```
method. There are many possible options for advanced search filtering, which are
documented in the [Search API Reference](https://developer.box.com/en/guides/search/).
For most types of searches, a query string is required.

```c#
// Search for PDF or Word documents matching "Meeting Notes"
BoxCollection<BoxSearchResult> results = await client.SearchManager
.QueryAsyncWithSharedLinks("Meeting Notes", fileExtensions: new { "pdf", "docx" });
```

### Metadata Search

When searching on metadata values, a search query string is not required and
Expand Down