Sofware Architecture-SharePoint 2010-Getting name of all the Lists in a site via client-side object model

The new client-side object model provides remote access to the functionality of the SharePoint Foundation server-side object model. In previous releases of SharePoint Foundation, SOAP Web services provided access to only a fraction of the server-side object model, but in SharePoint Foundation 2010, the client object model fills many of the gaps.

Client Object model can be one of the following type:
1. ECMAScript (JavaScript, JScript)
2. .NET managed
3. Silverlight client
According to SDK Documentation:

The new ECMAScript (JavaScript, JScript), .NET managed, and Silverlight client object models each provide a subset of the server object model that is defined in Microsoft.SharePoint.dll, including objects that correspond to major objects at the site-collection level or lower in the SharePoint Foundation hierarchy. To improve security and performance, the client object models focus on the most relevant APIs for client-side development, and do not contain all the types and members that are represented in the server object model.
The client object models are provided through proxy .js and managed .dll files, respectively, which can be referenced in custom applications like other object models. The object models are implemented as a Windows Communication Foundation (WCF) service (.../_vti_bin/client.svc), but uses Web bindings to implement efficient request batching. All operations are inherently asynchronous, and commands are serialized into XML and sent to the server in a single HTTP request. For every command, a corresponding server object model call is made, and the server returns a response to the client in compacted JavaScript Object Notation (or JSON) format, which the proxy parses and associates with appropriate objects. 

1. Getting name of all the Lists in a site via ECMAScript (JavaScript, JScript).
Create a new site page and in contentplaceholder add following code:

<a href="#"  onclick="getAllSPLists();"> Click me</a>
<script type="text/javascript">
var  myLists;
function getAllSPLists()
{
 // get current context
 var ctx =  SP.ClientContext.get_current();
 // point  to get all the lists 
 myLists =  ctx.get_web().get_lists();
 // Load all lists
 ctx.load(myLists );
 // async excute
 ctx.executeQueryAsync(Function.createDelegate(this,this.succeeded) , Function.createDelegate(this,this.failed));
 }
 
 function succeeded(sender, args)
 {
 alert('Call succeeded......');
//get enumerator
 var ListEnumerator = this.myLists.getEnumerator();
// iterate to all items
 while(ListEnumerator.moveNext())
 {
 // point to current item
 var currentItem = ListEnumerator.get_current();
 // show item
 alert('Item:  ' + currentItem.get_title());
 }
 
 }
 function failed(sender, args)
 {
 alert('Failed. ' + args.get_message() + '\n' + args.get_stackTrace());
 }
</script>

Save site page and run it to see the result.
2. Getting name of all the Lists in a site via .NET managed
Create a console project and add reference to Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll
After that following piece of code provide name of all the lists in a sharepoint site:

 using (var ctx = new ClientContext("http://win2k8-ultimate/sites/Call/"))
            {
               var list = ctx.Web.Lists;
               ctx.Load(list );
               ctx.ExecuteQuery();
               foreach (var item in list)
               {
                   Console.WriteLine("Item: " + item.Title + " - " + item.Description+ "\n");
               }
            }

Console will show name of all lists.
3. Getting name of all the Lists in a site via Silverlight Client.
Create a silver project and in main page “MainPage.xaml”, add a list box with following tag as:

<ListBox Height="100" HorizontalAlignment="Left" Margin="10,10,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" />

The code behind of this page will be as:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;

namespace ClientSideSilverApp
{
    public partial class MainPage : UserControl
    {
        IEnumerable<List> spList = null;
        public MainPage()
        {
            InitializeComponent();
            using (var ctx =  ClientContext.Current)
            {

                spList = ctx.LoadQuery(ctx.Web.Lists);
                ctx.ExecuteQueryAsync(Succeded, Failed);

            }
        }

        private void Succeded(object sender, ClientRequestSucceededEventArgs e)
        {
            Dispatcher.BeginInvoke(BindList);
        }
        private void Failed(object sender, ClientRequestFailedEventArgs e)
        {
            
        }

        private void BindList()
        {
            listBox1.Items.Clear();
            listBox1.ItemsSource = spList;
            listBox1.DisplayMemberPath = "Title";
        }
    }
}

Compile the project to make sure that there is no compile time error.
Now login to sharepoint site, Upload .xap file generated from above acivity to document library of sharepoint site and copy the URL of file.
Go to “Edit” >> “Insert”, click on web part, select category of “Media and Content” , select “Silverlight web part” and click on add. Provide the URL of file obtained from previous step. Configure the properties of web part as per requirement.
This will show list box with name of all the list in sharepoint site.
Happy coding for SharePoint……:)

Advertisements

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

%d bloggers like this: