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

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube