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'/>" & _

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 principal in PH&V Information Services, specializing in Web development with expertise in SOA, client-side development, and user interface design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His most recent book ("rtfm*") is on writing effective user manuals, and his blog on language and technical writing can be found at

comments powered by Disqus

Reader Comments:

Thu, Sep 12, 2013 Gloriana lowest car insurance viagra online buy brand viagra online viagra on line DOT DOT DOT DOT

Wed, Sep 11, 2013 Starleigh viagra auto insurance cheap car insurance on line cheap viagra generic DOT http://w DOT DOT DOT

Thu, Jan 31, 2013 Delly insurance auto auctions nwpna order cialis :-P car insurance quotes gqgyzl DOT nwpna DOT :-P DOT gqgyzl

Thu, Jan 31, 2013 Klondike nexium 8-DDD accutane 273860 cheapest cialis 42546 DOT 8-DDD DOT 273860 DOT 42546

Tue, Jan 29, 2013 Vlora cheap auto insurance quotes us lykisv insurance life 88877 low car insurance 99991 DOT lykisv DOT 88877 DOT 99991

Thu, Jan 24, 2013 Earthwind nj car insurance %-] viagra bgoy buy propecia in usa tqfbgc DOT %-] DOT bgoy DOT tqfbgc

Thu, Jan 17, 2013 Ivalene car insurance 8-[[ life insurance =-OO auto insurance rates fnn DOT 8-[[ DOT =-OO DOT fnn

Wed, Jan 16, 2013 Carlye auto insurance online nuvdx viagra :P propecia uqm DOT nuvdx DOT :P DOT uqm

Add Your Comments Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Please type the letters/numbers you see above

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.