SharePoint 2007: Accessing a SharePoint List programmatically

17 December, 2010 | | 0 comments |

Share |
Microsoft SharePoint 2007 offers the best portal, collaboration and content management for an enterprise. Teams using a SharePoint portal can collaborate using Document libraries, lists, picture libraries, discussion boards, surveys, etc. Amongst those I mentioned, document libraries are the most used allowing a team to share documents(of Office 2003/07) and stay synchronized with the latest one allowing real-time collaboration. These lists and libraries can be accessed programmatically in C# using the SharePoint Object Model(SOM). Though SOM can also be used to access different entities like Workflows, Site Features, etc; in this post I'll be focusing on accessing lists/libraries using SOM.


Following will give you a quick gist of how we can access a SharePoint list present in a SharePoint site based on the hierarchy.


Hierarchy Class

• SharePoint Site Collection
(i.e. top-level or parent site)

SPSite

• SharePoint Site
(i.e. target or sub-site)

SPWeb

• SharePoint List under the target site

SPList

• List Items in the List

SPListItem


In my following example, I’m trying to access images in a Picture Library named ‘PictureLibrary1’ present at the URL http://ram2003svr:8080/PictureLibrary1. The List Items existing in the ‘PictureLIbrary1’ are ‘Name’, ‘Title’. I have used an ASP.Net website for this example, so make sure you have the following three libraries added as a reference to your project.


• Microsoft.SharePoint
• Microsoft.SharePoint.Security
• Microsoft.SharePoint.intl

You can copy the DLL’s from your GAC using this link Here.


CODE Snippet:


  • using System;
  • using System.Web;
  • using System.Web.UI;
  • using System.Web.UI.HtmlControls;
  • using System.Web.UI.WebControls;
  •  
  • using Microsoft.SharePoint;
  • using Microsoft.SharePoint.WebControls;
  • using Microsoft.SharePoint.WebPartPages;
  •  
  • public partial class Image_Library_Webpart_Default : System.Web.UI.Page
  • {
  •     
  •     /*
  •         We're trying to access a 'Picture Library' located at below URL.
  •         URL: http://ram2003svr:8080/PictureLibrary1
  •       
  •         The below process is similar for accessing a Document Library or Lists like 'Tasks','Announcements', etc.
  •     */
  •     
  •     protected void Page_Load(object sender, EventArgs e)
  •     {
  •  
  •         //SPSecurity.RunWithElevatedPrivileges() is mandatory since a user might not have sufficient privileges to access this list.
  •  
  •         SPSecurity.RunWithElevatedPrivileges(delegate()
  •         {
  •  
  •             //SPSite returns the Site Collection object for the URL
  •  
  •             using (SPSite spSite = new SPSite("http://ram2003svr:8080/"))    
  •             {
  •                 /* Returns the object of the site (takes relative site URL after the top-site) which we want to access.
  •                 In this case, it is BLANK since the site is a top-level site */
  •  
  •                 using (SPWeb spWeb = spSite.AllWebs[""])
  •                 {   
  •                     //Returns the List object for the Picture Library. Takes the List Name as an argument.
  •  
  •                     SPList spList = spWeb.Lists["PictureLibrary1"];
  •  
  •                     //Iterate through the SPListItems in the List.
  •  
  •                     foreach (SPListItem spListItem in spList.Items)
  •                     {
  •                         
  •                         //You can access the various attributes in the list like 'Name','Title'
  •  
  •                         Response.Write("Name of image file : " + spListItem["Name"].ToString() + "<br /><br />");
  •  
  •                         Response.Write("Title of image file : " + spListItem["Title"].ToString() + "<br /><br />");
  •  
  •                         Response.Write("URL of image file : " + "http://ram2003svr:8080/PictureLibrary1/" + spListItem["Name"].ToString() + "<br /><br />");
  •                         
  •                     }
  •  
  •                 }
  •             }
  •  
  •         });
  •  
  •     }
  • }






You can download the source code Here:





Source code for accessing a SharePoint 2007 List programmatically






0 comments: