Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2011-05-10
22
Medium Priority
?
1,698 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:Isaac
  • 11
  • 6
  • 3
20 Comments
 
LVL 14

Expert Comment

by:abhitrig
ID: 35732183
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!
0
 
LVL 5

Author Comment

by:Isaac
ID: 35737476
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.
0
 
LVL 5

Author Comment

by:Isaac
ID: 35740435
intellisense is not picking up the "web" part of the web.serverRelatativeurl.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Expert Comment

by:abhitrig
ID: 35740924
Should be SPContext.Current.Web.ServerRelativeUrl in your case
0
 
LVL 5

Author Comment

by:Isaac
ID: 35749028
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

0
 
LVL 14

Expert Comment

by:abhitrig
ID: 35749065
SPFolder folder = web.GetFolder(url);

to   SPFolder folder =SPContext.Current.Web.GetFolder(url);
0
 
LVL 5

Author Comment

by:Isaac
ID: 35756315
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
0
 
LVL 5

Author Comment

by:Isaac
ID: 35757044
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

0
 
LVL 13

Expert Comment

by:game-master
ID: 35766306


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.
0
 
LVL 5

Author Comment

by:Isaac
ID: 35769374
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;
        }
    }
0
 
LVL 13

Expert Comment

by:game-master
ID: 35774096


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
0
 
LVL 5

Author Comment

by:Isaac
ID: 35774137
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

0
 
LVL 13

Accepted Solution

by:
game-master earned 2000 total points
ID: 35774328

Good morning!

I was looking at your code, it seems that your passing caseFolder as your folder name but the
caseFolder value is the following format: yourWeb\foldername\... your checkFolderExists method will not see this folder at all on your myWeb.

change the caseFolder to the actual folder name..


I hope i can give u some idea.


Best regards,
Alvin
0
 
LVL 5

Author Comment

by:Isaac
ID: 35774343
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.
0
 
LVL 13

Assisted Solution

by:game-master
game-master earned 2000 total points
ID: 35774407


Hi,

Good morning!

yes, that is what I was saying..

If txtCaseNumber.Text is really the correct format or name of your Folder.


Best Regards,
Alvin
0
 
LVL 5

Author Comment

by:Isaac
ID: 35780325
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;
        }
    }
0
 
LVL 13

Expert Comment

by:game-master
ID: 35782404
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

0
 
LVL 5

Author Comment

by:Isaac
ID: 35797145
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
0
 
LVL 5

Author Closing Comment

by:Isaac
ID: 35797170
My solution is right.
0
 
LVL 13

Expert Comment

by:game-master
ID: 35798760


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
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question