Sharepoint Forum

 
Home » Forum » Sharepoint       Ask a questionRSS Feeds

Dropdown of a library in a Web Part

  Asked By: Roxana Middleton         Date: Jul 24, 2008      Category: Sharepoint      Views: 259
 

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
Library.

Tagged:            

 

4 Answers Found

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

This APress article seems like it should provide you with the start you are
looking for:
http://tinyurl.com/5tkd5s

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

Here is a simpler version too:
http://tinyurl.com/5dug22

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

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++)
{
ddlReports.Items.Add(ReportLibrary.Items[i].DisplayName.ToString());
}
this.Controls.Add(ddlReports);

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

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
cache.

So, this code:
for (int i = 0; i < items; i++)
{
ddlReports.Items.Add(ReportLibrary.Items[i].DisplayName.ToString());
}

would look like this:
foreach (SPListItem item in ReportLibrary.Items)
{
ddlReports.Items.Add(item.DisplayName.ToString());
}

I typically even go a step further:
SPListItemCollection items = ReportLibrary.Items;
foreach (SPListItem item in items)
{
ddlReports.Items.Add(item.DisplayName.ToString());
}

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.


Your Answer
  • Answer should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].