Tuesday 12 June 2012

How to get values From Multiple Values LookUp Field in Silverlight Client Object Model


In the below example  i  have  fetched  values from a multi values Look Up Field  and  populated a  treeview.

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;

namespace multilookupvalue
{
public partial class MainPage : UserControl
{
ClientContext cc;
string name;
int recordno;
public MainPage()
{

InitializeComponent();
cc = new ClientContext(ApplicationContext.Current.Url);
cc.Load(cc.Web);
listobj = cc.Web.Lists.GetByTitle("your list name");
List s = cc.Web.SiteUserInfoList;
cc.Load(listobj);
CamlQuery cml = new Microsoft.SharePoint.Client.CamlQuery();
view = "<view/>";
cml.ViewXml = view;
_lsititemcoll = listobj.GetItems(cml);
cc.Load(_listitemcoll);
cc.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), null);

}
private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)
{

// This is not called on the UI thread.
Dispatcher.BeginInvoke(datacon);

}
private void datacon()
{
recordno = 1;
treeview tv1 = new treview();
foreach(ListItem li in _listitemcoll)
{

FieldLookupValue[]  fvalue  =   li["lookup column  name"]  as FieldLookupValue[];
TreeViewItem item = new TreeViewItem();
item.Header = "Record" + recordno;
for(int i=0;i<fvalue.Count();i++)
{

TreeViewItem subitem = new TreeViewItem();
subitem.Header =fvalue[i].LookUpValue;
item.Items.Add(subitem);

}
tv1.Children.Add(item);
LayoutRoot.Children.Add(tv1);
}

}
}

No comments:

Post a Comment