Using the Silverlight Object Model
Published: May 2010
Available in SharePoint Online
Microsoft SharePoint Foundation 2010 supports implementation of the Silverlight client object model in two contexts: within a Silverlight Web Part, and within the Silverlight Cross-Domain Data Access system, through which a Silverlight application interacts with Microsoft SharePoint Foundation 2010 data. A third possibility--modifying client access cross-domain policy on the server--opens security risks and is not supported in SharePoint Foundation 2010.
Because query execution is asynchronous when you use the SharePoint Foundation Silverlight object model, you must pass delegates for callback methods as parameters in the ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) method, similarly to query execution in the JavaScript object model. However, to run code that makes changes in the user interface (UI) through the Silverlight object model, you must delegate this work to the Dispatcher object of the thread that created the UI by calling BeginInvoke, as seen in the following examples.
To use the SharePoint Foundation Silverlight object model within a Silverlight Web Part, you can create a Silverlight application in Visual Studio, and add your code to the Page class in the default Page.xaml.cs file of your project. After you build your project, upload the project's application package (.xap) file to a document library. Insert a Silverlight Web Part into a Web Parts page and point the URL source of the Web Part to the .xap file in the document library.
The following examples assume the definitions of a Button control and a TextBlock control in the MainPage.xml file of the project.
Retrieving list data
The following example shows how to retrieve SharePoint Foundation data within the context of a Silverlight application. The example involves using event handlers for success or failure of query execution. The onQuerySucceeded method creates a delegate, UpdateUIMethod, to display returned list data in the UI.
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; namespace Microsoft.SDK.SharePointServices.Samples { public partial class MainPage : UserControl { Web oWebsite; ListCollection collList; IEnumerable<List> listInfo; public MainPage() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { ClientContext clientContext = ClientContext.Current; oWebsite = clientContext.Web; ListCollection collList = oWebsite.Lists; clientContext.Load(oWebsite, website=>website.Title); listInfo = clientContext.LoadQuery( collList.Include( list=>list.Title, list=>list.Fields.Include( field=>field.Title).Where( field=>field.Required == true && field.Hidden != true))); clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed); } private void onQuerySucceeded(object sender, ClientRequestSucceededEventArgs args) { UpdateUIMethod updateUI = DisplayInfo; this.Dispatcher.BeginInvoke(updateUI); } private void onQueryFailed(object sender, ClientRequestFailedEventArgs args) { MessageBox.Show = "Request failed. " + args.Message + "\n" + args.StackTrace; } private void DisplayInfo() { MyOutput.Text = "Title: " + oWebsite.Title; collList = oWebsite.Lists; foreach (List oList in listInfo) { MyOutput.Text += "\n\tList: " + oList.Title; FieldCollection collField = oList.Fields; foreach (Field oField in collField) { MyOutput.Text += "\n\t\tField: " + oField.Title; } } } private delegate void UpdateUIMethod(); } }
The previous example uses the Current property to specify the current request context, instead of using the ClientContext(String) constructor and specifying a URL. You can upload, for example, the .xap file to Shared Documents in the root Web site of the site collection, and create a Web Parts page in a child Web site anywhere beneath the root Web site, and add a Silverlight Web Part to this page that points to the .xap file and therefore implements its logic. In other words, your code might live in the root Web site, but its logic becomes available to any Web site in the site collection.
Creating a list item
The following example shows how to create a list item in a specific list. The example uses a delegate to display information about the new item in the UI.
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; namespace Microsoft.SDK.SharePointServices.Samples { public partial class MainPage : UserControl { private List oList; private string siteUrl = "http://MyServer/sites/MySiteCollection/MyWebSite"; public MainPage() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { ClientContext clientContext = new ClientContext(siteUrl); Web oWebsite = clientContext.Web; ListCollection collList = oWebsite.Lists; oList = clientContext.Web.Lists.GetByTitle("Announcements"); ListItem oListItem = oList.AddItem(new ListItemCreationInformation()); oListItem["Title"] = "My new item"; oListItem["Body"] = "This is my new Silverlight item."; oListItem.Update(); clientContext.Load(oList, list => list.Title); clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed); } private void onQuerySucceeded(object sender, ClientRequestSucceededEventArgs args) { UpdateUIMethod updateUI = DisplayInfo; this.Dispatcher.BeginInvoke(updateUI); } private void DisplayInfo() { MyOutput.Text = "New item created in " + oList.Title; } private delegate void UpdateUIMethod(); private void onQueryFailed(object sender, ClientRequestFailedEventArgs args) { MessageBox.Show("Request failed. " + args.Message + "\n" + args.StackTrace); } } }
I am not able to add Microsoft.Sharepoint.Client object as a reference in Silverlight Project.
- 12/30/2010
- Balaji_rcs
- 8/24/2011
- Michael M. Bangoy
- 11/19/2010
- Chad Beard
I'm attempting to access List Item field values via the Client Object Model. I'm not sure that I understand the process correctly. I'm retrieving fiels via the the managed include accessor as the example illistrates using Linq to Objects.
I am able to retrieve Id and DisplayName using the following syntax :
clientContext.Load(collListItem,
items => items.Include(
item => item.Id,
item => item.DisplayName));
However, when I attempt to retrieve other field values in the list for instance the 'URL' field, which I know exists ( You can see this by browsing to the list and exploring the field names used) via the following method
...
item => item.Id,
item => item.DisplayName,
item => item["URL"]));
I get an exception : "Invalid cross-thread access"
When I remove the ",item => item["URL"]" section the query executes successfully.
Can anyone tell me how I have to go about retrieving field values in lists and if there is an option to retrieve ALL fields in the ListItem by default?
Regards,
Pieter Theron
- 10/4/2010
- Pieter Theron
- 8/16/2010
- Michael Nagy
- 8/16/2010
- Michael Nagy
- 8/16/2010
- Michael Nagy
- 4/6/2010
- Nathaniel Granor
ive create my sliverlight app but i want to do something that affects sharepoint, and i cant find that Microsoft.SharePoint.Client dll to reference it
any help
- 3/15/2010
- zafer_510
I am creating a silverlight application and deploying it as a feature on sharepoint using client object model.Can anybody tell me how to retrieve all list in a site which contains a particular type of content type.E.g if custom content type - "My Content Type" is used in different lists in a site then i want to retrieve all these lists in silver light application.
Please reply to this query as i need it urgently...
Thanks.
- 2/22/2010
- ursYash
