Interview Questions administrator

Best SharePoint administrator Interview Questions and Answers with topic wise:


Recycle Bin | MMS | ManagedVsCrawledProperties |

Saturday, June 14, 2014

How To: Create nth level of Folders in SharePoint document library programmatically c#


Create nth level of folders creation in SharePoint document library:  

Requirement:  Recently I have got one requirements i.e. create 2 levels of folder creation in the SharePoint document library means create a root folder and sub folder inside it.

Solution: I started development to support 2 levels of folder creation but I thought later on what to do if users may come back with 3 levels of folder creation. For that reason I have developed to support up to nth level folder creation.

e.g.: Folder creation levels
Level 1: /Document library/ Folder/
Level2:  /Document library/ Folder/Sub Folder/
Level3:  /Document library/ Folder/Sub Folder/Sub Folder/
Nth level:  /Document library/ Folder/Sub Folder/Sub Folder/..................../ nth sub folder

Approach: Check whether Folder exists or not
This approach will check for that “Folder exists” condition: if not create a root folder in the document library and nth level of sub folders in it.
Level1: Check for folder exists; if not create a root folder and nth sub folders in it.
Level2: If Root Folder exists; check for sub folder exists if not create a sub folder in root folder and nth sub folders in it.
Level3: If sub folder exists; Check for sub sub folder exists if not create a sub sub folder and nth sub folders in it.
…….
Up to Nth level.

Below code satisfy all of the above conditions:

 
string targetLoc="/DocLibName/FolderName/SubFolderName/"

#region "Method: FolderCreation

        private void CreateFolder(string siteURL, string targetLoc)
        {
            string[] folderCnt = targetLoc.Split('/');
            DOCUMENT_LIBRARY = folderCnt[0];
            string folderURL = string.Empty;
            string docURL = siteURL + "/" + DOCUMENT_LIBRARY;

            try
            {
                using (SPSite site = new SPSite(siteURL))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists.TryGetList(DOCUMENT_LIBRARY);
                        string url = list.RootFolder.ServerRelativeUrl;
                        for (int i = 1; i < folderCnt.Count(); i++)
                        {
                            folderURL = docURL += "/" + folderCnt[i];
       //Check whether folder exists
                            if (!web.GetFolder(folderURL).Exists)
                            {
    //Create nth level sub folders
                                CreateSubFolders(folderCnt, list, url, i);
                            }
                            else
                            {
                                url += "/" + folderCnt[i];
                            }

                            _foldersPath += folderCnt[i] + "/";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogExceptions(ex.InnerException.ToString());
            }
        }

        private void CreateSubFolders(string[] folderCnt, SPList list, string folderURL, int j)
        {
            SPListItem newFolder = null;
            string url = folderURL;
            for (int i = j; i < folderCnt.Count(); i++)
            {
                newFolder = list.AddItem(url, SPFileSystemObjectType.Folder, folderCnt[i]);
                newFolder.Update();
                url += "/" + folderCnt[i];
            }
        }

        #endregion "Method: CreateFolders


Please let me if faced any issues while using the above my approach and kindly provide me yours valuable suggestions and feedbacks.

3 comments:

  1. This is a great post, thank you for sharing! would you mind providing dev software requirements as well? also what should I add as a reference? I'm getting errors on SPList
    thanks!

    ReplyDelete
  2. I am starter C# on SharePoit 2010/2013

    How should I start the code?

    Should I include?

    using.microsoft.sharepoint.client
    What class should I use?

    Can you give an example?
    Thanks
    Peter

    ReplyDelete
  3. I am starter C# on SharePoit 2010/2013

    How should I start the code?

    Should I include?

    using.microsoft.sharepoint.client
    What class should I use?

    Can you give an example?
    Thanks
    Peter

    ReplyDelete