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

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

Subscribe on YouTube

Upcoming Training Events