Search This Blog

Monday, September 1, 2014

Sharepoint 2010 Get list data Different technique

We can access sharepoint list data using
1). Object model
2).Client object model

1). Object model 

Using object model get sharepoint list data.

      using(SPSite site = new SPSite(SPContext.Current.Web.Url))
      {

          using (SPWeb _web = site.OpenWeb())
            {

                SPList _list = _web.Lists["ListName"];
                SPListItemCollection collection = _list.Items;
                foreach (SPListItem item in collection)
                {
                        string title =     item["Title"] ;
                }
           }
      }

It is very easy to get data from sharepoint list but only we use in sharepoint environment. what about other?
If we want to use in silverlight application or in console application.
for that we use Client object model.

2).Client Object Model

The Client Object Model is really three separate object models, one for the .NET CLR, one for Silverlight, and one for JavaScript. The .NET CLR version is used to create applications such as WinForms, Windows Presentation Foundation (WPF), and console applications, as well as PowerShell scripts. The Silverlight version works with both in-browser and out-of-browser Silverlight applications. The JavaScript version enables your Ajax and jQuery code to call back to SharePoint.

For client object model u have to add reference  Microsoft.SharePoint.Client.dll
then add reference

using Microsoft.SharePoint.Client;
 
        ClientContext clientContext = new ClientContext("http://ServerName");
        List list = clientContext.Web.Lists.GetByTitle("ListName");
        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml = "<View/>";
        ListItemCollection listItems = list.GetItems(camlQuery);
        clientContext.Load(list);clientContext.Load(listItems);
        clientContext.ExecuteQuery();
        foreach (ListItem listItem in listItems)
            Console.WriteLine("Id: {0} Title: {1}", listItem.Id, oListItem["Title"]); 
    
 For more details on client object model please check this MSDN
 We see this is .net client object model code for getting list data in console application.
 How to get List data using Javascript.

Using Javasctipt ECMA Script for getting sharepoint list data 

            var clientContext = null;
      var web = null;
 
      ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");

        function Initialize()
        {
         clientContext = new SP.ClientContext.get_current();
         web = clientContext.get_web();
         this.list = web.get_lists().getByTitle("ListName");
         clientContext.load(list, 'Title', 'Id');
         clientContext.executeQueryAsync(Function.createDelegate(this,        this.onListLoadSuccess),

       Function.createDelegate(this, this.onQueryFailed));
        }

        function onListLoadSuccess(sender, args) {
         alert("List title : " + this.list.get_title() + "; List ID : "+  this.list.get_id());
        }

        function onQueryFailed(sender, args) {
          alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
        }​

No comments:

Post a Comment