Sunday, August 21, 2011

SPFolder Collection in SharePoint 2010

This code loop all the folders inside the "Shared Documents" folder.
It will retrieve all the folders inside the Shared documents.I have an requirement to allow the user to upload the documents in to managed folder into Shared Documents and trigger the Workflow.Soon I will post the Workflow code.
public string[] LoopAllFolder()
        {
            string folderName = "";
            SPSite site = SPContext.Current.Site;
            SPFolder cols = site.OpenWeb().Folders[site.Url + "/Shared Documents"];
            if (cols.SubFolders.Count > 0)
                    {
                        //SPFileCollection files = cols.Files;
                        SPFolderCollection folders = cols.SubFolders;
                        foreach (SPFolder Colsfolders in folders)
                        {
                            folderName = folderName + Colsfolders.Name.ToString() + ",";
                        }

                    }
            return folderName.TrimEnd(',').Split(',');
          
                }

                
        }


I bind these folders in dropdownlist by removing the "Forms" folder.
List FolderList = new List(LoopAllFolder());
            FolderList.RemoveAt(0);
            ddlList.DataSource = FolderList;
            ddlList.DataBind();