1) Iterating through list items
namespace SilverlightSPCOM
{
public partial class MainPage : UserControl
{
private ClientContext context = null;
private Web web = null;
ListItemCollection listItems;
private delegate void UpdateUIMethod();
public MainPage()
{
InitializeComponent();
}
private void btnRetrive_Click(object sender, RoutedEventArgs e)
{
LoadData();
}
private void LoadData()
{
context = ClientContext.Current;
web = context.Web;
List lstEmployees = web.Lists.GetByTitle("Employees");
CamlQuery Query = new CamlQuery();
Query.ViewXml = "<View/>";
listItems = lstEmployees.GetItems(Query);
context.Load(lstEmployees);
context.Load(listItems);
context.ExecuteQueryAsync(LoadSucces, LoadFailed);
}
private void DisplayData()
{
foreach (ListItem listItem in listItems)
{
lblOutputLabel.Text += Environment.NewLine + "\n" +
"ID: " + listItem.Id + " Title:" + listItem["Title"] + " First Name : " + listItem["First_x0020_Name"]
+ " Last Name" + listItem["Last_x0020_Name"];
}
}
private void LoadSucces(object sender, ClientRequestSucceededEventArgs e)
{
UpdateUIMethod updateUI = DisplayData;
this.Dispatcher.BeginInvoke(updateUI);
}
private void LoadFailed(object sender, ClientRequestFailedEventArgs e)
{
MessageBox.Show("Request Failed: " + e.Message + ", Stack Trace:" + e.StackTrace);
}
}
}
2) Getting only specific properties
Instead of getting all properties of any specific object, if we want to get only specific properties that we are interested in, we can use lambda expression in query. This will result in less network traffic.
namespace SilverlightSPCOM
{
public partial class MainPage : UserControl
{
private ClientContext context = null;
private Web web = null;
ListItemCollection listItems;
private delegate void UpdateUIMethod();
public MainPage()
{
InitializeComponent();
}
private void btnRetrive_Click(object sender, RoutedEventArgs e)
{
LoadData();
}
private void LoadData()
{
context = ClientContext.Current;
web = context.Web;
context.Load(web,
s => s.Title,
s => s.Description);
context.ExecuteQueryAsync(LoadSucces, LoadFailed);
}
private void DisplayData()
{
lblOutputLabel.Text += "Web Title : " + web.Title + Environment.NewLine + "\n" + " Description:" + web.Description ;
}
private void LoadSucces(object sender, ClientRequestSucceededEventArgs e)
{
UpdateUIMethod updateUI = DisplayData;
this.Dispatcher.BeginInvoke(updateUI);
}
private void LoadFailed(object sender, ClientRequestFailedEventArgs e)
{
MessageBox.Show("Request Failed: " + e.Message + ", Stack Trace:" + e.StackTrace);
}
}
}
3) Create a list and list items
namespace SilverlightSPCOM
{
public partial class MainPage : UserControl
{
private ClientContext context = null;
private Web web = null;
private delegate void UpdateUIMethod();
public MainPage()
{
InitializeComponent();
}
private void btnRetrive_Click(object sender, RoutedEventArgs e)
{
LoadData();
}
private void LoadData()
{
context = ClientContext.Current;
web = context.Web;
ListCreationInformation lstCreationListFromSL =
new ListCreationInformation();
lstCreationListFromSL.Title = "List created from silverlight";
lstCreationListFromSL.TemplateType = (int)ListTemplateType.GenericList;
List lstFromSL = web.Lists.Add(lstCreationListFromSL);
Field fldFirstName = lstFromSL.Fields.AddFieldAsXml(@"<Field Type='Text' DisplayName='First Name'/>", true, AddFieldOptions.DefaultValue);
Field fldLastName = lstFromSL.Fields.AddFieldAsXml(@"<Field Type='Text' DisplayName='Last Name'/>", true, AddFieldOptions.DefaultValue);
Field fldAge = lstFromSL.Fields.AddFieldAsXml(@"<Field Type='Number' DisplayName='Age'/>", true, AddFieldOptions.DefaultValue);
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem listItem = lstFromSL.AddItem(itemCreateInfo);
listItem["Title"] = "1 Item created From SL";
listItem["First_x0020_Name"] = "John";
listItem["Last_x0020_Name"] = "Desilva";
listItem["Age"] = "25";
listItem.Update();
listItem = lstFromSL.AddItem(itemCreateInfo);
listItem["Title"] = "2 Item created From SL";
listItem["First_x0020_Name"] = "Michelle";
listItem["Last_x0020_Name"] = "Alberto";
listItem["Age"] = "24";
listItem.Update();
context.ExecuteQueryAsync(LoadSucces, LoadFailed);
}
private void DisplayData()
{
lblOutputLabel.Text += "List Created successfully and items are also created successfully";
}
private void LoadSucces(object sender, ClientRequestSucceededEventArgs e)
{
UpdateUIMethod updateUI = DisplayData;
this.Dispatcher.BeginInvoke(updateUI);
}
private void LoadFailed(object sender, ClientRequestFailedEventArgs e)
{
MessageBox.Show("Request Failed: " + e.Message + ", Stack Trace:" + e.StackTrace);
}
}
}
Many more examples to come. Keep reading Part-9 of this series.
2 comments:
I am using 2) Getting only specific properties in Silver light and my silver light app renders correctly from documents library but it is failing in button click event to get web title using Lamda expression.
UnHandled error in SilverLight Application The query expression is not supported at Microsoft.SharePoint.Client.DataRetrieval.ProcessMemberAccessQueryExpression
Anonymous,
it should not be, if possible please share code so we get some idea.
Post a Comment