Friday, June 13, 2014

Sorting Grid Columns in LightSwitch apps Hosted on SharePoint 2013

Get grids to load pre-sorted and still allow users to re-sort via the column headers:
  1. Remove any sorts from the Query Designers but leave any desired filters:

    LS QueriesQueryDesigner
  2. For each Query, click the ‘Write Code’ drop down and find the PreprocessQuery method and it will add the declaration to the C# SPPurchaseReqDataService class
  3. Set the query value with LINQ:
     partial void QueryNewPR_PreprocessQuery(ref IQueryable<PurchaseRequisition> query)  
         {  
           query = query.OrderByDescending(p => p.Modified);  
         }  
     partial void QuerySubmittedPR_PreprocessQuery(ref IQueryable<PurchaseRequisition> query)  
         {  
           query = query.OrderByDescending(p => p.Modified);  
         }  

That's it. It's pretty straightforward once you do it but LightSwitch can be a bit confusing, at least for me. In theory, this code is supposed to do exactly what the Query Designer does. But, doing it in code (on the server-side) allows the users to re-sort whereas the designer lets the user believe they can re-sort but the grid is just reloaded with the original sort. I am able to repro this 100% of the time. I’m guessing that I am not the only one. If this is a known bug and/or if it is/gets fixed, please let me know. In any case, I hope this is still useful information on sorting in LightSwitch.

No comments:

Post a Comment