Monday, April 19, 2010

Creating List/Library Folders Programmatically

Very useful link to create folders programmatically:

I am calling createCT method just after the folder has created:



public static SPFolder GetFolder(SPList targetList, string folderUrl)


{


if (string.IsNullOrEmpty(folderUrl))


return targetList.RootFolder;


SPFolder folder = targetList.ParentWeb.GetFolder(targetList.RootFolder.Url + "/" + folderUrl);


if (!folder.Exists)


{


if (!targetList.EnableFolderCreation)


{


targetList.EnableFolderCreation = true;


targetList.Update();


}


// We couldn't find the folder so create it


string[] folders = folderUrl.Trim('/').Split('/');


string folderPath = string.Empty;


for (int i = 0; i < folders.Length; i++)


{


folderPath += "/" + folders[i];


folder = targetList.ParentWeb.GetFolder(targetList.RootFolder.Url + folderPath);


if (!folder.Exists)


{


SPListItem newFolder = targetList.Items.Add("", SPFileSystemObjectType.Folder, folderPath.Trim('/'));


newFolder.Update();


folder = newFolder.Folder;


}


}


createCT(folderUrl, folder, targetList);


}


// Still no folder so error out


if (folder == null)


throw new SPException(string.Format("The folder '{0}' could not be found.", folderUrl));


return folder;


}

No comments:

Post a Comment