Tuesday, June 28, 2011

How to access SharePoint List Items using Silverlight Object Model

In the last postI introduced the Silverlight Object Model, which helps to access the SharePoint objects directly from Silverlight. In this I am posting about how to access SharePoint List Items using Silverlight Object Model. In this I am using Caml query object, which is used to get the Fields.

using Microsoft.SharePoint.Client;


private List list;
private ListItemCollection listitem;

public MainPage()
{
InitializeComponent();

ClientContext spContext = new ClientContext("http://pct174:10972");
if (spContext != null)
{
List list = spContext.Web.Lists.GetByTitle("Custom List");
listitem = list.GetItems(CamlQuery.CreateAllItemsQuery());
spContext.Load(listitem);
spContext.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), new ClientRequestFailedEventHandler(OnRequestFailed));
}
}

private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)
{
Dispatcher.BeginInvoke(FillList);
}
private void OnRequestFailed(Object sender, ClientRequestFailedEventArgs args)
{
MessageBox.Show(args.Message);
}

private void FillList()
{
var lst = new List();
foreach (var i in listitem)
{
lst.Add(new listdata
{
Title = i["Title"].ToString(),
EmployeeName = i["Employee_x0020_Name"].ToString(),
Gender = i["Gender"].ToString(),
DOB = ((DateTime)i["DOB"])
} );
}
And if you don’t want all the columns or you want to specify the columns you want to access you can use Caml Query object’s ViewXml property.?


CamlQuery camlQuery = new CamlQuery();
string query = "";
camlQuery.ViewXml = query;
this.oListItems = this.oList.GetItems(camlQuery);

You can access Custom Lists and Document Libraries by this method.

No comments: