Accessing Data from Sharepoint 2010 List in Silverlight Using Client Object Model

In Sharepoint 2010 Microsoft has come up with the APIs which can be used to access the data in client side execution like in ECMA script or Silverlight.

Here we’ll see How to Use Sharepoint Client Object Model in Silverlight ….

Creating Sharepoint Custom List

  • Create A custom list in Sharepoint 2010 Say”CustomerData”.
  • Add Columns to it Say “CustomerName”,”CustomerID””, “CustomerAddress” & “CustomerWebPage”

Creating Silverlight Project

  • Create a Silverlight project….
  • Add reference to the Following two dlls which can be found at following location (C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14TEMPLATELAYOUTSClientBin)

1. Microsoft.Sharepoint.Client.Silverlight
2.Microsoft.Sharepoint.Client.Silverlight.Runtime

  • Create a Class for Customer fetch Data in its object (Say in Separate file Customer.cs So as to create Less mess in One Place)

public class Customer
{
public String CustName{ get; set; }
public String CustID{ get; set; }
public String CustAddress{ get; set; }
public String CustWebPage{ get; set; }

public Customer(String Name, String Id, String Address, String hyperLnk)
{
this.
CustName= Name;
this.
CustID= Id;
this.
CustAddress= Address;
this.
CustWebPage = hyperLnk;
}

}
public class Customers : ObservableCollection
{

}

  • Add the Following Code to the User Control in Silverlight MainPage.xaml

  • Now Create A method to fetch the data from Sharepoint. All the calls we’ll be making to Sharepoint will be Async Calls as Silverlight doesn’t support Sync Calls.


private ClientContext context = null;
private Web web = null;
private List lst;
ListItemCollection lstCol ;
private delegate void UpdateUIMethod();
private Customers lstCust = new Customers();

public MainPage()
{
InitializeComponent();
fetchData();

}

public void fetchData(
{
context = ClientContext.Current;
web = context.Web;
lst = web.Lists.GetByTitle("CustomerData");

CamlQuery cmlQry = new CamlQuery();


cmlQry.ViewXml = "";

lstCol = lst.GetItems(cmlQry);

context.Load(lstCol,
items => items.Include(
item => item.Id,
item => item["CustomerName"],
item => item["CustomerID"],
item => item["CustomerAddress"],
item => item["CustomerWebPage"]));
context.ExecuteQueryAsync(OnSiteLoadSuccess, OnSiteLoadFailure);

}

private void OnSiteLoadSuccess(object sender, ClientRequestSucceededEventArgs e)
{

UpdateUIMethod updateUI = LoadSiteData;
this.Dispatcher.BeginInvoke(updateUI);
}

private void OnSiteLoadFailure(object sender, ClientRequestFailedEventArgs e)
{

}

private void LoadSiteData()
{
Customers cust = new Customers();

string strName, strID, strHyperlink, strAddress



foreach (var val in lstCol)
{
strName= (val["CustomerName"] != null) ? val["CustomerName"].ToString() : string.Empty;
strID= (val["CustomerID"] != null) ? val["CustomerID"].ToString() : string.Empty;
strHyperlink = (val["CustomerWebPage"] != null) ? ((FieldUrlValue)val.FieldValues["CustomerWebPage"]).Url : string.Empty;
strAddress= (val["CustomerAddress"] != null) ? val["CustomerAddress"].ToString() : string.Empty;

lstCust.Add( new Customers() { new Customer(strTitle, strSummary, strCategory, strHyperlink) });
}

lstBox.ItemsSource = lstCust;

}


Above Code can be used to fetch Data from Sharepoint List and Show it in a list box.
Now it will be very easy to modify the above code as required to fetch data form any source in Sharepoint and use it in silverlight.

Disclaimer : Above code was a small part of project which I’ve extracted as a tutorial. In case any issue is there with the code please leave a comment and I’ll get back to you ASAP with the solution.

Leave a Reply

Your email address will not be published. Required fields are marked *