Jan 31

Read data from SharePoint List

MOSS | WSS 6 Comments »

Be the first to rate this post

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

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)



[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags: , , , , ,

Jan 24

Hi,

I am writing on my blog after a very long long time and I logged in on my web site after several unsuccessful attempt. :)

Well I have just started studying Sharepoint. So this is not for the guru. It is just for the people who just started Sharepoint like me.

In this example I will write a console application, which will read some data from a comma separated file (.csv) and will insert that data into sharepoint list.

I created a custom list in MOSS and named it myData. This list has some columns which are given below:

FirstName, LastName, age.

I also made a .csv file which have some data.

Snapshot of the sharepoint list is given below. You can see that this list is empty.

1

Code of my class is given below. You can also find this class in attachment of this post.

Code is self explanatory and also commented.

here is the code:

   1:  class Program
   2:      {
   3:          static void Main(string[] args)
   4:          {
   5:              try
   6:              {
   7:                  SPSecurity.RunWithElevatedPrivileges(delegate()
   8:                  {
   9:                      //OPEN THE REQUIRED WEB
  10:                      SPSite site = 
                           new SPSite("http://qaiser-6tmdbbtq:40934");
  11:                      SPWeb web = site.OpenWeb();
  12:                      // REQUIRED LIST OF THE WEB
  13:                      SPList lstNames = web.Lists["myData"];
  14:                      //READ THE CSB FILE

15: StreamReader sr =

File.OpenText("C:\\Names.csv");

  16:                      string Data = string.Empty;
  17:                      //LOOP THROUGHT AL THE FILE
  18:                      while (!sr.EndOfStream)
  19:                      {
  20:                          Data = sr.ReadLine();
  21:                          if (Data.IndexOf(',') > 0)
  22:                          {
  23:                              //SPLIT DATA
  24:                              string[] Columndata = 
                                    Data.Split(new char[] { ',' });
  25:                              // DECLARE LIST ITEM FOR ADD
  26:                              SPListItem item = lstNames.Items.Add();

27: // ASSIGN APPROPRITE VALUES FROM THE

// DATA

  28:                              item["Title"] = 
                                   Columndata[0].ToString();

29: item["LastName"] =

Columndata[1].ToString();

  30:                              item["Age"] = 
                                Convert.ToInt32(Columndata[2].ToString());

31: // DATA WILL NOT BE INSERTED UNLESS YOU

// CALL UPDATE METHOD OF LIST ITEM.

  32:                              ///NOTE:

33: ///item.Update was throwing an error

//which was saying that I dont have rights to insert

  34:                              ///data into the list. So to make sure 
                    //that my code runs with proper security I encapsulate 

35:///all of my code into a delegate which you can see in the first line

/// of try body, which is

  36:///SPSecurity.RunWithElevatedPrivileges(delegate()
  37:                              ///
  38:                              item.Update();
  39:                          }
  40:                      }
  41:                      web.Dispose();
  42:                      site.Dispose();
  43:                  });
  44:   
  45:              }
  46:              catch (Exception ex)
  47:              {
  48:                  Console.WriteLine(ex.ToString());
  49:              }
  50:          }
  51:      }

.

In next post I will do the same thing but insted of using sharepoint object model, I will use Sharepoint web services.

Sorry that I didn't explain things in detail as I am in hurry :)

Remember me in your prayers..



[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags: