In VS2010 I have a SharePoint 2010 project with an Application Page. In this simple page I have header information about an order and a SharePoint:ListView for the product detail. So, I just want to filter that ListView by OrderId:
SPWeb 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();
This code show a list with all the items in the "OrderDetail" list.
I tried to do with an SPQuery, but I don't know how to associate the SPListItemCollection (the result of the query) to the SharePoint开发者_JAVA百科: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);
How can I filter a SharePoint:ListView with the result of a SPQuery? I am using incorrect components?
Thanks in advance...
Nice try there!
You can set the query string through the list view's query
parameter. So given your code above you would need the following:
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();
An example you can find here: Using SharePoint's SPView Class and CAML as a Query Language and MSDN here: SPView.Query
.
This code running for me.
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();
精彩评论