Determine if a file can be uploaded to a list in SharePoint 2010

711 views Asked by At

I am trying to write a method that uploads an xml file to a list in SharePoint 2010. For this I have another method that populates a DropDownList control with lists with the BaseType DocumentLibrary. The DDL populates fine except that it includes libraries that I cannot upload to, for example "Customized Reports" is one of them.

So I basically need to refine the filter so it only shows me "standard" document libraries.

Here is my code where I populate the DropDownList:

protected void initExportControlsForUploadingtoList()
{
    //Check if there are lists on the current website.
    if (SPContext.Current.Web.Lists != null && SPContext.Current.Web.Lists.Count > 0)
    {
        //Get the lists.
        SPListCollection currentsitelists = SPContext.Current.Web.Lists;

        //Reset the DropDownList holding the lists.
        ddlExportSelectList.Items.Clear();
        ddlExportSelectList.Items.Add("");

        //Populate the DDL with the lists' names.
        foreach (SPList list in currentsitelists)
        {
            if (!list.Hidden && list.BaseType == SPBaseType.DocumentLibrary)
            {
                ddlExportSelectList.Items.Add(list.Title);
            }
        }

        //Select the first item in the DDL, which is "empty".
        ddlExportSelectList.SelectedIndex = 0;
    }
    else
    {
        lblStatus.Text = "No lists found.";
    }
}

EDIT:

Error details:

System.ArgumentException "Value does not fall within the expected range."

at Microsoft.SharePoint.SPFolderCollection.get_Item(String urlOfFolder) at SiteCollectionTreeView.VisualWebPart1.MigrationTool.btnExportOK_Click(Object sender, EventArgs e)

The error happens in this block of code (part of btnExportOK_Click(Object sender, EventArgs e)):

try
{
    //Get the folder with the corresponding list name as value from DropDownList.
    SPFolder librarytouploadto = SPContext.Current.Web.Folders[ddlExportSelectList.SelectedValue];
}
catch (Exception ex)
{
    lblStatus.Text = "Problem uploading to that list. Try another one or create a new one.";
    return;
}

EDIT:

OK if I change the try block to:

try
{
    //Get the folder with the corresponding list name as value from DropDownList.
    string url = SPContext.Current.Web.Lists[ddlExportSelectList.SelectedValue].DefaultDisplayFormUrl;
    librarytouploadto = SPContext.Current.Web.GetList(url).RootFolder;
}
catch (Exception ex)
{
    lblStatus.Text = "Problem uploading to that list. Try another one or create a new one.";
    return;      
}

It works. But now I am not sure if I should be doing this. For example, is "Customized Reports" a list to which the user should be able to upload random xml files to? Or doesn't it really matter as long as the user is comfortable with it and knows where his/her files are?

0

There are 0 answers