Wednesday 16 January 2013

Sharepoint Folder creation programmatically

I am showing you how we can create folder programmatically.

namespace FolderCreation
{
    class Program
    {
        static void Main(string[] args)
        {
         //   SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPSite site = new SPSite("URL");
                site.AllowUnsafeUpdates = true;
                SPWeb web = site.OpenWeb();
                web.AllowUnsafeUpdates = true;
                SPList list = web.Lists.TryGetList("YOUR-LIST");
                SPListItem folderColl = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder);
                folderColl["Title"] = "NewFolder";
                folderColl.Update();
                list.Update();
                web.AllowUnsafeUpdates = false;
                site.AllowUnsafeUpdates = false;
            }
        }
    }
}

Please add Microsoft.Sharepoint namespace.
 

No comments:

Post a Comment