我在Sharepoint中有一个自定义搜索webpart,它有7个过滤器。我使用CAML Query从Sharepoint列表中获取数据。我想编写一个通用的SPQuery,它将根据搜索参数过滤出数据。搜索参数是可选的。如果用户输入任何2个参数,那么我需要获得与指定的2个参数相对应的数据。如何使用CAML Query实现此目的?我无法想到基于搜索参数生成我的caml查询的通用方法..
答案 0 :(得分:1)
以下是我在Silverlight中使用的一些代码,用于根据搜索参数生成CAML查询。 也许它很有帮助。
private ClientContext context;
private Microsoft.SharePoint.Client.List SampleSPList;
private Microsoft.SharePoint.Client.ListItemCollection SampleSPCollection;
//Load the List
void LoadList()
{
SampleSPList = context.Web.Lists.GetByTitle("Sample");
context.Load(SampleSPList);
CamlQuery query = new Microsoft.SharePoint.Client.CamlQuery();
string camlQueryXml = "";
bool existsWhere = false;
if (searchString.Text != "")
{
camlQueryXml = camlQueryXml + "<Contains><FieldRef Name='sampleString' /><Value Type='Text'>" + searchString.Text + "</Value></Contains>";
if (existsWhere == true) {
camlQueryXml = "<And>" + camlQueryXml + "</And>";
}
existsWhere = true;
}
if (searchTextArea.Text != "")
{
camlQueryXml = camlQueryXml + "<Contains><FieldRef Name='sampleTextArea' /><Value Type='Text'>" + searchTextArea.Text + "</Value></Contains>";
if (existsWhere == true)
{
camlQueryXml = "<And>" + camlQueryXml + "</And>";
}
existsWhere = true;
}
if (existsWhere == true) { camlQueryXml = "<View><Query><OrderBy><FieldRef Name='ID' /></OrderBy><Where>" + camlQueryXml + "</Where></Query></View>"; }
else { camlQueryXml = "<View><Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query></View>"; }
query.ViewXml = camlQueryXml;
SampleSPCollection = SampleSPList.GetItems(query);
context.Load(SampleSPCollection);
context.ExecuteQueryAsync(ListLoaded, ListLoadFailed);
}
答案 1 :(得分:0)
实际上通过编写逻辑来动态生成spquery来解决问题