如何使用 caml 查询来获取 sharepoint 中的可选搜索值

发布于 2024-09-05 04:28:00 字数 210 浏览 2 评论 0原文

我在 Sharepoint 中有一个自定义搜索 Web 部件,其中有 7 个过滤器。我正在使用 CAML 查询从 Sharepoint 列表中获取数据。我想编写一个通用的 SPQuery,它将根据搜索参数过滤掉数据。搜索参数是可选的。如果用户输入任意 2 个参数,那么我需要获取与指定的这 2 个参数相对应的数据。如何使用 CAML 查询来实现此目的?我无法想到基于搜索参数生成 caml 查询的通用方法。

I have a custom search webpart in Sharepoint which have 7 filters. I am getting data from a Sharepoint list using CAML Query. I want to write a generalized SPQuery which will filter out the data based on the search parameters. The search parameters are optional . If user enters any 2 parameters then I need to get data corresponding to those 2 parameters specified. How do I use CAML Query to achieve this? I am unable to think of a generalized approach for generating my caml query based on search parameters..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与之呼应 2024-09-12 04:28:00

下面是我在 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);
    }

Here is a bit of code I used in Silverlight to generate a CAML query based on search parameters.
Maybe it is helpful.

    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);
    }
梦醒时光 2024-09-12 04:28:00

实际上通过编写逻辑来动态生成spquery解决了这个问题

actually solved the issue by writing logic to dynamically generate the spquery

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文