Populating name of files from SharePoint Document library in a drop down list using C#

No.of Views2016
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:56
Tag : SharePoint , Development and Programming
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.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

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.
 

Image Loading

Step 1

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

Step 2

Add reference of Windows SharePoint Services 

Image Loading

Step 3

On click event of button write the below code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes; 
namespace ElementBindingDemo
{public partial class MainPage : UserControl
    {public MainPage()
        {
            InitializeComponent();
            topSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(topSlider_ValueChanged);
        }void topSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {//throw new NotImplementedException();'topTextBlock.Text = topSlider.Value.ToString(); 
        }
    }
}

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.


Complete 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;
        }
    }
}

Result Of Above Code

Image Loading

Conclusion

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

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top