Shahid Riaz Bhatti

if(my.work == “Interesting” || my.availableTime > my.workHours) { this.blog.Post();}

Read data from SharePoint List

January 31
by Shahid Riaz Bhatti 31. January 2010 16:04

In my previous POST i showed that how we can use SharePoint object model to insert records into a SharePoint List. In that example I made a console application which reads a comma separated  file and then inserts those records into SharePoint List. Topics which I covered in that example are given below:

  • SPWeb
  • SPList
  • SPListItemCollection

The URL of that example is given below: Click here to see that example.

So the concept which we used in that example is very useful. We can make a client application (i.e. Window/Web) which could be used to insert take input from the user and inserts those records into SharePoint List.

Now today I will show you that how can we retrieve data from a SharePoint list and can display those records in a tabular form to user.

Possible approaches are given below:

  1. Make a Window/Web application which Query the SharePoint list and display those records into a Datagrid/DataList or repeater.
  2. We can make a console application which query the SharePoint List and show the data in console screen.
  3. We can make a user control, which query the SharePoint list and show the result set into Datagrid/DataList or repeater. Then we can put that user control directly on any web page or can place that user control in SharePoint web page.
  4. Another approach is also that we wrap the user control into a web part and then place that web part on Moss web site page.
  5. Create Web part, which query the SharePoint list and display the result set into a tabular form.

I have used all the above mentioned to display SharePoint list. For this example I will use approach # 5.

Topics which we will learn in this example are:

  1. SPWeb
  2. SPContext
  3. SPList
  4. SPListItem
  5. SPListItemCollection
  6. Collaborative Application Markup Language (CAML)
  7. WebPart

My purpose is just to query the sharepoint list and display data into a tabular form, So I will not be using rich css to to decorate my web part.

Here is the summary of my Web part:

  • Create a SharePoint Web part.
  • I will declare and Initilize my controls in CreateChildControls event of the web part to add my controls in web part.
  • In the same event (i.e. CreateChildControls) I will query my SharePoint list and will populate my controls with the SharePoint List data.                                                                   

Part of my web part code is given below:
1-31-2010 7-32-55 AM

In the above snapshot I have highlighted the section which is using CAML. This will be used to fetch the record from the SharePoint List. Rest of the code is self explanatory. In the rest of the code I am pulling data from individual column of the list and inserting it into a table cell.

This is It.

Note:

 

  • Code file of the web part is attached in this POST. (File Name is WebPart1)
  • List template which I am using is also attached in this POST. (MYNews.stp)

 

You can follow this approach, but if you want to use my code, then you have to make list from the list template which is attached in this POST.

Basically this is first part of the web part. In my next web part I will make another web part which will open the detailed news, when user click on the see more link of this web part.

Regards,
Shahid Riaz Bhatti

WebPart1.cs (5.98 kb)

MyNews.stp (13.83 kb)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , ,

MOSS | WSS

RecentComments

Comment RSS

Most comments

supplynflshop supplynflshop
51 comments
tiffany-bracelets tiffany-bracelets
39 comments
AVI to iPad AVI to iPad
36 comments