Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Sharepoint List using c#

How to get fields values from a particular list item.In my case i want to get all form fileds of Workplan list.Actually i want to get Workplan all list item and insert to sharepoint 2013 associated database.

example

I try the following code.

string strUrl = "http://example.com/default.aspx";
using (SPSite site = new SPSite(strUrl))
{
    using (SPWeb web = site.OpenWeb())
    {                 

        SPList list = web.Lists[52];
        SPQuery myquery = new SPQuery();
        myquery.Query = "";        
        SPListItemCollection items = list.GetItems(myquery);                

        foreach (SPListItem item in items)
        {
            if (item != null)
            {
                var Name = item.ListItems.Fields.List;
                Console.WriteLine("Name is :" + Name);
            }
        }  
    }
}
like image 209
moss Avatar asked Sep 08 '14 06:09

moss


2 Answers

This is the easiest way I can think of using Server Object Model:

string strUrl = "http://example.com";                
using(SPSite oSite = new SPSite(strUrl))
{        
   using(SPWeb oWeb = oSite.OpenWeb())
   {
      SPList list = oWeb.Lists["Workplan"];

       foreach(SPField field in list.Fields)
       {
           Console.WriteLine(field.Title);
       }
    }            
}

Btw, as for your site-URL "http://example.com/default.aspx" it is enough to do it like "http://example.com".

For more information on Sharepoint I recommend using this site in the future.

like image 125
チーズパン Avatar answered Nov 03 '22 01:11

チーズパン


using (SPSite site = new SPSite("URL")
     {
        using (SPWeb web = site.OpenWeb("sitecollection/subsite"))
        {
         //to get specific list type
           string listUrl = "/sites/sitecollection/subsite/Lists/Announcements";
           SPList list = web.GetList(listUrl);
           Console.WriteLine("List URL: {0}", list.RootFolder.ServerRelativeUrl);
        }
     }

//To get all lists from spweb use this:

SPSite oSiteCollection = SPContext.Current.Site;
using(SPWebCollection collWebs = oSiteCollection.AllWebs)
{
foreach (SPWeb oWebsite in collWebs)
{
    SPListCollection collSiteLists = oWebsite.Lists;
    foreach (SPList oList in collSiteLists)
    {
        //get your each list here
    }
    oWebsite.Dispose();
}
}
like image 36
StartCoding Avatar answered Nov 03 '22 01:11

StartCoding