Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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"]

share|improve this answer
    
make, model, x, and y were all correct but the IP field while named IP was actually Title but it still does not display the values –  BeardFist Jul 7 '11 at 15:54
    
That was indeed the problem because I am getting the data correctly just the data grid isn't showing it as expected –  BeardFist Jul 7 '11 at 21:36
    
Hi, I suggest you start a new question so we can help you with the silverlight now working :-) –  Fox Jul 8 '11 at 13:26

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

share|improve this answer
    
Inaccuracy in this answer - The Client OM runs within the Silverlight application and doesn't require jQuery to access ListData.svc. –  James Love Jul 8 '11 at 17:59
    
I never said that. What I was saying is that jQuery has much more simplified access to lists through listdata.svc than does Silverlight. I was merely suggesting that rather than using the Client OM through silverlight he use listdata.svc, which implements the Client OM. –  Vap0r Jul 8 '11 at 19:08
    
Ah, understood - using the REST APIs (which are a darn sight faster than using the managed OM). –  James Love Jul 8 '11 at 19:56
    
Fully agree with James. Using Jquery with Silverlight was the way to go for Sharepoint 2007. Whereas this was a suitable solution, it requires the developer to build his/her own object model. Sharepoint 2010 has a built in Client Object model which is safer and faster. –  Fox Jul 11 '11 at 11:55
    
Note Vap0r.. Not trying to disprove your answer as it's also very valid. Just pointing to the fact that the recommended way of Client side dev in 2010 is using the Client OM. MS realised that there was a big need for Ajax loading and thus they built the Client OM. –  Fox Jul 11 '11 at 11:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.