Beispiel #1
0
        private KeywordQuery CreateKeywordQuery()
        {
            var keywordQuery = new KeywordQuery(ClientContext);

            // Construct query to execute
            var query = "";

            if (!string.IsNullOrEmpty(Query))
            {
                query = Query;
            }

            keywordQuery.QueryText  = query;
            keywordQuery.ClientType = ClientType;
            if (MyInvocation.BoundParameters.ContainsKey("TrimDuplicates"))
            {
                keywordQuery.TrimDuplicates = TrimDuplicates;
            }
            if (MyInvocation.BoundParameters.ContainsKey("Refiners"))
            {
                keywordQuery.Refiners = Refiners;
            }
            if (MyInvocation.BoundParameters.ContainsKey("Culture"))
            {
                keywordQuery.Culture = Culture;
            }
            if (MyInvocation.BoundParameters.ContainsKey("QueryTemplate"))
            {
                keywordQuery.QueryTemplate = QueryTemplate;
            }
            if (MyInvocation.BoundParameters.ContainsKey("RankingModelId"))
            {
                keywordQuery.RankingModelId = RankingModelId;
            }
            if (MyInvocation.BoundParameters.ContainsKey("HiddenConstraints"))
            {
                keywordQuery.HiddenConstraints = HiddenConstraints;
            }
            if (MyInvocation.BoundParameters.ContainsKey("TimeZoneId"))
            {
                keywordQuery.TimeZoneId = TimeZoneId;
            }
            if (MyInvocation.BoundParameters.ContainsKey("EnablePhonetic"))
            {
                keywordQuery.EnablePhonetic = EnablePhonetic;
            }
            if (MyInvocation.BoundParameters.ContainsKey("EnableStemming"))
            {
                keywordQuery.EnableStemming = EnableStemming;
            }
            if (MyInvocation.BoundParameters.ContainsKey("EnableQueryRules"))
            {
                keywordQuery.EnableQueryRules = EnableQueryRules;
            }
            if (MyInvocation.BoundParameters.ContainsKey("SourceId"))
            {
                keywordQuery.SourceId = SourceId;
            }
            if (MyInvocation.BoundParameters.ContainsKey("ProcessBestBets"))
            {
                keywordQuery.ProcessBestBets = ProcessBestBets;
            }
            if (MyInvocation.BoundParameters.ContainsKey("ProcessPersonalFavorites"))
            {
                keywordQuery.ProcessPersonalFavorites = ProcessPersonalFavorites;
            }

            if (SortList != null)
            {
                var sortList = keywordQuery.SortList;
                sortList.Clear();
                foreach (string key in SortList.Keys)
                {
                    SortDirection sort = (SortDirection)Enum.Parse(typeof(SortDirection), SortList[key] as string, true);
                    sortList.Add(key, sort);
                }
            }
            if (SelectProperties != null)
            {
                var selectProperties = keywordQuery.SelectProperties;
                selectProperties.Clear();
                foreach (string property in SelectProperties)
                {
                    selectProperties.Add(property);
                }
            }
            if (RefinementFilters != null)
            {
                var refinementFilters = keywordQuery.RefinementFilters;
                refinementFilters.Clear();
                foreach (string property in RefinementFilters)
                {
                    refinementFilters.Add(property);
                }
            }
            if (Properties != null)
            {
                foreach (string key in Properties.Keys)
                {
                    QueryPropertyValue propVal = new QueryPropertyValue();
                    var value = Properties[key];
                    if (value is string)
                    {
                        propVal.StrVal = (string)value;
                        propVal.QueryPropertyValueTypeIndex = 1;
                    }
                    else if (value is int)
                    {
                        propVal.IntVal = (int)value;
                        propVal.QueryPropertyValueTypeIndex = 2;
                    }
                    else if (value is bool)
                    {
                        propVal.BoolVal = (bool)value;
                        propVal.QueryPropertyValueTypeIndex = 3;
                    }
                    else if (value is string[])
                    {
                        propVal.StrArray = (string[])value;
                        propVal.QueryPropertyValueTypeIndex = 4;
                    }
                    keywordQuery.Properties.SetQueryPropertyValue(key, propVal);
                }
            }
            return(keywordQuery);
        }
Beispiel #2
0
        private KeywordQuery CreateKeywordQuery()
        {
            var keywordQuery = new KeywordQuery(ClientContext);

            // Construct query to execute
            var query = "";

            if (!string.IsNullOrEmpty(Query))
            {
                query = Query;
            }

            keywordQuery.QueryText  = query;
            keywordQuery.ClientType = ClientType;
            if (ParameterSpecified(nameof(TrimDuplicates)))
            {
                keywordQuery.TrimDuplicates = TrimDuplicates;
            }
            if (ParameterSpecified(nameof(Refiners)))
            {
                keywordQuery.Refiners = Refiners;
            }
            if (ParameterSpecified(nameof(Culture)))
            {
                keywordQuery.Culture = Culture;
            }
            if (ParameterSpecified(nameof(QueryTemplate)))
            {
                keywordQuery.QueryTemplate = QueryTemplate;
            }
            if (ParameterSpecified(nameof(RankingModelId)))
            {
                keywordQuery.RankingModelId = RankingModelId;
            }
            if (ParameterSpecified(nameof(HiddenConstraints)))
            {
                keywordQuery.HiddenConstraints = HiddenConstraints;
            }
            if (ParameterSpecified(nameof(TimeZoneId)))
            {
                keywordQuery.TimeZoneId = TimeZoneId;
            }
            if (ParameterSpecified(nameof(EnablePhonetic)))
            {
                keywordQuery.EnablePhonetic = EnablePhonetic;
            }
            if (ParameterSpecified(nameof(EnableStemming)))
            {
                keywordQuery.EnableStemming = EnableStemming;
            }
            if (ParameterSpecified(nameof(EnableQueryRules)))
            {
                keywordQuery.EnableQueryRules = EnableQueryRules;
            }
            if (ParameterSpecified(nameof(SourceId)))
            {
                keywordQuery.SourceId = SourceId;
            }
            if (ParameterSpecified(nameof(ProcessBestBets)))
            {
                keywordQuery.ProcessBestBets = ProcessBestBets;
            }
            if (ParameterSpecified(nameof(ProcessPersonalFavorites)))
            {
                keywordQuery.ProcessPersonalFavorites = ProcessPersonalFavorites;
            }
            if (ParameterSpecified(nameof(CollapseSpecification)))
            {
                keywordQuery.CollapseSpecification = CollapseSpecification;
            }

            if (SortList != null)
            {
                var sortList = keywordQuery.SortList;
                sortList.Clear();
                foreach (string key in SortList.Keys)
                {
                    SortDirection sort = (SortDirection)Enum.Parse(typeof(SortDirection), SortList[key] as string, true);
                    sortList.Add(key, sort);
                }
            }
            if (SelectProperties != null)
            {
                keywordQuery.SelectProperties.Clear();
                var selectProperties = keywordQuery.SelectProperties;
                if (SelectProperties.Length == 1 && SelectProperties[0].Contains(","))
                {
                    SelectProperties = SelectProperties[0].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                }

                foreach (string property in SelectProperties)
                {
                    selectProperties.Add(property.Trim());
                }
            }
            if (RefinementFilters != null)
            {
                var refinementFilters = keywordQuery.RefinementFilters;
                refinementFilters.Clear();
                foreach (string property in RefinementFilters)
                {
                    refinementFilters.Add(property);
                }
            }
            if (Properties != null)
            {
                foreach (string key in Properties.Keys)
                {
                    QueryPropertyValue propVal = new QueryPropertyValue();
                    var value = Properties[key];
                    if (value is string)
                    {
                        propVal.StrVal = (string)value;
                        propVal.QueryPropertyValueTypeIndex = 1;
                    }
                    else if (value is int)
                    {
                        propVal.IntVal = (int)value;
                        propVal.QueryPropertyValueTypeIndex = 2;
                    }
                    else if (value is bool)
                    {
                        propVal.BoolVal = (bool)value;
                        propVal.QueryPropertyValueTypeIndex = 3;
                    }
                    else if (value is string[])
                    {
                        propVal.StrArray = (string[])value;
                        propVal.QueryPropertyValueTypeIndex = 4;
                    }
                    keywordQuery.Properties.SetQueryPropertyValue(key, propVal);
                }
            }
            return(keywordQuery);
        }
        private static IEnumerable<TrendingDocument> GetTrendingDocuments(ClientContext clientContext, string[] actorIds)
        {
            // Build Office Graph Query
            string graphQueryText = null;

            if (actorIds.Length > 1) {
                StringBuilder graphQueryBuilder = new StringBuilder();

                foreach (string actorId in actorIds) {
                    if (graphQueryBuilder.Length > 0) {
                        graphQueryBuilder.Append(",");
                    }

                    graphQueryBuilder.AppendFormat("actor({0},action:1020)", actorId);
                }

                graphQueryBuilder.Append(",and(actor(me,action:1021),actor(me,or(action:1021,action:1036,action:1037,action:1039)))");

                graphQueryText = String.Format("or({0})", graphQueryBuilder.ToString());
            }
            else {
                graphQueryText = String.Format("or(actor({0},action:1020),and(actor(me,action:1021),actor(me,or(action:1021,action:1036,action:1037,action:1039))))", actorIds[0]);
            }

            // Ensure that the Web URL is available
            Web web = clientContext.Web;
            if (!web.IsPropertyAvailable("Url")) {
                clientContext.Load(web, w => w.Url);
                clientContext.ExecuteQuery();
            }

            // Configure Search Query
            KeywordQuery searchQuery = new KeywordQuery(clientContext);
            searchQuery.QueryText = String.Format("Path:{0}", web.Url);

            QueryPropertyValue graphQuery = new QueryPropertyValue();
            graphQuery.StrVal = graphQueryText;
            graphQuery.QueryPropertyValueTypeIndex = 1;
            searchQuery.Properties.SetQueryPropertyValue("GraphQuery", graphQuery);

            QueryPropertyValue graphRankingModel = new QueryPropertyValue();
            graphRankingModel.StrVal = @"{""features"":[{""function"":""EdgeWeight""}],""featureCombination"":""sum"",""actorCombination"":""sum""}";
            graphRankingModel.QueryPropertyValueTypeIndex = 1;
            searchQuery.Properties.SetQueryPropertyValue("GraphRankingModel", graphRankingModel);

            searchQuery.SelectProperties.AddRange(new string[] { "Author", "AuthorOwsUser", "DocId", "DocumentPreviewMetadata", "Edges", "EditorOwsUser", "FileExtension", "FileType", "HitHighlightedProperties", "HitHighlightedSummary", "LastModifiedTime", "LikeCountLifetime", "ListID", "ListItemID", "OriginalPath", "Path", "Rank", "SPWebUrl", "SecondaryFileExtension", "ServerRedirectedURL", "SiteTitle", "Title", "ViewCountLifetime", "siteID", "uniqueID", "webID" });
            searchQuery.BypassResultTypes = true;
            searchQuery.RowLimit = 5;
            searchQuery.RankingModelId = "0c77ded8-c3ef-466d-929d-905670ea1d72";
            searchQuery.ClientType = "DocumentsTrendingInThisSite";

            SearchExecutor searchExecutor = new SearchExecutor(clientContext);
            ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(searchQuery);
            clientContext.ExecuteQuery();

            List<TrendingDocument> trendingDocuments = new List<TrendingDocument>(results.Value[0].ResultRows.Count());

            foreach (var row in results.Value[0].ResultRows) {
                string[] lastModifiedByInfo = row["EditorOwsUser"].ToString().Split('|');

                trendingDocuments.Add(new TrendingDocument(
                    row["Title"].ToString(),
                    row["ServerRedirectedURL"].ToString(),
                    GetPreviewImageUrl(row, web.Url),
                    (DateTime)row["LastModifiedTime"],
                    lastModifiedByInfo[1].Trim(),
                    GetUserPhotoUrl(lastModifiedByInfo[0].Trim(), web.Url)));
            }

            return trendingDocuments;
        }