0
votes

I created a custom property in the user profile. I want to search through all the user profiles and output those profiles in which the custom property contains a certain string

For example:

User1- Custom Property value is 1,2,3 User2- Custom Property value in 2,4,5 User3- Custom Property value is 4,6,8

I want to output all the profiles in which Custom Property contains 2 (using c# code)

So the output should have User1 and User2

Can someone suggest the best way to implement this? I did find some links in the internet for searching user profiles use KeyWord search but and not sure if those methods could be used to search through Custom Properties. Example: https://www.collaboris.com/how-to-use-search-keywordquery-to-query-user-profiles-in-sharepoint/

I am using SharePoint 2013

1

1 Answers

1
votes

We ended up promoting the Custom Property that we added to the User profile to a Managed property. Also it seems like we can do wildcard searches on managed properties so we do People searches like "CustomProperty:*,2,*" so that it would return all the user profiles which have the number 2 in the custom property of their user profile

Interestingly the wildcard works only on the end for OOTB properties like FirstName so we cant do things like FirstName:oh and expect it would return John Doe's profile But we can certainly do this - FirstName:joh* and that would return all the people whose first name starts with Joh (which would include John Doe)

But it seems like the wildcard works both at the beginning and the end for the custom managed properties (which helped a great deal for us)

On how to return the results of the search using c# we used this-

    private DataTable GetPeople(SPSite spSite, string queryText)
    {

        var keywordQuery = new KeywordQuery(spSite)
         {
             QueryText = queryText,
             KeywordInclusion = KeywordInclusion.AllKeywords,
             SourceId = System.Guid.Parse("b09a7990-05ea-4af9-81ef-edfab16c4e31")
         };

        keywordQuery.RowLimit = this.MaxProfilesToDisplay;

        keywordQuery.SelectProperties.Add("AccountName");
        keywordQuery.SelectProperties.Add("PictureURL");

        SearchExecutor e = new SearchExecutor();
        ResultTableCollection rt = e.ExecuteQuery(keywordQuery);
        var tab = rt.Filter("TableType", KnownTableTypes.RelevantResults);
        var result = tab.FirstOrDefault();
        DataTable DT = result.Table;
        return DT;
    }

and we would invoke this like

DataTable dt = GetPeople(SPContext.Current.Site, "CustomProperty:*,2,*" );