在VS2010中,我有一个带有应用程序页面的SharePoint 2010项目。在这个简单的页面中,我有关于订单的标题信息和SharePoint:ListView的产品详细信息。我要通过OrderId:
过滤ListViewSPWeb myWeb = SPControl.GetContextWeb(Context);
SPList lstOrderDetail = myWeb.Lists["OrderDetail"];
SPView vwOrderDetail = lstProductosPedidos.Views["ShortedForCustomer"];
lvOrderDetail.ListId = lstOrderDetail.ID.ToString("B").ToUpperInvariant();
lvOrderDetail.ViewId = vwOrderDetail.ID.ToString("B").ToUpperInvariant();
lvOrderDetail.DataBind();
这段代码显示了一个包含"OrderDetail"列表中所有项目的列表。
我试图做一个SPQuery,但我不知道如何将SPListItemCollection(查询的结果)关联到SharePoint:ListView。
SPQuery qryOrderDetail = new SPQuery(vwOrderDetail);
qryOrderDetail.Query = string.Format(@"
<Where>
<Eq>
<FieldRef Name='OrderId' LookupID='True'/>
<Value Type='Number'>{0}</Value>
</Eq>
</Where>", iOrderID);
SPListItemCollection lstOrderDetailFiltered = lstProductosPedidos.GetItems(qryOrderDetail);
我如何过滤一个SharePoint:ListView与SPQuery的结果?我使用了不正确的组件?
做得好!
可以通过列表视图的query
参数设置查询字符串。因此,对于上面的代码,您将需要以下内容:
SPView vwOrderDetail = lstProductosPedidos.Views["ShortedForCustomer"];
vwOrderDetail.Query = string.Format(@"
<Where>
<Eq>
<FieldRef Name='OrderId' LookupID='True'/>
<Value Type='Number'>{0}</Value>
</Eq>
</Where>", iOrderID);
vwOrderDetail.Update();
你可以在这里找到一个例子:使用SharePoint的SPView类和CAML作为查询语言和MSDN在这里:SPView.Query
.
这段代码为我运行。
SPSite oSite = new SPSite([Site URL]);// [Site URL] change it to your sharepoint site URL
SPWeb oWeb = oSite.OpenWeb();
SPList oList = oWeb.Lists["shared documents"];
SPViewCollection oViewCollection = oList.Views;
string strViewName = "MyCustomView";
System.Collections.Specialized.StringCollection viewFields =
new System.Collections.Specialized.StringCollection();
viewFields.Add("Name");
viewFields.Add("Type");
string query = "<Where><Eq><FieldRef Name="Name"/>" +
"<Value Type="Text">mysample</Value></Eq></Where>";// here you can filter your items using the selected
item in the dropdownlist
oViewCollection.Add(strViewName, viewFields, query, 100, true, false);
oWeb.Update();