Monday, 25 October 2010

Using SPGridView and Linq to display filterable lists

Problem: Display a SharePoint list and make it filterable.

Initial Hypothesis: Create new page add the new Xslt List View Web Part (XLVWP) to the page. Customise the Xslt using SharePoint Designer (SPD). The web part can now be exported if required. Add filter web parts to filter the data for end users.

Resolution: This gives a comprehensive solution for internal users but clients needing a specific design or advanced querying will need a different solution. You can customise the xslt for the list and paging and sorting are built in. However in my instance I needed drop down lists that could query against multi select choice columns.

Problem Redefined: Using the SPGridView control display user's data based on selection criteria.

Hypothesis:

Create a list that contains a choice column, the column should allow multiple choices.

Ensure your generate a Linq to SharePoint proxy

Create a Visual Web Part project that uses a visual web part to display the list results and the results are filtered by a drop down lists that works against the SharePoint list.

Resolution:
Filter the SPDataGrip using LINQ to SharePoint as shown below:

Step 1: Create the "Customers" list.
Stet 2 : Add the new column based on a multiple choice field type.

Shown above is my custom list "Customer", it has two columns namely: "Title" of type string and "Industry" of type choice (multi)

Step 3: Add a few random records.

Step 4: Create a new Visual Studio project, I chose a "Visual Web Part Project".

Step 5: Add the generate SPMetal code to your new project. I used the SPMetal template provided by CKSDev but as long as you have added the SPMetal for the new list it will work.

Step 6: Open the user control ascx and add a SPGridfiew and a drop down list.

Step 7: In the code behind add the Linq query to retrieve the Customers list.