Wednesday, September 17, 2008

Using ListViewByQuery control in your custom code

Code:

<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" 
Inherits="SPListViewPage, ListView, Version=1.0.0.0, Culture=neutral, PublicKeyToken=566105b43a7965be" %>

<%@ Register Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.WebControls" TagPrefix="sharepoint" %>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<sharepoint:ListViewByQuery ID="SPListViewByQuery" runat="server" />
</asp:Content>

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

public partial class SPListViewPage : LayoutsPageBase
{
protected ListViewByQuery SPListViewByQuery;

protected void Page_Load(object sender, EventArgs e)
{
SPList list = this.Web.Lists["Contacts"];
SPView view = list.Views["All contacts"];
SPQuery query = new SPQuery(view);
query.ViewFields = "<FieldRef Name='LinkTitle'/>" +
"<FieldRef Name='FirstName'/>" +
"<FieldRef Name='Company'/>" +
"<FieldRef Name='WorkCity'/>";
query.Query = "<Where>" +
"<Eq>" +
"<FieldRef Name='Company' />" +
"<Value Type='Text'>Sandeep Training Company</Value>" +
"</Eq>" +
"</Where>" +
"<OrderBy>" +
"<FieldRef Name='LinkTitle' />" +
"</OrderBy>";
SPListViewByQuery.List = list;
SPListViewByQuery.Query = query;
}
}

2 comments:

Moni December 30, 2010 at 6:31 PM  

Hi, I have been trying to use this the ListViewByQuery control on a subsite to display a list from the parent site. It seems to support standard list views fine, however I cannot get the DataSheet view to display on the child site. Is the DataSheet View support for this.

Thanks,Moni

Sandeep K Nahta December 30, 2010 at 7:40 PM  

Dataview can be used at child site using workarounds... google "sharepoint dataview child site"