Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Dropdown of a library in a Web Part

  Asked By: Roxana    Date: Jul 24    Category: Sharepoint    Views: 1280

I need some quick development help. I need to create a Dropdown List on
a custom Web Part that contains a list of files in a SharePoint Library.
The Web Part has a report viewer and I want the users to be able to
chose the report they want from a dropdown list that points to a Report



4 Answers Found

Answer #1    Answered By: Donnie Drake     Answered On: Jul 24

This APress article seems like it should provide you with the start you are
looking for:

Answer #2    Answered By: Nathaniel Henderson     Answered On: Jul 24

Here is a simpler version too:

Answer #3    Answered By: Tanner Moss     Answered On: Jul 24

I figured it out. It turned out to be really simple. Code below
for anyone interested.

SPSite siteCollection = SPContext.Current.Site;
SPList ReportLibrary =
siteCollection.AllWebs["/ReportCenter"].Lists["My Reports"];
int items = ReportLibrary.Items.Count;
DropDownList ddlReports = new DropDownList();
ddlReports.ID = "ddlReports";

for (int i = 0; i < items; i++)

Answer #4    Answered By: Bobby Boyd     Answered On: Jul 24

I highly recommend that you use foreach rather a for index when iterating
collections in SharePoint. In a for index loop, every request results in a call
to the database to retrieve that one listitem, whereas in a foreach loop, the
first request causes the API to retrieve the listitems into memory with a single
database call and each iteration simply retrieves the next listitem from that

So, this code:
for (int i = 0; i < items; i++)

would look like this:
foreach (SPListItem item in ReportLibrary.Items)

I typically even go a step further:
SPListItemCollection items = ReportLibrary.Items;
foreach (SPListItem item in items)

IMO, foreach is also more readable.

Finally, be aware that if anyone changes the display name for the "My Reports"
library, the code will break. It is better to either: 1) iterate the collection
of lists to find the one you are looking for with a break when it is encountered
OR 2) use a try catch block (bad WSS API, I know) to retrieve the list  or exit
the code block.

Didn't find what you were looking for? Find more on Dropdown of a library in a Web Part Or get search suggestion and latest updates.