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

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube