SPWeb.GetFolder Unable to Pass String Value In

82 views Asked by At

I am unable to pass a string value into my SPWeb.GetFolder despite my input being a string value.

private static void UploadEmlToSp(string sharePointSite, string sharePointDocLib, string emlFullPath, string requestNo)
{
    using (SPSite oSite = new SPSite(sharePointSite))
    {
        using (SPWeb oWeb = oSite.OpenWeb())
        {
            if (!System.IO.File.Exists(emlFullPath))
                throw new FileNotFoundException("File not found.", emlFullPath);

            SPFolder myLibrary = oWeb.Folders[sharePointDocLib];

            if (SPWeb.GetFolder(requestNo).Exists) <--errored
            {
                //Folder Exisits
            }

May I know what have I missed? Below is the error message.

An object reference is required for the non-static field, method, or property SPWeb.GetFolder(string)

3

There are 3 answers

0
TheGeneral On BEST ANSWER

You are calling an instance method like a static method. Just use the instance of SPWeb you have in oWeb

if (oWeb.GetFolder(requestNo).Exists) 

Static Classes and Static Class Members (C# Programming Guide)

0
Jerry On

SPWeb.GetFolder is not a staic method as official document specific:

SPWeb.GetFolder Method

So use the instace oWeb instead:

oWeb.GetFolder(requestNo).Exists
0
pilot13 On

Use the instance of the object oWeb that you created to get the method. The code should be written as follows

 if (oWeb.GetFolder(requestNo).Exists){
         //Folder Exisits
 }