.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Populating file names from SharePoint Document library in a drop down list

Posted By:Dhananjay Kumar       Posted Date: December 17, 2009    Points: 25    Category: SharePoint    URL: http://www.dotnetspark.com  
 

Populating file names from SharePoint Document library in a drop down list

Objective

This article will show how to iterate through SharePoint document library and list out all the file names and bind the list to a drop down list.

Working Screen

When user will click on button name of the files will get loaded from document library to drop down list. 

1.gif

Step 1

Create a window application. Drag and drop one Button and DropDown list.

Step 2

Add reference of Windows SharePoint Services

2.gif
 
Step 3

On click event of button write the below code


private
void button1_Click(object sender, EventArgs e)

{

    listName = new List<string>();

    using (SPSite site = new SPSite("http://adfsaccount:2222/"))

    {

        using (SPWeb web = site.OpenWeb())

        {

            SPList list = web.Lists["My Documents"];

            SPListItemCollection items = list.Items;

            foreach (SPListItem item in items)

            {

                //MessageBox.Show(item.Name);

                listName.Add(item.Name);

            }

        }

    } 

    comboBox1.DataSource = listName;
}

Explanation

  1. Returning SharePoint site using SPSite.

  2. Returning current web using SPWeb.

  3. My Documents is name of the document library.

  4. We are iterating through the document library and fetching all the file names and adding them to a string list.

Full Code

using
System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using Microsoft.SharePoint;

namespace WindowsFormsApplication2

{

    public partial class Form1 : Form

    {

        List<string> listName = null;

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            listName = new List<string>();

            using (SPSite site = new SPSite("http://adfsaccount:2222/"))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    SPList list = web.Lists["My Documents"];

                    SPListItemCollection items = list.Items;

                    foreach (SPListItem item in items)

                    {

                         listName.Add(item.Name);

                    }

                }

            } 

            comboBox1.DataSource = listName;

        }

    }
}

Output 

3.gif 

Conclusion

I have shown in this article, How to iterate through the Document Library. Thanks for reading.

 


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend