public async Task SearchMetadata_LiveSession()
        {
            var filter = new
            {
                attr1 = "blah",
                attr2 = new { gt = 5, lt = 5 },
                attr3 = new { gt = new DateTime(2016, 10, 1), lt = new DateTime(2016, 11, 5) },
                attr4 = "value1"
            };

            var mdFilter = new BoxMetadataFilterRequest()
            {
                TemplateKey = "testtemplate",
                Scope       = "enterprise",
                Filters     = filter
            };

            var results = await _client.SearchManager.SearchAsync(mdFilters : new List <BoxMetadataFilterRequest>()
            {
                mdFilter
            });

            Assert.AreEqual(1, results.Entries.Count, "Incorrect number of search results using metadata search");
        }
Exemple #2
0
        protected async override Task <int> Execute()
        {
            if (string.IsNullOrEmpty(this._query.Value) && (string.IsNullOrEmpty(this._mdFilterScope.Value()) && string.IsNullOrEmpty(this._mdFilterTemplateKey.Value())))
            {
                _app.ShowHelp();
                return(await base.Execute());
            }
            var      fileExtensions    = new List <string>();
            var      ancestorFolderIds = new List <string>();
            var      ownerUserIds      = new List <string>();
            var      contentTypes      = new List <string>();
            var      mdFilters         = new List <BoxMetadataFilterRequest>();
            DateTime createdAtFrom     = DateTime.Now.AddMonths(-6);
            DateTime createdAtTo       = DateTime.Now;
            DateTime updatedAtFrom     = DateTime.Now.AddMonths(-6);
            DateTime updatedAtTo       = DateTime.Now;

            if (this._ownerUserIds.HasValue())
            {
                ownerUserIds = this._ownerUserIds.Value().Split(',').ToList();
            }
            if (this._fileExtenstions.HasValue())
            {
                fileExtensions = this._fileExtenstions.Value().Split(',').ToList();
            }
            if (this._contentTypes.HasValue())
            {
                contentTypes = this._contentTypes.Value().Split(',').ToList();
            }
            if (this._ancestorFolderIds.HasValue())
            {
                ancestorFolderIds = this._ancestorFolderIds.Value().Split(',').ToList();
            }
            if (this._createdAtFrom.HasValue() && this._createdAtTo.HasValue())
            {
                try
                {
                    createdAtFrom = GeneralUtilities.GetDateTimeFromString(this._createdAtFrom.Value(), true);
                }
                catch
                {
                    createdAtFrom = DateTime.Parse(this._createdAtFrom.Value());
                }
                try
                {
                    createdAtTo = GeneralUtilities.GetDateTimeFromString(this._createdAtTo.Value(), true);
                }
                catch
                {
                    createdAtTo = DateTime.Parse(this._createdAtTo.Value());
                }
            }
            if (this._updatedAtTo.HasValue() && this._updatedAtFrom.HasValue())
            {
                try
                {
                    updatedAtFrom = GeneralUtilities.GetDateTimeFromString(this._updatedAtFrom.Value(), true);
                }
                catch
                {
                    updatedAtFrom = DateTime.Parse(this._updatedAtFrom.Value());
                }
                try
                {
                    updatedAtTo = GeneralUtilities.GetDateTimeFromString(this._updatedAtTo.Value(), true);
                }
                catch
                {
                    updatedAtTo = DateTime.Parse(this._updatedAtTo.Value());
                }
            }
            if (this._mdFilterTemplateKey.HasValue() && this._mdFilterScope.HasValue() && this._mdFilterJson.HasValue())
            {
                var mdTempKeyCount = this._mdFilterTemplateKey.Values.Count();
                var mdScopeCount   = this._mdFilterScope.Values.Count();
                var mdJsonCount    = this._mdFilterJson.Values.Count();
                if (mdTempKeyCount != mdScopeCount && mdTempKeyCount != mdJsonCount)
                {
                    throw new Exception("Mismatch on metadata filtering. Can't perform search.");
                }
                for (var i = 0; i < mdTempKeyCount; i++)
                {
                    var mdRequest = new BoxMetadataFilterRequest();
                    mdRequest.TemplateKey = this._mdFilterTemplateKey.Values[i];
                    mdRequest.Scope       = this._mdFilterScope.Values[i];
                    mdRequest.Filters     = this._mdFilterJson.Values[i];
                    mdFilters.Add(mdRequest);
                }
            }
            var boxClient = base.ConfigureBoxClient(oneCallAsUserId: this._asUser.Value(), oneCallWithToken: base._oneUseToken.Value());
            BoxCollection <BoxItem> result;

            if (mdFilters.Count() > 0)
            {
                result = await boxClient.SearchManager.SearchAsync(this._query.Value, scope : this._scope.Value(),
                                                                   fileExtensions : fileExtensions, ancestorFolderIds : ancestorFolderIds,
                                                                   ownerUserIds : ownerUserIds, type : this._type.Value(), contentTypes : contentTypes,
                                                                   createdAtRangeFromDate : createdAtFrom, createdAtRangeToDate : createdAtTo,
                                                                   updatedAtRangeFromDate : updatedAtFrom, updatedAtRangeToDate : updatedAtTo, mdFilters : mdFilters);
            }
            else
            {
                result = await boxClient.SearchManager.SearchAsync(this._query.Value, scope : this._scope.Value(),
                                                                   fileExtensions : fileExtensions, ancestorFolderIds : ancestorFolderIds,
                                                                   ownerUserIds : ownerUserIds, type : this._type.Value(), contentTypes : contentTypes,
                                                                   createdAtRangeFromDate : createdAtFrom, createdAtRangeToDate : createdAtTo,
                                                                   updatedAtRangeFromDate : updatedAtFrom, updatedAtRangeToDate : updatedAtTo);
            }

            foreach (var item in result.Entries)
            {
                base.PrintItem(item);
            }
            return(await base.Execute());
        }