[SalesForce] How to search Knowledge Articles bypassing Guest user permissions in Napili Community

Background:

  1. We have a public page on Napili community where guest user can type the search term into the custom search component and see the results.
  2. If we use the OOB Napili search functionality, it is not showing all the articles matching the search term but only what guest user can see.
  3. So we built a custom search component as mentioned in this link hoping that we can bypass user permissions.

But still we are getting results based on user permissions.

Here is the complete code for the component:

CommunityCustomSearch.cmp:

<aura:component implements="forceCommunity:searchInterface" 
                access="global" 
                controller="ComunityCustomSearchApexController">

    <div class="search">
        <div class="search-wrapper">
            <form class="search-form">
                <div class="search-input-wrapper" style="display:inline-block">
                    <input  style="width:100%" 
                            aura:id="search-input" 
                            type="text" 
                            placeholder="My Search"/>
                </div>
                <button type="button" 
                        onclick="{!c.searchBtnHandler}">
                    <span   class=" label bBody truncate" 
                            dir="ltr">Search</span>
                </button>
            </form>
        </div>
    </div>
</aura:component>

CommunityCustomSearchController.js:

({
    searchBtnHandler : function(component, event, helper) {

        var searchText = component.find('search-input').getElement().value;
        console.log(searchText);

        var action = component.get("c.searchArticles");
        action.setParams({  searchTerm : searchText});

        action.setCallback(this, function(response) {
            var state = response.getState();
            if(state === "SUCCESS"){
                console.log('Server call SUCCESS');
                console.log('Results returned from server ');
                console.log(response.getReturnValue().length);
                console.log(response.getReturnValue()[0].articleTitle);
            }else{
                console.log('Server call for getting Search Results failed');
            }
        });

        $A.enqueueAction(action);
    }
})

ComunityCustomSearchApexController.cls:

public without sharing class ComunityCustomSearchApexController {

    @AuraEnabled
    public static List<SearchResultsWrapper> searchArticles(String searchTerm){

        String sosl = 'FIND \'*' + searchTerm + '*\' IN ALL FIELDs';
        sosl = sosl + ' Returning Knowledge__kav(ID, Title,UrlName ';
        sosl = sosl + ' WHERE PublishStatus = \'Online\'';
        sosl = sosl + ' AND Language = \'en_US\')  WITH SNIPPET (target_length=120)';
        Search.SearchResults searchResults = search.find(sosl);

        List<Search.SearchResult> articleList = searchResults.get('Knowledge__kav');

        List<SearchResultsWrapper> wrapperList = new List<SearchResultsWrapper>();

        for (Search.SearchResult searchResult : articleList) {
            Knowledge__kav article = (Knowledge__kav) searchResult.getSObject();
            SearchResultsWrapper obj = new SearchResultsWrapper();
            obj.articleTitle = article.Title;
            wrapperList.add(obj);
        }

        System.debug(JSON.serialize(wrapperList));
        return wrapperList;
    }

    public class SearchResultsWrapper{
        @AuraEnabled
        public String articleTitle;
    }

}

Data:

  1. I have two articles in the system.
  2. One article is accessible by Guest users(IsVisibleInPkb = TRUE)
  3. Another article is accessible only for Community users(IsVisibleInCsp = TRUE).

Results:

  1. If I access this component as guest user and type the search term, I am seeing only one result in the console.
  2. When I access the same component wrapped in a aura:application as an internal user and for the same search term, I am seeing two results in the console.

Any clues?

Best Answer

This is quite obvious that user won't get access to Article, until unless it is published for user's channel.

For public community users, you will have to publish the second article for PKB (Public Knowledge Base) channel.

Regarding the link, you can customise the search component to control your search behaviour for other standard and custom objects, but Article sharing and permissions are designed in more controlled manner and are managed by Salesforce.

For example, you can control following operations for articles in your customised search component:

  • Display only specific type of article types
  • Display only 2 records out of available 5 records, based on certain conditions

However article sharing with user will be controlled by Channels and Data category access.

A possible workaround to search Articles with Guest user profile:

  • Implement a custom object to store Article Title, Summary, URL and ID and keep it in sync with Knowledge Articles, even they are not published for Public Knowledge base
  • Grant access to Guest user to this custom object. So even an article is not accessible, he would be able query the title
  • If the user selects the article, navigate to login page or article URL
Related Topic