Check if Folder is available

Hi,

I have written below code to check if the folder exists in the site.

Through folder is available still it is displaying as 'Folder does not exists'.

Please check if i am making any mistake here

                               

using Sy

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://servername:1268/sites/sitecoll/WikiSite/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    if (web.GetFolder("General").Exists)
                    {
                        Console.WriteLine("Folder exists");
                    }
                    else
                    {
                        Console.WriteLine("Folder does not exists");
                    }
                    Console.ReadLine();
                }
            }
        }


    }
}

Regards,

S

December 11th, 2014 2:51am

Hi,

GetFolder() expects either Path as string parameter. Please refer this link. So please specify the Server relative path in GetFolder().

Another reason could be the URL Passed in SPSite and SPWeb initialization.Please refer to link below:

http://stackoverflow.com/questions/18497772/spfolder-exists-returns-false-when-it-does-exist

Hope it helps!

Thanks,

Avni Bhatt

Free Windows Admin Tool Kit Click here and download it now
December 11th, 2014 4:09am

Hello,

This should work. Are you sure folder name is correct and exist in same web.Also check whether you have permission in list to access that folder. You may also try this code:

private bool FolderExist(SPWeb web, string folderName) 
{
    SPFolder folder = web.GetFolder(folderName);
    return folder.Exists;
}
http://sharepoint.stackexchange.com/questions/19896/how-to-check-if-a-folder-exists-in-a-list

December 11th, 2014 4:59am

Hi,

To check folder exists or not, instead of "General" pass relative url of the folder.

Ex: 

String folderUrl = <currentWeb>.Url + "/" + <List>.RootFolder.Url + "/" + <FolderName>;

                        if (!CheckFolderExists(folderUrl, currentWeb))
                        {
                       //do the code
    
                        }

 //Check folder exists in list
        public static bool CheckFolderExists(String folderURL, SPWeb web)
        {
            bool exists = false;
            try
            {
                if (web.GetFolder(folderURL).Exists)
                {
                    exists = true;
                }
                else
                {
                    exists = false;
                }
            }
            catch (Exception ex)
            {
            
                exists = false;
            }
            return exists;
        }

Thanks,

Vivek 

Free Windows Admin Tool Kit Click here and download it now
December 11th, 2014 5:31am

Hi,

Finally i was able to get with the below code,but i was able to check only under Pages Folder.

But i want to check all the sub folders of Pages folder,how can i proceed for this?

 SPList myList = web.Lists["Pages"];
SPFolder oFolder = myList.RootFolder.SubFolders["foldername"];

But instead of mentioning 

 if (oFolder == null)
                    {
                        Console.WriteLine("FOLDER NOT FOUND");
                    }
                    else
                    {
                        Console.WriteLine("FOLDER FOUND");                      
                        Console.ReadLine();
                    }

December 11th, 2014 5:58am

Please refer to below link

http://www.sharepointsecurity.com/sharepoint/sharepoint-development/iterating-recurring-through-nested-folders-in-c/

Free Windows Admin Tool Kit Click here and download it now
December 11th, 2014 6:05am

Hi,

As per my requirement it has to check for General folder under Pages library folders.

If it is available it has to display the file count of General folder.

For this i have written the code as below

string strfoldername = "General"

 SPFolderCollection subFolders = wikiweb.GetFolder("Pages").SubFolders;

 foreach (SPFolder folder in subFolders)
                            {

Console.WriteLine(folder.ItemCount.ToString(););   

                            }

But it is displaying the count of folders under Pages library,not under the General folder.

Please correct me if i am doing any mistake here.

Regards,

Sudheer

December 11th, 2014 4:29pm

Hi check this piece of code that i found in this link

http://www.c-sharpcorner.com/Blogs/10551/programmatically-check-if-the-folder-exists-in-the-sharepoin.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Collections;
 
namespace CheckIfFolderExists
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://serverName/sites/Vijai/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    if (web.GetFolder("Shared aaaaa").Exists)
                    {
                        Console.WriteLine("Folder exists");
                    }
                    else
                    {
                        Console.WriteLine("Folder does not exists");
                    }
                    Console.ReadLine();
                }
            }
        }
    }
}

Free Windows Admin Tool Kit Click here and download it now
December 11th, 2014 5:08pm

Hi,

Through below code i was able to check if Folder is available

SPList myList = web.Lists["Pages"];
SPFolder oFolder = myList.RootFolder.SubFolders["foldername"];

But instead of mentioning 

 if (oFolder == null)
                    {
                        Console.WriteLine("FOLDER NOT FOUND");
                    }
                    else
                    {
                        Console.WriteLine("FOLDER FOUND");                      
                        Console.ReadLine();
                    }

Regards,

Sudheer

December 13th, 2014 10:15am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics