Get SharePoint ListItems onto the Page Easily With ListViewByQuery

ListViewByQuery is useful for retrieving ListItems to display to your users.

I use CAML (Collaborative Application Markup Language) queries all the time to retrieve selected ListItems from Lists in SharePoint. But once I retrieve the ListItems, I only want to process the ListItems about half the time. The other half of the time, I want to show the selected items to the user. This is where the ListViewByQuery is useful: just add it to your page (or WebPart/Visual WebPart), insert a query into it, and your retrieved ListItems are displayed to your users.

In a Visual WebPart, the easiest way to use ListViewByQuery is to add this tag to the UserControl's .ascx file:

<SharePoint:ListViewByQuery  runat="server" ID="ViewInaList" />

Then, in the UserControl's code file, you must configure your ListViewByQuery. The first thing you need to do is set the ListViewByQuery's List property to the List from which you're going to extract the ListItems. This code sets the ListViewByQuery to work with a List called Employees, in the same Site as the WebPart being used:

Me.viewinalist.List = SPContext.Current.Web.Lists("Employees")

The next step is to configure an SPQuery to retrieve the data. This code creates an SPQuery that will retrieve those ListItems where the Department field is set to HR (and also sort the items by the Pay field), using whatever List the ListViewByQuery has been set to:

Dim qry As New SPQuery(Me.ViewInaList.List.DefaultView)
qry.Query = "<Where><Gt>" & _
            "<FieldRef Name='Department'/>" & _
            "<Value Type='Text'>HR</Value>" & _
            "</Gt></Where>" & _
            "<OrderBy><FieldRef Name='Pay' Ascending='True'/>" & _
     "</OrderBy>"

If you don't want to display all the fields from the original List, you can set the SPQuery's ViewFields property to a set of FieldRefs to specify the fields you do want to display. This example causes the ListViewByQuery to display only the Fields called EmpName and Job.

The SPQuery will still retrieve all the Fields associated with the List (including a bunch of SharePoint Fields that aren't normally displayed to the user), but only EmpName and Job will be displayed in the ListViewByQuery. Setting ViewFieldsOnly to True, as I do here, causes the SPQuery to only retrieve the fields normally displayed to the user. That's still more fields than EmpName and Job, but considerably fewer fields than the SPQuery will get if you don't set this property:

qry.ViewFields = "<FieldRef Name='EmpName'/><FieldRef Name='Job'/>"
qry.ViewFieldsOnly = True

The final step is to stuff the SPQuery into the ListViewByQuery's Query property:          

Me.viewinalist.Query = qry

And you're done! The ListViewByQuery will retrieve the selected rows and display them.

The ListViewByQuery isn't a perfect control: it doesn't really fire any events (if you don't count Load, Init and so on), and if there's a way to determine which items the user has selected on the server, I haven't found it (you can do it on the client). But if you just want to get your ListItems on the page, this is the fastest way.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Mastering Blazor Authentication and Authorization

    At the Visual Studio Live! @ Microsoft HQ developer conference set for August, Rockford Lhotka will explain the ins and outs of authentication across Blazor Server, WebAssembly, and .NET MAUI Hybrid apps, and show how to use identity and claims to customize application behavior through fine-grained authorization.

  • Linear Support Vector Regression from Scratch Using C# with Evolutionary Training

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the linear support vector regression (linear SVR) technique, where the goal is to predict a single numeric value. A linear SVR model uses an unusual error/loss function and cannot be trained using standard simple techniques, and so evolutionary optimization training is used.

  • Low-Code Report Says AI Will Enhance, Not Replace DIY Dev Tools

    Along with replacing software developers and possibly killing humanity, advanced AI is seen by many as a death knell for the do-it-yourself, low-code/no-code tooling industry, but a new report belies that notion.

  • Vibe Coding with Latest Visual Studio Preview

    Microsoft's latest Visual Studio preview facilitates "vibe coding," where developers mainly use GitHub Copilot AI to do all the programming in accordance with spoken or typed instructions.

  • Steve Sanderson Previews AI App Dev: Small Models, Agents and a Blazor Voice Assistant

    Blazor creator Steve Sanderson presented a keynote at the recent NDC London 2025 conference where he previewed the future of .NET application development with smaller AI models and autonomous agents, along with showcasing a new Blazor voice assistant project demonstrating cutting-edge functionality.

Subscribe on YouTube