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 :

Advertisements

2 thoughts on “Autocomplete suggestion in sharepoint using silverlight client object model

  1. I assume the following code:

    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)

    is in the constructor of MainPage? This will retrieve all of the list items. Is there a way to retrieve just the items that match vs. the entire column and then filter through them?

    • Hi Daoud,

      To anwser your question :
      Yes.You can call this code within the constructor of the MainPage.
      Yes, you can use CAML Query to filter the items based on some condition.

      Here is an example,

      string query = “Add your CAML Query here”;
      CamlQuery cQuery = new CamlQuery();
      cQuery.ViewXml = query;
      ListItemCollection itemColl = list.GetItems(cQuery);

      Its just that you are replacing CamlQuery.CreateAllItemsQuery() in the code with the filter condition.

      You can use CAML builder tool to construct query.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s