How to read from a list using Silverlight and the SharePoint 2010 Client Object Model?
-
16-10-2019 - |
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><stack>
</stack></stack><client><client>
<browser name="Netscape" version="5" />
<useragent>Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.803.0 Safari/535.1</useragent>
<language>en-US</language>
<referrer>http://[server]/SitePages/Home.aspx</referrer>
<location>http://[server]/SitePages/Home.aspx</location>
<correlation>add082be-4131-4548-b4fe-aedf71d64809</correlation>
</client></client><team></team><originalFile></originalFile></SendClientScriptErrorReport></soap:Body></soap:Envelope>
I'm sure there is something I missed, any help would be appreciated.
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