Link to home
Start Free TrialLog in
Avatar of Isaac
IsaacFlag for United States of America

asked on

CheckFolderExists(Microsoft.SharePoint.SPWeb, string)' has some invalid arguments

I get the following error when I try to check if a folder exists in a document library.

Object reference not set to an instance of an object. : c:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\TEMPLATE\CONTROLTEMPLATES\WebUserControl.ascx(27): error CS1502: The best overloaded method match for 'ASP._controltemplates_webusercontrol_ascx.CheckFolderExists(Microsoft.SharePoint.SPWeb, string)' has some invalid arguments  


myWeb = SPContext.Current.Web;
destList = (SPDocumentLibrary)myWeb.Lists["HOTLINE"];

When I do this, using myWeb, it works.
bool folderExist = CheckFolderExists(myWeb, txtCaseNumber.ToString());

Using destList, does not work
bool folderExist = CheckFolderExists(destList, txtCaseNumber.ToString());

Any suggestions?
private bool CheckFolderExists(SPWeb wb, string fldr)
    {
        try
        {
            if (!string.IsNullOrEmpty(fldr)) 
            {
                SPFolder folder = wb.GetFolder(fldr);
                return folder.Exists;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }

Open in new window

Avatar of abhitrig
abhitrig
Flag of United States of America image

your method takes SPWeb as an input variable:CheckFolderExists(SPWeb


myWeb = SPContext.Current.Web;
destList = (SPDocumentLibrary)myWeb.Lists["HOTLINE"];

myweb return SPWeb, so it works fine
destList return a SPList, so it breaks.

If you are tryiong to get the folder from the "hotline" list, the code should be

string url = web.ServerRelativeUrl + "/hotline/FOLDERNAME";
                        SPFolder folder = web.GetFolder(url);

Hope this helps!
Avatar of Isaac

ASKER

I am trying to check if a folder exists in the document library.
How do I do that?  I thought the code I had above accomplished that.
Avatar of Isaac

ASKER

intellisense is not picking up the "web" part of the web.serverRelatativeurl.
Should be SPContext.Current.Web.ServerRelativeUrl in your case
Avatar of Isaac

ASKER

Now I get this error

Object reference not set to an instance of an object. : c:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\TEMPLATE\CONTROLTEMPLATES\WebUserControl.ascx(37): error CS0103: The name 'web' does not exist in the current context  
 

 

string url = SPContext.Current.Web.ServerRelativeUrl + "dev/" + txtCaseNumber.ToString();
                        SPFolder folder = web.GetFolder(url);

                        bool folderExist = CheckFolderExists(url, folder);


 private bool CheckFolderExists(SPWeb wb, string fldr)
    {
        try
        {
            if (!string.IsNullOrEmpty(fldr))
            {
                SPFolder folder = wb.GetFolder(fldr);
                return folder.Exists;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }

Open in new window

SPFolder folder = web.GetFolder(url);

to   SPFolder folder =SPContext.Current.Web.GetFolder(url);
Avatar of Isaac

ASKER

I still get an error:

 error CS1502: The best overloaded method match for 'ASP._controltemplates_webusercontrol_ascx.CheckFolderExists(Microsoft.SharePoint.SPWeb, string)' has some invalid arguments
Avatar of Isaac

ASKER

I modified the code to the below and I get no errors but when it gets to my if statement that checks if the folder exist, it's always "None" even though the folder does exist.

Any ideas?
string caseFolder = SPContext.Current.Web.ServerRelativeUrl + "/" + txtCaseNumber.ToString();

SPWeb aWeb = SPContext.Current.Web;
bool folderExist = CheckFolderExists(aWeb, caseFolder);

                        if (folderExist)
                        {
                            msg.Text += "Folder Exist<br />";
                        }
                        else
                        {
                            msg.Text += "None<br />";
                        }








private bool CheckFolderExists(SPWeb wb, string fldr)
    {
        try
        {
            if (!string.IsNullOrEmpty(fldr))
            {
                SPFolder folder = wb.GetFolder(fldr);
                return folder.Exists;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }

Open in new window



Hi good morning!

Simple explanation on error that you have received.

myWeb = SPContext.Current.Web;
destList = (SPDocumentLibrary)myWeb.Lists["HOTLINE"];
\

Case 1:
When I do this, using myWeb, it works.
bool folderExist = CheckFolderExists(myWeb, txtCaseNumber.ToString());


Case 2;
Using destList, does not work
bool folderExist = CheckFolderExists(destList, txtCaseNumber.ToString());


case 1 will work because your passing the correct PARAMETER SPWEB on your CheckFolderExists function...

case 2 will not work because your passing incorrect object on the SPWEB on your CheckFolderExists function.


myWeb = SPContext.Current.Web;

*** myWeb is an instance of SPWeb object


destList = (SPDocumentLibrary)myWeb.Lists["HOTLINE"];
*** destList  is an instance of SPList and NOT SPWEB..


did you get now why your having error?


try something like this.

private bool CheckFolderExists(SPWeb parentWeb, string folderName) {
    try
    {
          SPFolder folder = parentWeb.GetFolder(folderName);
          if (folder == null) {
              return false;
          }
          else {
               return true;
          }
      }
   }
  catch(System.Exception ex)
  {
     return false;
  }



to call the method to check if specific folder exist..
try something like this...


using (SPSite site = new SPSite("http://server/site"))   //change this URL by your SITE URL..
{
using (SPWeb web = site.OpenWeb())
{
   bool folderExist = CheckFolderExists(web, "yourFolderName"
}




i hope i can give u some idea..


best regards,
Alvin.
Avatar of Isaac

ASKER

Thanks Alvin for your comment.

Here's what I have.... It will always show that the folder does not exist even if it does.

bool folderExist = CheckFolderExists(myWeb, "010203");

                        if (folderExist)
                        {
                            msg.Text += "Folder Exist<br />";
                        }
                        else
                        {
                            msg.Text += "None<br />";
                        }

 private bool CheckFolderExists(SPWeb wb, string fldr)
    {
        try
        {
            SPFolder folder = wb.GetFolder(fldr);
            if (folder == null)
            {
                msg.Text += "TRUE<br />";
                return folder.Exists;
            }
            else
            {
                msg.Text += "FALSE<br />";
                return false;
            }
        }
        catch
        {
            return false;
        }
    }


Hi Good Morning!

What is the value of myWeb on the  bool folderExist = CheckFolderExists(myWeb, "010203");

Does the currently log-user has access on the myWeb and the Folder your looking?


In sharepoint, permissions has very big factor to consider.


Can you attach your code here for us to check..


thanks.


Best regards,
Alvin
Avatar of Isaac

ASKER

Here's all my code
<%@ Control Language="C#" AutoEventWireup="true" %>
<%@ Register assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.WebControls" tagprefix="SharePoint" %>
<%@ Register Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint" tagprefix="SharePoint" %>
<%@ Register Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.Utilities" tagprefix="Utilities" %>


<script runat="server">

   protected void Button1_Click(object sender, EventArgs e)
   {
       using (SPSite mySite = new SPSite("http://dev/"))
       {
           using (SPWeb myWeb = mySite.OpenWeb())
           {

               SPList sourceList = (SPDocumentLibrary)myWeb.Lists["Hotline List"];

               SPList destList = (SPDocumentLibrary)myWeb.Lists["HOTLINE"];

               foreach (GridViewRow row in GridView1.Rows)
               {
                   HiddenField hdID = (HiddenField)row.FindControl("hdID");
                   CheckBox complete = (CheckBox)row.FindControl("complete");
                   DropDownList ddlStatus = (DropDownList)row.FindControl("status");
                   TextBox txtCaseNumber = (TextBox)row.FindControl("caseNumber");
                   DropDownList ddlPriority = (DropDownList)row.FindControl("priority");

                   if (complete.Checked)
                   {
                       SPListItem item = sourceList.GetItemById(Convert.ToInt32((hdID.Value)));
                       byte[] fileBytes = item.File.OpenBinary();
                       string destUrl = destList.RootFolder.Url + "/" + item.File.Name;
                       SPFile destFile = destList.RootFolder.Files.Add(destUrl, fileBytes, true /*overwrite*/);

                       string folderPath = destList.RootFolder.Url;
                       string caseFolder = myWeb.ServerRelativeUrl + "/HOTLINE/" + txtCaseNumber.Text.ToString();

                       msg.Text += caseFolder + "<br />";

                       bool folderExist = CheckFolderExists(myWeb, caseFolder);
                       msg.Text += folderExist+"<br /><br />";

                       if (folderExist)
                       {
                           msg.Text += "Folder Exist<br />";
                       }
                       else
                       {
                           msg.Text += "None<br />";
                       }


                       // add the metadata to File
                       SPListItem destItem = destFile.Item;
                       destItem["STATUS"] = ddlStatus.SelectedValue.ToString();
                       destItem["CASE NUMBER"] = txtCaseNumber.Text.ToString();
                       destItem["PRIORITY"] = ddlPriority.SelectedValue.ToString();
                       destItem["COMPLETE"] = complete.Checked.ToString();

                       destItem.Update();

                       item.Delete();

                       //Re-bind the Grid after delete
                       GridView1.DataBind();
                   }
               }
           }
       }
   }


   private bool CheckFolderExists(SPWeb wb, string fldr)
   {
       try
       {
           SPFolder folder = wb.GetFolder(fldr);
           if (folder==null)
           {
               msg.Text += "FALSE<br />";
               return false;
           }
           else
           {
               msg.Text += "TRUE<br />";
               return folder.Exists;
           }
       }
       catch
       {
           return false;
       }
   }

   protected void Page_Load(object sender, EventArgs e)
   {
   }

</script>

<style type="text/css">
   .style1
   {
       width: 100%;
   }
</style>

<SharePoint:SPDataSource ID="HotLineCase"
                 runat="server"
                 DataSourceMode="List"
                 SelectCommand="<Query><OrderBy><FieldRef Name='Modified' Ascending='False' /></OrderBy></Query>">
     <selectParameters>
           <asp:Parameter Name="WebID" DefaultValue="RootWeb" />
           <asp:Parameter Name="ListName" DefaultValue="Hotline List" />
     </selectParameters>
</SharePoint:SPDataSource>

<table class="style1">
       <tr>
           <td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
   DataSourceID="HotLineCase" Font-Names="Verdana" Font-Size="X-Small"
   HorizontalAlign="Center" GridLines="Horizontal" DataKeyNames="ID">
   <RowStyle HorizontalAlign="Center" />
   <Columns>

       <asp:TemplateField HeaderText="Complete">
       <ItemTemplate>
           <asp:CheckBox runat="server" id="complete" Text="" />
       </ItemTemplate>
       </asp:TemplateField>

       <asp:BoundField HeaderText="Type" DataField="Type" />

       <asp:TemplateField HeaderText="Case Number">
           <ItemTemplate>
               <asp:TextBox ID="caseNumber" runat="server"></asp:TextBox>
           </ItemTemplate>
       </asp:TemplateField>

       <asp:TemplateField HeaderText="Document Name">
           <ItemTemplate>
               <asp:HyperLink ID="DnLink" runat="server" NavigateUrl='<%# "http://landis/Hotline%20List/" + Eval("Name").ToString() %>' Text='<%# Bind("Name") %>'></asp:HyperLink>
           </ItemTemplate>
       </asp:TemplateField>

       <%--<asp:TemplateField HeaderText="Document Name"></asp:TemplateField>--%>
       <asp:TemplateField HeaderText="STATUS">
       <ItemTemplate>
           <asp:DropDownList ID="status" runat="server">
               <asp:ListItem Value="...."></asp:ListItem>
               <asp:ListItem Value="Reroute">Reroute</asp:ListItem>
               <asp:ListItem Value="Spam">Spam</asp:ListItem>
           </asp:DropDownList>
       </ItemTemplate>
       </asp:TemplateField>
       <asp:BoundField HeaderText="Modified" DataField="Modified" />
       <asp:TemplateField HeaderText="Priority">
           <ItemTemplate>
              <asp:DropDownList ID="priority" runat="server">
               <asp:ListItem Value="">.....</asp:ListItem>
               <asp:ListItem Value="Yes">Yes</asp:ListItem>
              <asp:ListItem Value="No">No</asp:ListItem>
           </asp:DropDownList>
           </ItemTemplate>
       </asp:TemplateField>
       <asp:BoundField HeaderText="E-Mail From" DataField="E-Mail From" />
       <asp:TemplateField>
           <ItemTemplate>
               <asp:HiddenField ID="hdID" runat="server" Value='<%# Eval("ID") %>' Visible="True" />
           </ItemTemplate>
       </asp:TemplateField>
   </Columns>
   <HeaderStyle HorizontalAlign="Center" BackColor="#CCCCCC" />
   <AlternatingRowStyle BackColor="#EEEEEE" />
</asp:GridView>
           </td>
       </tr>
       <tr>
           <td align="center"><asp:Button ID="Button1" runat="server" Text="Transfer"
                   onclick="Button1_Click" /></td>
       </tr>
       <tr>
           <td align="center">
               <asp:Label ID="msg" runat="server" Text=""></asp:Label></td>
       </tr>
   </table>
   <br />

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of game-master
game-master
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Isaac

ASKER

Good Morning Alvin,

Thanks for helping

So are you saying that I should change this

string caseFolder = myWeb.ServerRelativeUrl + "/HOTLINE/" + txtCaseNumber.Text.ToString();
 to this

string caseFolder = txtCaseNumber.Text.ToString();


' txtCaseNumber.Text.ToString()' is what the user enters.  I then have to check if that folder exists.

I will give what you say a try.  

Thanks and I really appreciate your help.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Isaac

ASKER

Hellp Alvin,

That did not work.

Don't I need to get my "SPWeb" to point to the document library where the folder is located for it to be found?


The code below always returns false even if the folder does exist.
private bool CheckFolderExists(SPWeb wb, string fldr)
    {
        try
        {
            SPFolder folder = wb.GetFolder(fldr);
            if (!folder.Exists)
            {
                msg.Text += "NO FOLDER<br />";
                return false;
            }
            else
            {
                msg.Text += "FOLDER EXISTS<br />";
                return folder.Exists;
            }
        }
        catch
        {
            return false;
        }
    }
Hi,

Good Morning!

Can you debug on which part of that method causes the return value to be false?

Try to put a debug on the CATCH, lets see what's the problem.

and change the "return.Exists" to just True.


Let me know if you try it again.

Browse the Document Library your looking on your sharepoint site, then send me the URL that appears on the browser..


Best regards,
Alvin

Avatar of Isaac

ASKER

game-master,

Thanks for your help!

Here's what I did to get it working:

string caseFolder = "/HOTLINE1/" + txtCaseNumber.Text.ToString();

Someone in another forum said this:

GetFolder should have a path to the Folderi.e.

For Lists:

/Lists/ListName/FolderName

This is assuming the SPWeb object points to the site which has this List object

For Document libraries it will be

/DocLibName/FolderName
Avatar of Isaac

ASKER

My solution is right.


Hi TheInnovator,

Good morning!

I'm glad you solve the problem with your own idea..I'ts just simply means that you continue working and learning for the things that your not familiar with..


I'm glad i gave you some idea.
thanks for the points.


best regards,
Alvin