Tag Archive | Client Object Model

Autocomplete suggestion in sharepoint using silverlight client object model

In this article i would like to share with you the implementation of auto-complete suggestion in sharepoint using silverlight control.I have considered  ‘AutoCompleteBox’ control in silverlight and have bounded the ‘Title’ of a sharepoint list.

Here goes the code :

MainPage.xaml

<sdk:AutoCompleteBox Height=”28″ HorizontalAlignment=”Left” Name=”autoCompleteBox1″ VerticalAlignment=”Top” Width=”195″  FilterMode=”Contains”  Grid.Column=”2″  />

MainPage.xaml.cs

ClientContext clientContext = new ClientContext(ApplicationContext.Current.Url);
clientContext.Load(clientContext.Web);
list = clientContext.Web.Lists.GetByTitle(“Sample”);
clientContext.Load(list);
ListItemCollection itemColl = list.GetItems(CamlQuery.CreateAllItemsQuery());
clientContext.Load(itemColl);
clientContext.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnSucceed), null);

private void OnSucceed(object sender, ClientRequestSucceededEventArgs args)
{
Dispatcher.BeginInvoke(BindData);
}

private void BindData()
{
List<string>searchItems = new List<string>();

if (itemColl.Count > 0)
{

foreach (ListItem item in itemColl)
{
searchItems.Add(item[Constants.title].ToString());
}
}
autoCompleteBox1.ItemsSource = searchItems;
autoCompleteBox1.Focus()
}

The below screenshot shows the ‘Title’ of the sharepoint list being populated :

Applying theme for silverlight controls

As we know we can create a rich UX using silverlight , i would like to share in this article on “how to apply themes to the silverligh controls“.

1. Download and install silverlight 4 for visual studio 2010, make sure the installtion is successfull.

2. Now Download silverlight 4 toolkit that contains SDK ,some samples, themes and documentations.

3.Navigate to “C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Toolkit\Apr10” to see the contents of the toolkit.

4.Open the silverlight project and add reference to System.Windows.Controls.Theming.Toolkit.dll from bin folder.

5.Also add reference to the theme that you would like to apply for the controls (In this example i have considered reference to System.Windows.Controls.Theming.TwilightBlue.dll ).

6.Go to MainPage.xaml in the project and include the namespace in the UserControl section of the MainPage.xaml file.

xmlns:ThemeBlue=”clr-namespace:System.Windows.Controls.Theming;assembly=System.Windows.Controls.Theming.TwilightBlue”

7.Now add <ThemeBlue:TwilightBlueTheme> just before <Grid x:Name=”LayoutRoot” Background=”White”> and close the tag after Grid definiton.

Here is an example of the silverlight control(with theme) integrated with sharepoint.

Image

Client Object Model

Overview :
One of the most promising feature of Sharepoint 2010 is Client object model . Though it still supports object model on server side , client side will play an important role in sharepoint development in coming days .

In 2007 we would either go for server object model to execute code within server where sharepoint is hosted or web services from remote desktops to get rid of a license . However there has been performance issue consuming web services . Hence microsoft came up with a new concept called “Client Object model” where we can consume sharepoint object within remote servers .

Why to use Client Object model ?
Fewer web service calls improves performance.
It can be called from ,NET Code, javascript(ECMAScript) or silverlight.
Reduces network congestion, as there is fewer round trips b/w client and servers.
Gets rid of licensing issues.

How it works :
When we call API’s from the client side / silverlight applications, these API’s are bundled in to XML by the object model and sent as a request to server for processing . The server after processing sends backs the response[result] as a JSON object , the object model picks up this and converts it to .NET object exposing it to the user. It can be seen that there is a fewer round trips as the entire operation is bundled and executed at once.

Namespaces :
Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime are two prominent namespaces used for consuming object model on the client side using C#.

How to use :
All objects now in client model doesn’t begin with SP , i.e : it is Web for SPWeb, List for SPList .The SP concept has been removed in this model. However there are couple of methods that has to be used .
The object whose properties will be accessed in the code has to be first loaded and executed so that they will be ready to access , we will be getting NotInitializedException otherwise.

Load(object whose value properties will be accessed)
ExecuteQuery()

Implmentation ?
using sp = Microsoft.SharePoint.Client;

using (sp.ClientContext objContext = new sp.ClientContext(“url“))
{
sp.Web web = objContext.Web;

//Load and execute List to access its properties
objContext.Load(web.Lists);
objContext.ExecuteQuery();
foreach (sp.List oList in web.Lists)
{
//do something to access OList properties

}

Hope you will be now able to build the application on you own .