What’s the best way to create a file under a nested folder in Sharepoint ?
My current method
public string CreateSPFile(string spServerURL, string spDocumentLibraryURL, string folder, string fileName, Stream fileStream, bool overwrite)
{
if (SPSite.Exists(new Uri(spServerURL)))
{
SPSite site = new SPSite(spServerURL);
SPWeb oWebsite = site.OpenWeb();
oWebsite.AllowUnsafeUpdates = true;
SPFolder spFolder = oWebsite.Folders[spDocumentLibraryURL];
if (!string.IsNullOrEmpty(folder))
{
spFolder.SubFolders[folder].Files.Add(fileName, fileStream, overwrite);
}
else
{
SPFileCollection files = spFolder.Files;
spFolder.Files.Add(fileName, fileStream, overwrite);
}
oWebsite.AllowUnsafeUpdates = false;
site.Close();
}
}
As you can see, if I want to create a file under nested folder, i need to modified my codes.
What will be better way to handle this kind of saving nested folder situation?
According to my project structure, the file can be like /DocumentLibrary/Folder1/Folder2/Folder3/File.txt.
You can load a folder by its server relative URL:
With this approach you do not have to load folder by folder and your code works with n folder levels.
I’ve updated your code sample and added some comments regarding SharePoint best practices: