Sharepoint Tutorials Accessing SharePoint List Data with LINQ

In
this Sharepoint Examle we will learn how to use the SharePoint client object model to
program an ECMAScript application against SharePoint data.This sharepoint tutorial will deal with accessing Sharepoint List Data with LINQ Entity Framework.

Here we are creating a demo project which is a standard SharePoint project developed
using the Visual Studio Tools for SharePoint.

It contains two application
pages.

The LAYOUTS folder of the SharePoint file system
contains a javascript library sp.js
which contains the client object model for ECMAScript.

Open the project HelloClientOM
from the starter files in Visual Studio 2010.

Notice the ScriptLink
tag in the PageHead content place
holder. It references the sp.js
library

Double-click the ClientOM1.aspx page
to view the code.

<SharePoint:ScriptLink ID="SPScriptLink" runat="server"

LoadAfterUI="true"
Localizable="false" Name="sp.js" />

Also in ECMScript you need to create a client context before
you can retrieve data from a SharePoint site.

With ECMAScript you can retrieve the client context from the
SharePoint site in which the script runs.

Use the Load method to specify which data to load from
SharePoint.

Use the asynchronous method executeQueryAsync to retrieve the information from SharePoint.

This
method needs a reference to a callback method when the execution succeeds and a
reference to a callback method when the execution fails

The
PageHead content place holder also contains
a javascript function execClientOM. You can use SP.ClientContext.get_Current() to retrieve the client context of
the SharePoint site in which the application page runs.

You can load a number
of SharePoint objects before the execQueryAsync
method is executed to retrieve the desired information from SharePoint.

This
javascript function defines that the current SharePoint site and the lists of
this site needs to be retrieved

The application page contains a content place holder with ID Main. This content place holder contains the controls that make up the user interface.
The execClientOM javascript function is triggered when the user clicks a HTML button
Locate the Main content place holder, which contains only one HTML control, a button.

When the application page runs in the context of SharePoint, clicking the button will execute the javascript that calls the client object model.

Build the project and deploy it to the SharePoint site.
Open an internet browser and navigate to your SharePoint site.

Navigate to the /_layouts/HelloClientOM/ClientOM1.aspx page.

Click the Execute
Client OM button.A message box will pop up, displaying the title of the site
and the number of lists in the site.

You
can achieve the same using JQuery.
The second application page creates a little accordion containing a list box
that will display the lists in the SharePoint site.The project contains a second application page with name ClientOM2.aspx.Take a look at the
PageHead content place holder in this page. The first tag is the ScriptLink tag and contains a reference
to the sp.js file that contains the
client object library.

The following tags contain references to the JQuery API and to some custom style sheets.

The javascript code creates a little accordion containing all the lists in the SharePoint site.
Locate the Main content place holder, which contains an HTML list box and a DIV with ID accordion.

When you look at the javascript you see that some javascript is registered when the page loads.Go back to the <script>
tag in the PageHead content place
holder. The _spBodyOnLoadFunctionNames.Push
function registers the custom javascript function Intialize() when the page loads.

//Entry point for our code

_spBodyOnLoadFunctionNames.push("Initialize");

This function gets a reference to the context of the
SharePoint site. The current site and its lists are loaded. Like in the ClientOM1.aspx page, the execQueryAsync function executes
asynchronously and needs a reference to a function that needs to be called when
the function returns successfully from the server, and a reference to a
function that needs to be called when the call to the server failed.

The
LoadListBox() function executes some
JQuery to add an event handler to
the list box and to enumerate through the SharePoint lists.Take
a look at the code in the LoadListBox()
function. It iterates through the collection of SharePoint lists by using the lists.getEnumerator() function. The
list title and the list id are used to a list item to the list box for every
single SharePoint list

' + ListName
+ '

When the application page runs in the context of SharePoint,
clicking the button will execute the javascript that calls the client object
model.

This sample page only adds an accordion for each
list containing the list title but you can easily add functions to retrieve
information from the selected list using JQuery and the client object model.

Build the project and deploy it to the SharePoint site.Open an internet browser and navigate to your SharePoint
site.

Navigate to the /_layouts/HelloClientOM/ClientOM2.aspx page. When the page loads,
the Initialize() function runs, and
the list box is populated with the lists in the current SharePoint site

Select one or more lists in the list box. An accordion for
each list is added to the <DIV>
element.