How to interacting with the various data sources in sharepoint?. The Complete SharePoint Developer Course 2023 [Videos].

we will be covering one of the most common tasks of SharePoint i.e. interacting with the various data sources such as lists or document libraries. A great thing about SharePoint is that you have a number of options available for interacting with data. Some examples are Server Object Model, Client-Side Object Model, REST services etc.

Before you can do anything with SharePoint programmatically, you need to establish a connection and context with your SharePoint site. However, for this we need SharePoint on Premises, which can be installed on Window Server.

You need to add reference in your project to Microsoft.SharePoint.dll or Microsoft.SharePoint.Client.dll. With the appropriate references added to your project, you can then begin to set the context and code within that context.

Let us have a look at a simple example.

Step 1 − Open Visual Studio and create a new project from File → New → Project menu option.

Step 2 − Select Windows from Templates → Visual C# in the left pane and choose Console Application in the middle pane. Enter the name of your project and click OK.

Step 3 − Once the project is created, right-click the project in Solution Explorer and select Add → References.

Console Application

Step 4 − Select Assemblies → Extensions in the left pane and check Microsoft.SharePoint in the middle pane and click OK.

Now right-click the project again in Solution Explorer and select Properties.


Step 5 − Click the Build Tab in the left pane and uncheck the Prefer 32-bit option.

Build Tab

Step 6 − Now go back to the Program.cs file and replace it with the following code.

using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharePointData {
   class Program {
      static void Main(string[] args) {
         using (var site = new SPSite("http://waqasserver/sites/demo")) {
            var web = site.RootWeb;
            var lists = web.Lists;
            foreach (SPList list in lists) {
               Console.WriteLine("	" + list.Title);

Note − In the above code first created a new SPSite object. This is a disposable object, so it is created within a using statement. The SPSite constructor takes in the URL to the site collection, which will be different in your case.

The var web = site.RootWeb will get the root of the site collection.

We can get the lists using web.Lists and print the title of the list items.

When the above code is compiled and executed, you will see the following output −

SharePoint Tutorials
   Composed Looks
   List Template Gallery
   Master Page Gallery
   Site Assets
   Site Pages
   Solution Gallery
   Style Library
   Theme Gallery
   User Information List
   Web Part Gallery

See All

Comments (719 Comments)

Submit Your Comment

See All Posts

Related Posts

SharePoint / Blog

What is SharePoint?

SharePoint is a platform to support collaboration and content management system. It is a central web-based portal. Using SharePoint, you can manage your colleague’s and your own documents, social activities, data, and information.
8-Apr-2022 /49 /719

SharePoint / Blog

What is different types and versions to start working on SharePoint?

There are three main ways to install and use SharePoint − SharePoint Foundation SharePoint Server Office 365
8-Apr-2022 /49 /719

SharePoint / Blog

What are the default set of capabilities (or features) built into SharePoint?

we will be covering the default set of capabilities (or features) built into SharePoint that enables you to take advantage of the platform without doing any development.
8-Apr-2022 /49 /719