Shahid Riaz Bhatti

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

Insert Data in Sharepoint List using Sharepoint object model

January 24
by Shahid Riaz Bhatti 24. January 2010 13:36

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..

Be the first to rate this post

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

Tags:

WSS | MOSS

Comments

Add comment


(Will show your Gravatar icon)  

Enter the word
1332


biuquote
  • Comment
  • Preview
Loading



RecentComments

Comment RSS

Most comments

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