0
votes

I have a CAML query which retrieves data from a SharePoint list. It passes for 1 input, but fails when there are 2 or more.

Error:

Microsoft.SharePoint.SPException: One or more field types are not installed properly. Go to the list settings page to delete these fields. ---> System.Runtime.InteropServices.COMException: One or more field types are not installed properly. Go to the list settings page to delete these fields.


After researching, probable cause may be due to mismatch SharePoint column InternalName . However, query managed to execute for 1 input, but not 2 or more.

Base on the query examples below, am I having a wrong format for the FAIL cases?


1. PASS:

<Where>
    <And>
        <Eq>
            <FieldRef Name="Header1Ref"/><Value Type="Text">H1</Value>
        </Eq>
        <Neq>
            <FieldRef Name ="ContentType"/><Value Type="Text">Document</Value>
        </Neq>
    </And>
</Where>

2. FAIL:

<Where>
    <And>
        <Eq>
            <And>
                <Or>
                    <Eq>
                        <FieldRef Name="Header1Ref"/><Value Type="Text">H1</Value>
                    </Eq>
                    <Eq>
                        <FieldRef Name="Header1Ref"/><Value Type="Text">H2</Value>
                    </Eq>
                </Or>
                    <Neq>
                        <FieldRef Name ="ContentType"/><Value Type="Text">Document</Value>
                    </Neq>
            </And>
        </Eq>
    </And>
</Where>

3. FAIL:

<Where>
    <And>
        <Eq>
            <And>
                <Or>
                    <FieldRef Name="Header1Ref"/><Value Type="Text">H1</Value>
                    <FieldRef Name="Header1Ref"/><Value Type="Text">H2</Value>
                </Or>
                <Neq>
                    <FieldRef Name ="ContentType"/><Value Type="Text">Document</Value>
                </Neq>
            </And>
        </Eq>
        <Neq>
            <FieldRef Name ="ContentType"/><Value Type="Text">Document</Value>
        </Neq>
    </And>
</Where>
1

1 Answers

0
votes

It is the CAML Query issue, I modify the code as below for your reference.

string siteUrl = "http://sp2013";
string listTitle = "DL";
string viewName="Test";

using (SPSite site = new SPSite(siteUrl))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists[listTitle];
        SPView view=list.Views[viewName];
        string orQuery = "<Or><Eq><FieldRef Name=\"Header1Ref\"/><Value Type=\"Text\">H1</Value></Eq><Eq><FieldRef Name=\"Header1Ref\"/><Value Type=\"Text\">H2</Value></Eq></Or>";
        string subQuery = String.Format("<Neq><FieldRef Name =\"ContentType\"/><Value Type=\"Text\">{0}</Value></Neq>", "Document");
        string queryText = String.Format("<Where><And>{0}{1}</And></Where>", orQuery, subQuery);
        var query = new SPQuery(view)
        {
            Query = queryText,
            ViewAttributes = "Scope=\"RecursiveAll\"",
            ViewFields = @"<FieldRef Name='RecordTitle'/>",
        };

        SPListItemCollection items = list.GetItems(query);
        foreach (SPListItem item in items)
        {
            Console.WriteLine(item["RecordTitle"]);
        }
        Console.ReadKey();
    }
}

The CAML Query as below.

<Where>
    <And>
        <Or>
            <Eq><FieldRef Name="Header1Ref"/><Value Type="Text">H1</Value></Eq>
            <Eq><FieldRef Name="Header1Ref"/><Value Type="Text">H2</Value></Eq>
        </Or>
        <Neq>
            <FieldRef Name="ContentType"/><Value Type="Text">Document</Value>
        </Neq>
    </And>
</Where>