Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have what I thought a simple problem, but can't seem to find any solutions that work or exist in SharePoint 2013.

I have a multiple selection list box in an InfoPath 2010 form. This box defines the category the document belongs to. There are 8 options and users can select as many as they like. When viewing the list users should be able to filter by category (using the standard method of filtering of columns). I can link the column to SharePoint with the merge data type, and this shows all of the values in the column but SharePoint does not allow filtering of this type of column:

Standard Column Can't be filtered

I have a field in my InfoPath form that’s hidden and concats the values in the multiple selection list box into a comma separated string. This is filterable as a column but as the values are saved as comma separated strings multiple selections are treated as a unique entry. I want the filter to look at this field and be able to select one of the 8 categories and display any item that contains this value, not just items that exclusively contain this value.

enter image description here

My concatinated string can be filtered, but multiple selections show as a new value.

For example if I have the following 4 items:

**Document**     **Category** 

Item1             1
Item2             1, 2
Item3             2
Item4             3, 8, 5

And I filter by category 1, I want it to display Item1 and Item2.

If I filter by category 1 and 2, I want to display Item1, Item2 and Item3

share|improve this question

2 Answers 2

I ran into this before and came up with a solution that is not as clean as one would have hoped, but it worked for me, granted that it's a dirty trick/workaround and it's far from being perfect.

I tested it in SharePoint 2010, but from what I can see from your description, nothing has changed in SharePoint 2013 (read: dealing with multiselection box in InfoPath and SharePoint is a real pita).

Ingredients:

  • a QueryString parameter declared on the XsltListViewWebPart
  • a filter on the XsltListViewWebPart
  • a CEWP webpart on the view page
  • an external file (stored for example in SiteAssets) with a bit of javascript and linked to the CEWP webpart
  • jquery
  • underscore.js

Description:

Open the page containing the XsltLVWP in SharePoint Designer and add a QueryString parameter, call it eg. Category. Then, create a filter with this criterion: Category Contains [Category] where [Category] is the parameter you've just declared.

Add a file to the SiteAssets library, call it eg. getCategories.js. The content should look more or less like this:

<script type="text/javascript">
function retrieveListItems() {
var clientContext = new SP.ClientContext();
var list = clientContext.get_web().get_lists().getByTitle('YourList');
var camlQuery = new SP.CamlQuery();

// retrieve only the category field
camlQuery.set_viewXml('<View><ViewFields><FieldRef Name="Category"/></ViewFields></View>');
this.listItems = list.getItems(camlQuery);

clientContext.load(listItems);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed)); 
}

function onQuerySucceeded(sender, args) {
    var listItemEnumerator = listItems.getEnumerator();

    var allCategories = [];    
    var i;

    while (listItemEnumerator.moveNext()) {
        var listItem = listItemEnumerator.get_current();
        var category = listItem.get_item('Category');

        //we need to split the comma separated string
        if (category.indexOf(',') !== -1){
            var tempCategories = category.split(',');
            for (i = 0; i < tempCategories.length; i++){
                allCategories[allCategories.length] = tempCategories[i].trim(); //remove initial white space
            }
        }
        else{
            allCategories[allCategories.length] = category;
        }     
    }   

    // we need unique categories
    allCategories = _.uniq(allCategories); //underscore.js uniq method

    // add all unique categories to the dropdown
    for (i = 0; i < allCategories.length; i++){
        var option = $('<option>');
        option.val(allCategories[i]);
        option.text(allCategories[i]);
        $('#categoriesDdl').append(option);
    }

    $('#categoriesDdl').on('change', function(){
        window.location.href = "/yuorsite/list/page.aspx?" +  "Category=" + $('#categoriesDdl').val();
    });
}

function onQueryFailed(sender, args) { 
//.... 
}


ExecuteOrDelayUntilScriptLoaded(retrieveListItems, 'sp.js')

</script>

<!-- this is our custom filter -->
<select id="categoriesDdl">
   <option>---select a category</option>
</select>

The code creates a dropdown list with all the unique categories extracted from the category field and append the selected value to a querystring parameter, thus triggering the filter on the XsltLVWB.

Add a CEWP to your view page and link the getCategories.js file.

Known issues:

  • having set a parameter and a filter, the first time the page is rendered, nothing is shown
  • if your categories have overlapping names, then the contains filter will produce unwanted results (eg. category 1 and category 10 will both be shown when your filter is = 1)

Other solutions:

considering that you're on SharePoint 2013, I would go the REST way and build my own datagrid with full custom filters (for the category filter dropdown the logic would be more or less the same). Here you can find some inspiration.

share|improve this answer

In the filter settings, use the expression:

contains(eval(field1,',,'), 'my category')
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.