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: | ![]() |


![Hardik Shah [Guru] on Facebook](http://2.bp.blogspot.com/_GTY2pIbrTVE/S9bFa41-64I/AAAAAAAAAN0/zSEOohhBod4/s400/Follow+Hardik+Shah%5BGuru%5D+on+Facebook.png)

![Follow Hardik Shah [Guru] on Windows Live](http://2.bp.blogspot.com/_GTY2pIbrTVE/S9pZPlwD84I/AAAAAAAAAOM/gbGumaiQkuU/s400/Follow+Hardik+Shah%5BGuru%5D+on+Windows+Live.jpg)
![Follow Hardik Shah [Guru] on Twitter](http://1.bp.blogspot.com/_GTY2pIbrTVE/S9pZPdKDbaI/AAAAAAAAAOE/6YIM-CZqdHc/s400/Follow+Hardik+Shah%5BGuru%5D+on+twitter.jpg)
![Follow Hardik Shah [Guru] on Orkut](http://4.bp.blogspot.com/_GTY2pIbrTVE/S9bAfs9JQAI/AAAAAAAAANU/ZuzHM2AwaLM/s400/Follow+Hardik+Shah%5BGuru%5D+on+Orkut.jpg)
![Follow Hardik Shah [Guru] on LinkedIn](http://4.bp.blogspot.com/_GTY2pIbrTVE/S9pZPJmXebI/AAAAAAAAAN8/30vvx38ETdc/s400/Follow+Hardik+Shah%5BGuru%5D+on+LinkedIn.jpg)
0 comments:
Post a Comment