How to Read from SharePoint 2013 List in 6 Ways

Suppose we have SharePoint 2013 and contains a list called “Users”

Now, there are 6 ways to ready the items for this list:

  • SharePoint 2013 Object Model (C#)
  • Client Side Object Model (CSOM)
  • SharePoint 2013 Web Services
  • REST/OData
  • Power Shell
  • LINQ

First, we need to install Microsoft Office Developer Tool for VS 2012 from this link,

because the VS 2012 come without and project for SharePoint 2013

http://www.microsoft.com/web/handlers/WebPI.ashx?command=GetInstallerRedirect&appid=OfficeToolsForVS2012GA

After installing this your VS new project window will be like:

SharePoint 2013 Object Model (C#)

In this example I will create a new project of type “SharePoint 2013 – Visual Web Part”

In the user control, add a GrivView:

<asp:GridView runat=”server” ID=”grvUsers” />

In the Page Load event write the following code snippet

using (SPSite site=new SPSite(SPContext.Current.Site.Url))

{

using (SPWeb web=site.OpenWeb())

{

SPList list = web.Lists[“Users”];

grvUsers.DataSource = list.Items.GetDataTable();

grvUsers.DataBind();

}

}

  • Right click on the project and deploy it
  • Go to any SharePoint page and edit the page
  • Insert your web part into your page from “Custom” group
  • Save and close the page

Client Side Object Model (CSOM)

CSOM is a good choice for running a console or windows application on the end user machine and interact with

SharePoint 2013

Create a new project of type “Console Application”

For this, we need to add the following references:

  • Microsoft.SharePoint.Client
  • Microsoft.SharePoint.Client.Runtime

In the Main method write:

ClientContext cc = new ClientContext(“http://sp2013:2000&#8221;);

Web web = cc.Web;

List list = web.Lists.GetByTitle(“Users”);

CamlQuery caml=new CamlQuery();

ListItemCollection items = list.GetItems(caml);

cc.Load<List>(list);

cc.Load<ListItemCollection>(items);

cc.ExecuteQuery();

foreach (Microsoft.SharePoint.Client.ListItem item in items)

{

Console.WriteLine(item.FieldValues[“Title”]);

}

Console.ReadLine();

SharePoint 2013 Web Services

To read from SharePoint list using web services and jQuery, use http://SPServices.codeplex.com

And for full documentation follow this link http://spservices.codeplex.com/wikipage?title=$().SPServices&referringTitle=Documentation

  • Copy the following JS files to SharePoint layouts “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\LAYOUTS”
    • jQuery
    • SPServcies
  • Open SharePoint Designer 2013
  • Open the your site collection
  • Go to your master page
  • Open it and write the following scripts

<script src=”/_layouts/15/jQuery.js” type=”text/javascript” ></script>

<script src=”/_layouts/15/jQuery.js” type=”text/javascript” ></script>

$().SPServices({

operation: “GetListItems”,

async: false,

listName: “Users”,

CAMLViewFields: “<ViewFields><FieldRef Name=’Title’ /></ViewFields>”,

completefunc: function (xData, Status) {

$(xData.responseXML).SPFilterNode(“z:row”).each(function() {

    alert($(this).attr(“ows_Title”));

});

}

});

Power Shell

In Power Shell you can use the following script:

$web=Get-SPWeb http://sp2013:2000

$list=$web.Lists[“Users”]

$list.Items[0]

$list.Items[0][“Title”]

You use for-each in Power Shell to iterate through all items.

foreach ($item in $list.Items)

{Write-Host $item.Title}

REST/OData

For using REST you have two options:

  • jQuery
  • C# (HttpRequest & HttpResponse)

For REST reference follow this link OData in SharePoint 2013 – Lists and Items

LINQ

To use LINQ, we will use SPMETAL

This will generate a CS file in C:\

Add this file to your application (Console or Windows)

Also, you need to add the following assembly

  • Microsoft.SharePoint.Linq

  • Add the generated class in Power Shell script to your project
  • Wirte the following code

MyWebDataContext context = new MyWebDataContext(“http://sp2013:2000&#8221;);

List<UsersItem> items=context.Users.ToList();

foreach (UsersItem item in items)

{

Console.WriteLine(item.Title);

}

Console.ReadLine();

Advertisements

OData in SharePoint 2013 – Lists and Items

I will explain below the most common GET operations which most of developer may need them while query SharePoint 2013 lists and list items using REST.

  • Get all lists in web

  • Get all list by title

  • Get all items in specific list
    • Request: http://<server>/_api /web1/web2/lists/GetByTitle(‘myList’)/items
    • Result: the below screen shows one item Meta Data in the response

  • Get list item by item ID
    • Request: http://<server>/_api /web1/web2/lists/GetByTitle(‘myList’)/items/GetItemById(4)
    • Result:

  • Return items with specific fields
    • Request: http://<server>/_api /web1/web2/lists/GetByTitle(‘myList’)/items?$select=Title,Age
    • Result:

  • Order items in the query

 

For more information: