By David Fekke
January 21st, 2011
I am working on project now where we are creating reports and saving them to a document library in Sharepoint 3. I am automating this by using .NET code to create the subdirectories and files in the document library. I have not seen a lot of good code examples on how to do this on the internet, but this is how I am doing it in C#.
When I add a folder to the document library I used the following code;
SPSite mySite = new SPSite("http://sitename/");
SPWeb myWeb = mySite.AllWebs["mywebname"];
SPFolderCollection myFolderCollection = myWeb.GetFolder("http://sitename/sites/mywebname/PDF Reports").SubFolders;
myFolderCollection.Add("FolderName");
To add a file from a filestream, I used the following code;
FileStream stream = File.Create("report.pdf", result.Length);
SPSite mySite = new SPSite("http://sitename/");
SPWeb myWeb = mySite.AllWebs["mywebname"];
SPFileCollection destFiles = myWeb.GetFolder("http://sitename/sites/mywebname/PDF Reports/FolderName").Files;
destFiles.Add("report.pdf", stream, true);
stream.Close();