How to create a folder in SharePoint 2010 Library Programmatically
public void CreateFolder(string
LibraryName, string folderName)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using
(SPSite osite = new
SPSite(SPContext.Current.Site.Url))
{
using
(SPWeb oweb = osite.OpenWeb())
{
SPDocumentLibrary MyDocLibrary = (SPDocumentLibrary)oweb.Lists[LibraryName];
SPFolderCollection MyFolders = oweb.Folders;
SPFolder folder
=oweb.GetFolder(MyDocLibrary.RootFolder.ServerRelativeUrl + "/" + folderName);
if (folder.Exists)
{
lblError.Text = "Folder exists";
}
else
{
MyFolders.Add(MyDocLibrary.RootFolder.ServerRelativeUrl + "/" + folderName + "/");
oweb.AllowUnsafeUpdates = true;
MyDocLibrary.Update();
oweb.AllowUnsafeUpdates = false;
SPFieldUrlValue ofldval = new
SPFieldUrlValue();
ofldval.Url =
MyDocLibrary.RootFolder.ServerRelativeUrl + "/"
+ folderName + "/";
lblError.Text = "Folder created : Url is : " +
ofldval.Url;
}
}
}
});
}
Comments
Post a Comment