0

I'm loading a SharePoint list using the client service object model (SharePoint 2013 on-premise). I'm using the following code:

private void buttonRefreshOrders_Click(object sender, RoutedEventArgs e)
{
    using (var ctx = new ClientContext("http://sharepoint/ci/resources"))
    {
        var list = ctx.Web.Lists.GetByTitle("Resource Order Form Content");

        var query = new CamlQuery
                        {
                            ViewXml =
                                @"<Where><Or><Eq><FieldRef Name=""Status""></FieldRef><Value Type=""Text"">Approved</Value></Eq><IsNotNull><FieldRef Name=""Status"" /></FieldRef></IsNotNull></Or></Where>"
                        };

        var collListItem = list.GetItems(query);

        ctx.Load(collListItem);
        ctx.ExecuteQuery();

        if (collListItem.Count == 0)
        {
            MessageBox.Show(
                "No orders are currently within the queue.",
                "Information Center",
                MessageBoxButton.OK,
                MessageBoxImage.Information);
        }
        else
        {
            MessageBox.Show("Success!");

            foreach (var item in collListItem)
            {
                MessageBox.Show(item["Status"].ToString());
            }
        }
    }

}

I receive the exception:

An unhandled exception of type 'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException' occurred in Microsoft.SharePoint.Client.dll

Typically I believe this happens when the name being referenced in the following line doesn't match what's in SharePoint's backend:

MessageBox.Show(item["Status"].ToString());

However I don't understand how that's the case here as I've used this exact same naming within my CamlQuery. I've also used SP Caml Query Helper 2013 and the field names appear to be correct.

How can I find the correct field names that I should be referencing?

Michael A
  • 9,480
  • 22
  • 70
  • 114

1 Answers1

1

Goto the list on your sharepoint site, goto list settings, then click on the column your looking to get the correct name of, it will then be in the address bar.

EG: YourSite/_layouts/FldEdit.aspx?List={Guid}&Field=Cost%5Fx0020%5FCentre%5Fx0020%5FCode

in this case the name of the field you want is Cost_x0020_Centre_x0020_Code (%5F is an "_" %20 is a space, etc you may need to find out a list of these.)

This isn't a complete list but it has most http://www.werockyourweb.com/url-escape-characters/

Nikerym
  • 515
  • 3
  • 12
  • Quick reply, thank-you! That's nailed it. Looks like I should use delegates to include them in my camlQuery execution as well. – Michael A Aug 18 '15 at 01:45
  • Just re-reading your question. You may also need to explicitly declare in your ViewXml the and before and then the closes at the end. – Nikerym Aug 18 '15 at 01:48
  • @Nikerym i am using sp2013 on premise but there is no encoding character in internal name even though there is space in display name. Is this an expected behavior? – Niladri Feb 22 '18 at 14:35
  • @Naladri sorry for the slow reply. I'm not sure what your issue is, Internal name shouldn't contain encoded characters, only the URL should (as it can't handle some characters like spaces) if you have a space in your display name, then your internal name should contain "display_x0020_name" because the internal name also cannot contain a space. – Nikerym Apr 10 '18 at 08:43