Share via


SharePoint 2010: Client Object Model with SilverLight

Introduction

SharePoint has been a worldwide success in its adoption as a tool of corporate Portals and Collaboration, this article is perspective view your demo as a development tool for enterprise solutions. 

In this article I will also give attention to one of the new services offered by SharePoint 2010 which is the Client for Silverlight Object Model, with the creation of a project and call methods to demonstrate their ability to access existing content in SharePoint sites. 

Solution 

The SharePoint platform is an integrated set of resources that can help improve organizational effectiveness by providing comprehensive content management and enterprise search, accelerating shared business processes, and facilitating information sharing for better business insight. 

The services offered range from Content Management, Document Management, Business Intelligence, Communities, Platform Integration external to Microsoft through the BCS (Business Connectivity Services), Workflows, Web Office app (Integration of Microsoft Office tools in Web format), SharePoint Designer 2010 , Project Management, Research Services Portal and external content, with different types of authentication using federations and other services. Finally a fairly comprehensive platform. 

During the past years the platform has grown to the level of service and level of importance in business institutions, making a decisive bets more than Microsoft, with its implementation and integration of all its products on this platform, becoming the operating system in the companies. 

One of its advantages is the ability to integrate the various Microsoft technologies and the ability of external integration platforms such as SAP, Oracle and others, to make a point of information sharing only the company, and built a Web platform that makes it very affordable to reach. 

In the next table are the services that are available for SharePoint 2010. 

http://aaclage.files.wordpress.com/2010/11/8f613e0ed52c7627af08f23d6d8ab06b.png?w=300 

With the release of SharePoint 2010 were provided multiple new services such as LINQ, new APIs for data access (using the Client Object Model), using REST - ADO.NET Data Services and using Ajax with webParts Asynchronous processes (without use of postback pages) and other new services. 
SharePoint 2010 features three new Client API's to interact with content from SharePoint sites: 

  • From applications. Net "never less than 3.5 Framework";
  • From Silverlight applications "never less than 2.0 Silverlight"
  • From ECMAScript or JavaScript, Jscript

http://aaclage.files.wordpress.com/2010/11/41a042f01664e7167a38faf6ad09b428.png?w=295 

Client Object Model Architecture 

These new Client Object Model API provides an object-oriented system to interact with SharePoint data remotely, which is very easy to use because its nomenclature is rather like the SharePoint Object Model Server. 

To demonstrate how to access the API Objects on the next table shows the different API's for various platforms and similarly the use of the Object Model. 

http://aaclage.files.wordpress.com/2010/11/f626b0061c2ab991719d9eb8d473d035.png?w=300 

Example 
In this example, I will create a small project in Visual Studio 2010, with an application in Silverlight 4, using the SharePoint API and integrating SharePoint 2010. 

To support our SharePoint solution I created a list called "General" with a set of test data. 

http://aaclage.files.wordpress.com/2010/11/2109812732eb8f9d098ca5687445a7a8.png?w=300  

Create a new project with Visual Studio 2010 Template in "Silverlight" and select the option to join a new Web site with the Silverlight version 4:

http://aaclage.files.wordpress.com/2010/11/b56ae32c04aa109d1ade1ee4ae6ccf83.jpg?w=300 http://aaclage.files.wordpress.com/2010/11/58761ac3f5f79f514f0b4b0f71fab127.png?w=300

To make calls to the SharePoint content through the API in Silverlight, you must add references to the DLL's that are in the installation folder of the SharePoint 2010 (Microsoft.SharePoint.Client.Silverlight and Microsoft.SharePoint.Client.Silverlight.Runtime ), typically in C: \ Program Files \ Common Files \ Microsoft Shared \ Web Server Extensions \ 14 \ TEMPLATE \ LAYOUTS \ ClientBin.
  

http://aaclage.files.wordpress.com/2010/11/8ff040a705fd76e61af9cb530beb8bd3.png?w=283 

Microsoft has released a package with the DLLs in SharePoint. NET Client Object Model, which can be downloaded to the client and their distribution. 
SharePoint Server 2010 Client Object Model Redistributable 
http://translate.googleusercontent.com/translate_c?hl=en&rurl=translate.google.com&sl=pt&tl=en&twu=1&u=http://www.microsoft.com/downloads/en/details.aspx%3FFamilyID%3Db4579045-b183-4ed4-bf61-dc2f0deabe47&usg=ALkJrhg4oda9VM8c05RlafYH5JsCIl5eLw 
Using Visual Studio 2010 there is the possibility of integrating Microsoft Expression Blend 4 with our applications developed in Silverlight. 

http://aaclage.files.wordpress.com/2010/11/b123f76861066172bc1be05f1eec08fc.png?w=300 

After we made our customization we can return to our solution in Visual Studio 2010 and start creating our methods to return the contents of the Site. For this I created two methods, a return to the list of our SharePoint site and another to return the contents of a list in a Datagrid. 
Method 1: 
Returns the name of the list on the SharePoint site using the Client Object Mode. We used a lambda expression to return the lists and the associated title. 

private void LoadLists()
{
ClientContext clientContext = new ClientContext(“http: //[Site]“);
oWebsite = clientContext.Web;
collList = oWebsite.Lists;
clientContext.Load(oWebsite,
website => website.Title);

//Lambda expression para retornar as Listas
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()
{ 
collList = oWebsite.Lists; 
listBox1.ItemsSource = listInfo; 
listBox1.DisplayMemberPath = { 
collList = oWebsite.Lists; 
listBox1.ItemsSource = listInfo; 
listBox1.DisplayMemberPath = "Title" ;
}
private delegate void UpdateUIMethod(); 
Example: http://msdn.microsoft.com/en-us/library/ee538971.aspx
Method 2: Returns data in the List "General" to a DataGrid using CAML to make filters to return the contents: 
private void LoadGrid()
{
      //Context of the SharePoint Site
      var context = new ClientContext( " http:// [Site]" );
      context.Load(context.Web);
      //return Object List“Geral”
      List projects = context.Web.Lists.GetByTitle( "Geral" );
      context.Load(projects);
      //CAML Query to filter values from the list
     CamlQuery camlQuery = new CamlQuery();
     camlQuery.ViewXml = "<View><Query><Where><Geq><FieldRef Name='ID'/>" +
        "<Value Type='Number'>0</Value></Geq></Where></Query><RowLimit>100</RowLimit></View>" ;
      _projectItems = projects.GetItems(camlQuery);
      context.Load(_projectItems);
      context.ExecuteQueryAsync(OnRequestSucceeded, null );
} 
#region Methods

private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)
{
            Dispatcher.BeginInvoke(BindData);
}

private void BindData()
{
            //User List "Geral" as List
             var list = new List<Geral>();
            foreach (var li in _projectItems)
            {
                        list.Add( new Geral
                       {
                         Title = li[ "Title" ].ToString(),
                         Nome = li[ "Nome" ].ToString(),
                         Activo = Convert.ToBoolean(li[ "Activo" ].ToString())
                       });
             }
            //Add List info into Datagrid
            dataGrid1.ItemsSource = list;
}

http://aaclage.files.wordpress.com/2010/11/e14c0ef9b8d870a3c65842d8c6f6e022.jpg?w=300 

In the following example was created in a short form for the Silverlight list "Advertising." This list is a set of reference articles to the site and this method shows how one can create a simple form, without the need for customization of pages aspx.Foram added to the Tab "form" of Silverlight 3 fields Fill in our article, as the URL of the site where it is intended, title and body. 

http://aaclage.files.wordpress.com/2010/11/0a9d61c7fd7942ea141b3c42db5be707.jpg?w=237 

Method 3: 
This method will create a new entry in the List of SharePoint a new ad. 

if (!TxtTitle.Text.Equals( "" ))
{
           ClientContext clientContext = new ClientContext(TxtUrl.Text);
           Web oWebsite = clientContext.Web;
 
           ListCollection collList = oWebsite.Lists;
 
           oList = clientContext.Web.Lists.GetByTitle( "Anuncios" );
           ListItem oListItem = oList.AddItem( new ListItemCreationInformation());
           oListItem[ "Title" ] = TxtTitle.Text;
           oListItem[ "Body" ] = TxtBody.Text;
           oListItem.Update();
           clientContext.Load(oList,list => list.Title);
           clientContext.ExecuteQueryAsync(onQueryInsertSucceeded, onQueryInsertFailed);
}
else
{ MessageBox.Show( "Empty!!" ); } 

http://aaclage.files.wordpress.com/2010/11/de2d9bbf230bde27c6648b51b4bc2c56.jpg?w=300 

With the creation of two small examples we can create our solution "AccessData.XAP." 

SharePoint Silverlight Web Part

SharePoint provides tools to integrate the project developed in Silverlight and add in our SharePoint sites in a very simple way. 
The first step is to add our Silverlight project in this case "AccessData.XAP" and add to our site in SharePoint and can be a Folder, or even a Document Library in the directory or folder "Hive" where SharePoint is installed in server. In this case I created a Folder within the site in SharePoint. These folders are not visible to end users, provided that the file is not deleted accidentally. 

http://aaclage.files.wordpress.com/2010/11/1fb859c54352ce13a9e936c1554a90b2.jpg?w=300 

Once added to our solution in Silverlight, accessing our website and added in a default SharePoint Web Part that is "Silverlight Web Part." One of the parameters that is required is the path where you will find our solution in Silverlight. 

http://aaclage.files.wordpress.com/2010/11/636b82c4206a82c792175ad6dfca804c.jpg?w=300 

Once everything is correctly added, will be made available on our website our solution for Silverlight within SharePoint WebPart in being able to work and interact with the content of the Site where you are. 

http://aaclage.files.wordpress.com/2010/11/91e22fe23667a88f8d457dee782f0d56.jpg?w=300 

When we are working on Web application environment Fiddler is an essential tool for capturing and validating the traffic on our Web when our Silverligth project is working we can find the transactions through the Client OM, and we find a call to the WCF Web Service "client.svc" and an XML Web Service for communication and transaction operations. 

http://aaclage.files.wordpress.com/2010/11/188b7a33db9b5b2691284f0f7cce9252.jpg?w=300 

There's another way you can interact with data in SharePoint with Silverlight through the "REST" in WCF with all its potential but that is another article... 

http://aaclage.files.wordpress.com/2010/11/4e321f219c07e22360a719f87565660d.jpg?w=300 

References