I have a SharePoint list (not a library) that accept the creation of folders.
I have written a small utility method that allow me to create a folder hierarchy if it does not yet exists :
public static SPFolder EnsureFolder(SPList list, string subFolderPath)
{
var folderPaths = subFolderPath.Split('/');
var currentFolder = list.RootFolder;
for (int i = 0; i < folderPaths.Length; i++)
{
var subFolder = currentFolder.SubFolders.Cast<SPFolder>().FirstOrDefault(f => f.Name == folderPaths[i]);
if (subFolder == null)
{
var newFolderItem = list.Items.Add(currentFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, folderPaths[i]);
newFolderItem.Update();
currentFolder.Update();
subFolder = newFolderItem.Folder;
list.Update();
}
currentFolder = subFolder;
}
return currentFolder;
}
This code is working the first time but if I made two successive calls, it fails :
var myList = web.Lists["some list"];
EnsureFolder(myList, "folder");
EnsureFolder(myList, "folder"); // same as 1st call
The second calls fails on the line :
newFolderItem.Update();
with this error :
Un fichier ou un dossier nommé http://server/sites/coll/wen/Lists/somelist/folder existe déjà. at Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx)
at Microsoft.SharePoint.Library.SPRequest.AddOrUpdateItem(String bstrUrl, String bstrListName, Boolean bAdd, Boolean bSystemUpdate, Boolean bPreserveItemVersion, Boolean bPreserveItemUIVersion, Boolean bUpdateNoVersion, Int32& plID, String& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bstrVersion, Object& pvarAttachmentNames, Object& pvarAttachmentContents, Object& pvarProperties, Boolean bCheckOut, Boolean bCheckin, Boolean bMigration, Boolean bPublish, String bstrFileName, ISP2DSafeArrayWriter pListDataValidationCallback, ISP2DSafeArrayWriter pRestrictInsertCallback, ISP2DSafeArrayWriter pUniqueFieldCallback)
at Microsoft.SharePoint.SPListItem.AddOrUpdateItem(Boolean bAdd, Boolean bSystem, Boolean bPreserveItemVersion, Boolean bNoVersion, Boolean bMigration, Boolean bPublish, Boolean bCheckOut, Boolean bCheckin, Guid newGuidOnAdd, Int32& ulID, Object& objAttachmentNames, Object& objAttachmentContents, Boolean suppressAfterEvents, String filename, Boolean bPreserveItemUIVersion)
at Microsoft.SharePoint.SPListItem.UpdateInternal(Boolean bSystem, Boolean bPreserveItemVersion, Guid newGuidOnAdd, Boolean bMigration, Boolean bPublish, Boolean bNoVersion, Boolean bCheckOut, Boolean bCheckin, Boolean suppressAfterEvents, String filename, Boolean bPreserveItemUIVersion)
at Microsoft.SharePoint.SPListItem.Update()
at somewhere in my project
This can be translated as "A folder XXX already exists".
When debugging, I can see that var subFolder = currentFolder.SubFolders.Cast<SPFolder>().FirstOrDefault(f => f.Name == folderPaths[i]);
will always return null. It looks like the SubFolders property is not up to date.
What have I to do to correct my code ? Am I calling the correct sequence of .Update()
calls?