dotnet thoughts 

a dotnet developer's technical blog

How to Add Items to Sharepoint list using Silverlight Object Model

Last few days I am working around Silverlight Object Model with sharepoint. In this post I am explaining about adding items to Sharepoint list using Silverlight Object Model. I created a list in sharepoint with few columns like Name, Email and Comments.

And I created a Silverlight application with few textboxes and a submit button.

Silverlight Screen

Silverlight Screen

Here is the code.

ClientContext clientContext = null;
Web web = null;
List list = null;

clientContext = new ClientContext("http://sharepoint:2010");
web = clientContext.Web;
list = web.Lists.GetByTitle("samplelist");
ListItem listItem = list.AddItem(new ListItemCreationInformation());
listItem["Title"] = Guid.NewGuid().ToString();
listItem["Name"] = this.txtName.Text;
listItem["Email"] = this.txtEmail.Text;
listItem["Comments"] = this.txtcomments.Text;
listItem.Update();
clientContext.Load(list, olist => olist.Title);
clientContext.ExecuteQueryAsync(
    new ClientRequestSucceededEventHandler(delegate(object o, ClientRequestSucceededEventArgs successargs)
    {
        this.Dispatcher.BeginInvoke(new Action(delegate()
        {
            MessageBox.Show("New Item Created " + listItem["Title"]);
        }), null);
    }),
    new ClientRequestFailedEventHandler(delegate(object o, ClientRequestFailedEventArgs failedArgs)
    {
        this.Dispatcher.BeginInvoke(new Action(delegate()
        {
            MessageBox.Show("New Item creation failed");
        }), null);
    })
);

Full screen option in Silverlight

Like Adobe Flash, Silverlight also supports full screen option. You can enable Full Screen option by setting the IsFullScreen property to true. This property available under Application.Current.Host.Content class.

You can toggle the Full Screen mode and normal mode using

Application.Current.Host.Content.IsFullScreen
= !Application.Current.Host.Content.IsFullScreen;

Happy Coding… :)

Hosting Silverlight application in Sharepoint 2010

Microsoft SharePoint 2010 comes with an out of the box Silverlight webpart which helps to host Silverlight applications in SharePoint. For hosting the Silverlight application you need to provide the URL of the Silverlight executable in the URL property of the webpart.

Configure URL Property of Silverlight Web Part

Configure URL Property of Silverlight Web Part

You can also pass the parameters to the Silverlight application using InitParams property.

Init Params Property

Init Params Property

You need to implement the Silverlight application such a way that it can accept and use the InitParams, which can be consumed in Application_Start event.

private void Application_Startup(object sender, StartupEventArgs e)
{
    IDictionary<string, string> parameters = e.InitParams;
    foreach (KeyValuePair<string,string> parameter in parameters)
    {
        Console.WriteLine("{0} - {1}", parameter.Key, parameter.Value);
    }
}

How to access SharePoint List Items using Silverlight Object Model

In the last postI introduced the Silverlight Object Model, which helps to access the SharePoint objects directly from Silverlight. In this I am posting about how to access SharePoint List Items using Silverlight Object Model. In this I am using Caml query object, which is used to get the Fields.

private Web oWebSite = null;
private List oList = null;
private ListItemCollection oListItems = null;
private ClientContext clientContext = null;

this.clientContext = ClientContext.Current;
if (this.clientContext == null)
{
this.clientContext = new ClientContext(url);
}
using (this.clientContext)
{
this.oWebSite = clientContext.Web;
this.oList = this.oWebSite.Lists.GetByTitle(this.listName);
this.oListItems = this.oList.GetItems(CamlQuery.CreateAllItemsQuery());
    clientContext.Load(this.oListItems);
clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);
}

private void onQuerySucceeded(object sender, ClientRequestSucceededEventArgs args)
{
//FillItems method converts the List items to Class Properties
//and binds them to the Silverlight User Interface.
this.Dispatcher.BeginInvoke(new Action(FillItems));
}

private void onQueryFailed(object sender, ClientRequestFailedEventArgs args)
{
MessageBox.Show("An error occured.\n" + args.Message + "\n" + args.StackTrace);
}

And if you don’t want all the columns or you want to specify the columns you want to access you can use Caml Query object’s ViewXml property.

CamlQuery camlQuery = new CamlQuery();
string query = "<OrderBy><FieldRef Name='Name' Ascending='False' /></OrderBy>";
camlQuery.ViewXml = query;
this.oListItems = this.oList.GetItems(camlQuery);

You can access CustomLists and Document Libraries by this method.

Using the Silverlight Object Model

Recently I moved to a new project where I have to integrate Silverlight with SharePoint. I had done a few posts regarding this also. Yesterday I found that in SharePoint 2010(SharePoint foundation), Microsoft introduces Silverlight Object Model which helps to access SharePoint Lists and Document Libraries directly from Silverlight.

Here is my first experiment with Silverlight Object Model which helps to retrieve all the Lists from a given SharePoint site, and displays it in a Combo Box.

private Web oWebSite;
private ListCollection collList;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    using (ClientContext clientContext = new ClientContext("http://sharepoint2010/"))
    {
        this.oWebSite = clientContext.Web;
        this.collList = this.oWebSite.Lists;
        clientContext.Load(collList);
        clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);
    }
}

private void onQuerySucceeded(object sender, ClientRequestSucceededEventArgs args)
{
    this.Dispatcher.BeginInvoke(new Action(DisplayLists));
}
private void onQueryFailed(object sender, ClientRequestFailedEventArgs args)
{
    MessageBox.Show("An error occured.\n" + args.Message + "\n" + args.StackTrace);
}

private void DisplayLists()
{
    foreach (List item in collList)
    {
        lstItems.Items.Add(item.Title);
    }
}

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.

« Newer PostsOlder Posts »