Wednesday, April 28, 2010

Error: SharePoint folders that are created programatically are hidden.

I created few folders programatically from a SharePoint Workflow.

They have been created with no exceptions but I was unable to see them from the UI. Assuming that the folders are not created, I tried to created them through the UI. Then I got an error message "Folder already exists".

The above behaviour was with Custom Lists whereas these folders show up quite well in Document Libraries.

So I did a bit of research and learnt some thing about SharePoint folders.

There are few ways to create folders.

One of them is to use
SPWeb.Folders.Add(folderUrl);
The above problem was as a result of using this code.

Hers is another way to create folders in a list which works with both custom lists and document libraries (I have not tested the rest yet)
/// 
        /// Check whether the folder path exists. If it does not, build the folder structure
        /// 
        /// 
/// 
/// 
/// 
        private static String EnsureFolderStructure(SPWeb web, SPList list, String folderPath)
        {
            String folderUrl = String.Empty;
            try
            {
                if (!DoesFolderExist(web, folderPath))
                {
                    String partialFolderUrl = String.Empty;
                    String[] folderNames = folderPath.Split('/');
                    int ctr = 0;
                    SPListItem folder = null;
                    foreach (String folderName in folderNames)
                    {
                        partialFolderUrl += String.Format("/{0}", folderName);
                        folderUrl = String.Format("{0}/{1}{2}", list.ParentWeb.Url, list.RootFolder.Url, partialFolderUrl);
                        if (!DoesFolderExist(web, folderUrl))
                        {
                            //Folder does not exist. Create it
                            if (ctr == 0)
                                //Important: Leave the folderUrl blank if this folder is create in the root list.
                                folder = list.Items.Add("", SPFileSystemObjectType.Folder, folderName);
                            else
                                folder = list.Items.Add(folderUrl.Replace("/" + folderName, ""), SPFileSystemObjectType.Folder, folderName);
                            folder["Title"] = folderName;
                            folder.Update();

                            list.Update(true);
                            web.Update();
                        }
                        ctr++;
                    }
                }
                else
                {
                    folderUrl = String.Format("{0}/{1}/{2}", list.ParentWeb.Url, list.RootFolder.Url, folderPath);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return folderUrl;
        }
        /// 
        /// Check whether a folder exists
        /// 
        /// 
/// 
/// 
        public static Boolean DoesFolderExist(SPWeb web, String folderUrl)
        {
            try
            {
                return web.GetFolder(folderUrl).Exists;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

4 comments: