I'm attempting to programmatically download all files from a document folder on a Sharepoint 2007 site. So far, I'm able to connect to the site, but am having issues connecting to the folders and download them.
try{
using(SPSite site = new SPSite("http://mysharepointserver/sites/subsite")){
using(SPWeb web = site.OpenWeb()){
Console.Write("Connected to site");
SPFolder testFolder = web.Folder["testFolder"];
//example method downloading folder
downloadFolder(testFolder);
}
}
}
catch(Exception e){
Log(e.ToString());
}
My console write works,so I know I am connecting to the site correctly. My log file outputs:
System.ArgumentException: Value does not fall within the expected range.
at Microsoft.SharePoint.SPListCollection.GetListByName(String strListName, Boolean bThrowException)
at Microsoft.SharePoint.SPListCollection.get_Item(String strListName)
I also attempted to print out the following:
using(SPWeb web = site.OpenWeb()){
Console.Write("Connected to site");
Console.Write(web.lists);
SPFolder testFolder = web.Folder["testFolder"];
//example method downloading folder
downloadFolder(testFolder);
}
Which outputs the following to console:
Connected to site
Microsoft.SharePoint.SPListCollection
But I'm not certain how to navigate through SPListCollection to retrieve my folder "testFolder"
Any help would be appreciated. Thanks!
When you connect to share point site, there are different types of libraries. Library that contains documents and folders is DocumentLibrary and not ListLibrary. Once you have the item / library by ID, cast it to correct SPDocumentLibrary to retrieve items you want.
Use https://learn.microsoft.com/en-us/dotnet/api/microsoft.sharepoint.spdocumentlibrary?view=sharepoint-server to get different methods and properties of DocumentLibrary to retrieve the testFolder.
Example of accessing document library item from :https://social.msdn.microsoft.com/Forums/en-US/5ee7fb55-5d90-4d28-8990-bf00479f891f/how-to-get-spdocumentlibrary?forum=sharepointdevelopmentprevious
SPSite siteCollection = this.Site;
SPWeb site = this.Web;
// obtain query string values
string ListId = Request.QueryString["ListId"];
string ItemId = Request.QueryString["ItemId"];
// create list object and list item object
SPList list = site.Lists[new Guid(ListId)];
SPListItem item = list.Items.GetItemById(Convert.ToInt32(ItemId));
// query for information about list and list item
string ListTitle = list.Title;
string ItemTitle = item.Title;
if (list is SPDocumentLibrary) {
SPDocumentLibrary documentLibrary = (SPDocumentLibrary)list;
string DocumentTemplateUrl = documentLibrary.DocumentTemplateUrl;
SPFile file = item.File;
string FileAuthor = file.Author.Name;
string FileSize = file.TotalLength.ToString("0,###") + " bits";
}