Question

I am attempting to write a simple Silverlight webpart that reads information from a list and then outputs it to a data grid. I can get all of the lists, their titles and their field titles fine but when I try to get a specific list's title and field titles I get nothing except an error.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace SP2010
{
public partial class MainPage : UserControl
{

    ClientContext context;
    Web spWeb;
    List customerList;
    ListItemCollection allCustomers;
    public MainPage()
    {
        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        InitializeComponent();
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        context = new ClientContext(ApplicationContext.Current.Url);
        spWeb = context.Web;
        context.Load(spWeb);
        customerList = spWeb.Lists.GetByTitle("testlist");
        context.Load(customerList);
        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml =
            @"<View>
                <Query></Query>
               <RowLimit>1000</RowLimit>
             </View>";
        allCustomers = customerList.GetItems(camlQuery);
        context.Load(allCustomers);
        context.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), new ClientRequestFailedEventHandler(OnRequestFailed));

    }
    private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)
    {
        Dispatcher.BeginInvoke(DisplayListData);
    }
    private void OnRequestFailed(Object sender, ClientRequestFailedEventArgs args)
    {
        MessageBox.Show(args.ErrorDetails + "   " + args.Message);
    }
    private void DisplayListData()
    {
        List<SPCustomers> spCustomers = new List<SPCustomers>();
        foreach (ListItem item in allCustomers)
        {
            spCustomers.Add(new SPCustomers
            {
                IPAddress = customerList.Fields.GetByTitle("IP").ToString(),
                Make = item["make"].ToString(),
                Model = item["model"].ToString(),
                xCord = item["x"].ToString(),
                yCord = item["y"].ToString()
            });

        }
        MyOutput.ItemsSource = spCustomers;
    }

This is the response I get from SharePoint

<?xml version="1.0" encoding="utf-8"?><soap:Envelope 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>
<SendClientScriptErrorReport 
  xmlns="http://schemas.microsoft.com/sharepoint/diagnostics/"><message>
Uncaught Error:Unhandled Error in Silverlight Application The property or field has not
been initialized.It has not been requested or the request has not been executed. It may
need to be explicitly requested.
  at Microsoft.SharePoint.Client.ListItem.GetFieldValue(String fieldName)
  at Microsoft.SharePoint.Client.ListItem.get_Item(String fieldName)
  at SP2010.MainPage.DisplayListData()</message><file></file><line>1</line><stack>&lt;stack&gt;
&lt;/stack&gt;</stack><client>&lt;client&gt;
&lt;browser name=&quot;Netscape&quot; version=&quot;5&quot; /&gt;
&lt;useragent&gt;Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.803.0 Safari/535.1&lt;/useragent&gt;
&lt;language&gt;en-US&lt;/language&gt;
&lt;referrer&gt;http://[server]/SitePages/Home.aspx&lt;/referrer&gt;
&lt;location&gt;http://[server]/SitePages/Home.aspx&lt;/location&gt;
&lt;correlation&gt;add082be-4131-4548-b4fe-aedf71d64809&lt;/correlation&gt;
&lt;/client&gt;</client><team></team><originalFile></originalFile></SendClientScriptErrorReport></soap:Body></soap:Envelope>

I'm sure there is something I missed, any help would be appreciated.

Was it helpful?

Solution

The Exception is complaining about one of Fields you are attempting to query in your list item:

Make = item["make"].ToString(),
Model = item["model"].ToString(),
xCord = item["x"].ToString(),
yCord = item["y"].ToString()

Make 100% sure that the field names are what you think they are. (Open the List Settings and click on the field and check what it's name is in the URL. Use that Name)

Also, do a test by just attempting to get item["Title"]

OTHER TIPS

You could try using jQuery with Silverlight for simplified access to the Client OM created through sharepoint's listdata.svc. Here's some info for that.
http://forums.silverlight.net/forums/p/74514/177281.aspx

You could also try using Silverlight's WebClient class for making HTTP GETs and POSTs to the /_vti_bin/listdata.svc. More info on that here:
http://weblogs.asp.net/albertpascual/archive/2009/03/03/silverlight-101-make-a-get-request-and-a-post-request.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top