Tuesday, November 29, 2011

Client Object Model with SilverLight

This is a very old article about SharePoint 2010 i have made in Portuguese for a IT Portuguese Magazine "Portugal a Programar", this article is for beginners and show you how you can use the Client Object Model with SilverLight.This article have references to MSDN articles that you can explore and understand how you can use the Client Object Model.
Hope you like this article....
For the Portuguese version of this article can be saw here.

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.
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
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.
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.
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:

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.
 
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://www.microsoft.com/downloads/en/details.aspx?FamilyID=b4579045-b183-4ed4-bf61-dc2f0deabe47
Using Visual Studio 2010 there is the possibility of integrating Microsoft Expression Blend 4 with our applications developed in Silverlight.
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();
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;
}
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.
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!!" ); }
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.
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.
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.
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.
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...

References

Using the Silverlight Object Model
http://msdn.microsoft.com/en-us/library/ee538971.aspx
Using the SharePoint Server 2010 Managed Client Object Model
http://msdn.microsoft.com/en-us/library/ee857094.aspx
Using the SharePoint Server 2010 Managed Client Object Model with the Open XML SDK 2.0
http://msdn.microsoft.com/en-us/library/ee956524.aspx
SharePoint 2010 SDK
SharePoint 2010 Reference: Software Development Kit
4 Create a Silverlight Web Part for SharePoint 2010
http://msdn.microsoft.com/en-us/magazine/ff956224.aspx
Post a Comment