locked
reading sharepoint list using C#

    Question

  •  hi all,

    Is there anyway we can read a list from Sharepoint which is of type calendar using C#


    Basically, what I need to do is read the data that has been entered in sharepoint Calendar.

    Thanks,

    Aditya

     
    Aditya
    • Moved by nobugz Wednesday, September 3, 2008 1:27 AM SP q (Moved from Visual C# General to SharePoint - Development and Programming)
    Wednesday, September 3, 2008 1:04 AM

Answers

  • Reading a list from SharePoint using the SDK is rather straight forward.  The catch, if you are not familiar with working with the SDK, is you have to run the code on the machine that has your SharePoint site.  If you need to do this from another machine, you need to use the SharePoint web services (which we can help you with as well).

    Here is a quick sample of code to access the list via the SDK:

    using Microsoft.SharePoint;  
     
     
    class SPTest {  
     
      public void ReadList() {  
     
      // Use using to make sure resources are released properly  
      using(SPSite oSite = new SPSite(pathToSite)) {  
        using(SPWeb oWeb = oSite.AllWebs[nameOfWeb]) {   
          // Alternately you can use oSite.RootWeb if you want to access the main site  
     
          SPList oList = oWeb.Lists[listName];  // The display name, ie. "Calendar"  
     
          foreach(SPListItem oItem in oList.Items) {  
            // Access each item in the list...  
            DateTime startTime = (DateTime)oItem["Start Time"];  
            // etc....  
          }  
     
        }  
      }  
     
     }  

    Hope this helps...

    Tony Champion
    Wednesday, September 3, 2008 2:01 AM
  • You need to specify the viewfields, query and queryoptions.. (maybe others).

    See the sample here:
    http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems.aspx
    Posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, September 5, 2008 4:45 PM

All replies

  • Reading a list from SharePoint using the SDK is rather straight forward.  The catch, if you are not familiar with working with the SDK, is you have to run the code on the machine that has your SharePoint site.  If you need to do this from another machine, you need to use the SharePoint web services (which we can help you with as well).

    Here is a quick sample of code to access the list via the SDK:

    using Microsoft.SharePoint;  
     
     
    class SPTest {  
     
      public void ReadList() {  
     
      // Use using to make sure resources are released properly  
      using(SPSite oSite = new SPSite(pathToSite)) {  
        using(SPWeb oWeb = oSite.AllWebs[nameOfWeb]) {   
          // Alternately you can use oSite.RootWeb if you want to access the main site  
     
          SPList oList = oWeb.Lists[listName];  // The display name, ie. "Calendar"  
     
          foreach(SPListItem oItem in oList.Items) {  
            // Access each item in the list...  
            DateTime startTime = (DateTime)oItem["Start Time"];  
            // etc....  
          }  
     
        }  
      }  
     
     }  

    Hope this helps...

    Tony Champion
    Wednesday, September 3, 2008 2:01 AM
  • You can also read SharePoint list items based on the Views of Calendar. I've seen in most of scenarios people read calendar list items based on the its views. Basically you need to leverage GetItems method of SPList class and pass the SPView as the parameter


    SPSite oSiteCollection = SPContext.Current.Site;
    SPList oList = oSiteCollection.AllWebs["Site_Name"].Lists["List_Name"];
    SPView oListView = oList.Views["View_Name"];

    SPListItemCollection collListItems = oList.GetItems(oListView);

    foreach (SPListItem oListItem in colListItems)
    {
        Label1.Text +=
            SPEncode.HtmlEncode(oListItem["Field1_Name"].ToString())
            + " -- " +
            SPEncode.HtmlEncode(oListItem["Field2_Name"].ToString())
            + "<BR>";
    }


    Sundar Narasiman
    Wednesday, September 3, 2008 6:14 AM
  • Hi Tony,

    No i am building a remote application and will not be residing on the machine where sharepoint is installed. So i guess i cannot make use of SPSite class. But I have the AbsoluteURL of the share point calendar.


    Thanks,


    Aditya
    Wednesday, September 3, 2008 6:16 PM
  • You will want to use the Lists.asmx web service.

    Here are some samples.. The calendar entries are list items and the calendar itself is just a list.
    http://www.codeproject.com/KB/sharepoint/SharePointListWebService.aspx


    Posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, September 3, 2008 6:48 PM
  • Hi,

    I have added the following piece of code

    my.Lists listService = new my.Lists();

    listService.Url = "http://my/sites/aditya/_vti_bin/lists.asmx?wsdl";

    listService.Credentials= System.Net.CredentialCache.DefaultCredentials;

    XmlNode ndListItems =

    listService.GetListItems("FriendsList", "{22D175CC-2EE8-4DE1-9056-DC6FE5E96F9D}",null,null,"",null,"");

    But am getting a NullReferenceException on execution of the above code

    Thanks,


    Aditya
    Wednesday, September 3, 2008 7:17 PM
  • You need to specify the viewfields, query and queryoptions.. (maybe others).

    See the sample here:
    http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems.aspx
    Posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, September 5, 2008 4:45 PM
  • Hello

     Here is function that I made just for the purpose of reading Lists from sharepoint :)

           public List<string> GetList(string listName, string fieldName)
           {  
            List<string> listOfTitle = new List<string>();
            string title = string.Empty;
            SPList oList = SPContext.Current.Web.Lists[listName];
            foreach (SPListItem oItem in oList.Items)
            {
               title = (
    string)oItem[fieldName];
               listOfTitle.Add(title);
             }
            return listOfTitle;
           }

    Cheers


    Momo
    • Proposed as answer by Mohamed Hachem Monday, September 14, 2009 7:28 PM
    • Unproposed as answer by Mike Walsh FIN Friday, January 28, 2011 2:33 PM
    Monday, September 14, 2009 7:28 PM
  •      SPSite site = new SPSite ("insert your site here, eg: http://mysite:5050" );
         SPWeb web = site.OpenWeb();

         // choose the list
         SPList list = web.Lists["insert your list Name" ];

         SPQuery myquery = new SPQuery ();
         myquery.Query = "insert your query here" ;

         // if you dosent insert query ( myquery.Query ="" ) you will get all items

         SPListItemCollection items = list.GetItems(myquery);

    for more info please refer

    SharePoint List C# Part 1

    Monday, November 30, 2009 4:07 AM