0

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)

gymcode
  • 4,431
  • 15
  • 72
  • 128
  • my `requestNo` is a parameter that is parsed in from my `main`. do I need to specially handle it? – gymcode Aug 26 '19 at 04:51
  • Possible duplicate of [CS0120: An object reference is required for the nonstatic field, method, or property 'foo'](https://stackoverflow.com/questions/498400/cs0120-an-object-reference-is-required-for-the-nonstatic-field-method-or-prop) – Bagus Tesa Aug 26 '19 at 04:51
  • sorry, this one might be a duplicate. [`SPWeb.GetFolder`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.sharepoint.spweb?view=sharepoint-server) is not indicated as a static and you call it on a static method. – Bagus Tesa Aug 26 '19 at 04:52

3 Answers3

0

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)

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
0

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
 }
pilot13
  • 36
  • 3
0

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

SPWeb.GetFolder Method

So use the instace oWeb instead:

oWeb.GetFolder(requestNo).Exists
Jerry
  • 3,480
  • 1
  • 10
  • 12