SharePoint list error - FileNotFoundException

Hello Gurus,

I am trying to update a sharepoint list via the web.

What am I doing wrong with the code below?

I get the following error:

"FileNotFoundException was unhandled by user code
Ther is no Web named '/Lists/Parts'. "

Here's a link to the actual link:
http://localhost/Lists/Parts/AllItems.aspx

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.SharePoint;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //String parameters to enable site and list access
        const string siteUrl = "http://landis";
        const string siteName = "/Lists/Parts/";
        const string listName = "Parts";

        //Return a site collection using the SPSite constructor providing the site URL
        SPSite siteCollection = new SPSite(siteUrl); //

        //Return the target web site based on site name
        SPWeb site = siteCollection.AllWebs[siteName];

        //Return a collection of list items based on the Items property
        //for the specific named list
        SPListItemCollection listItems = site.Lists[listName].Items;

        //Add a list item to the list items collection
        SPListItem item = listItems.Add();

        //Populate item data via user indexers with form input data
        item["Title"] = title.Text;
        item["Part Number"] = Convert.ToInt16(partNum.Text);
        item["Price"] = Convert.ToInt16(price.Text);

        //Update SP database with changes made to this item
        item.Update();

        //UI clean-up
        Label1.Text = "Done!";


    }
}

Open in new window

LVL 5
IsaacSharePoint Client Side DeveloperAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
psv1973Connect With a Mentor Commented:
Can you replace

using (SPWeb myWeb = mySiteCollection.OpenWeb())

with

SPWeb myWeb = mySiteCollection.Rootweb;

0
 
psv1973Commented:
Is this correct?

const string siteName = "/Lists/Parts/";
const string listName = "Parts";

It looks like SiteName is pointing to a list.

0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Ok.... Maybe I'm confused.  Is it supposed to be the url which is already siteUrl?  How do I find the siteName?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Ok.  I did a little research and modified and now I get this error: SPException was unhandled by user code.  The security validation for this page is invalid.  Click Back in your web browser, refresh the page, and try your operation again.

        using (SPSite mySiteCollection = new SPSite("http://landis/"))
        {
            using (SPWeb myWeb = mySiteCollection.OpenWeb())
            {
                SPList PartsList = myWeb.Lists["Parts"];
                SPListItem newPartItem = PartsList.Items.Add();

                newPartItem["Title"] = title.Text;
                newPartItem["Part Number"] = Convert.ToInt16(partNum.Text);
                newPartItem["price"] = Convert.ToDecimal(price.Text); 

               [b] newPartItem.Update();   [/b]<-- Error points here
            }
        }

Open in new window

0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
I modified the code using RunWithElebatedPrivileges but I still get an error:

Here's the error:

invalidoperationexception was unhandled by user code
operation is not valid due to the current state of the object.

string errVal = "";
         try  
          {              
             SPSecurity.RunWithElevatedPrivileges(delegate()
             {
                using (SPSite mySiteCollection = new SPSite("http://landis/"))
                {
                    using (SPWeb myWeb = mySiteCollection.OpenWeb())
                    {
                        SPList PartsList = myWeb.Lists["Parts"];
                        SPListItem newPartItem = PartsList.Items.Add();

                        newPartItem["Title"] = title.Text;
                        newPartItem["Part Number"] = Convert.ToInt16(partNum.Text);
                        newPartItem["Price"] = Convert.ToDecimal(price.Text);

                        newPartItem.Update();
                    }
                }
             });
         }
         catch (Exception f) 
         { 
             errVal = f.Message;

Open in new window

0
 
psv1973Commented:
Can you set SPWeb.AllowUnsafeUpdates to true before the update and back to false afterwards?
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
I actually tried that but still got the same error.
0
 
psv1973Commented:
Do you have checkin/checkout enabled on your list?
0
 
psv1973Commented:
Can you try doing the same throguh the interface.

Create a new item and fill only Title, Part number and Price. Does the item save ok?
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
>> Do you have checkin/checkout enabled on your list?  <<
Not sure how to disable/enable it

I am able to create and save a new item through the interface.
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Now I get this error:
Error      2      'Microsoft.SharePoint.SPSite' does not contain a definition for 'Rootweb' and no extension method 'Rootweb' accepting a first argument of type 'Microsoft.SharePoint.SPSite' could be found (are you missing a using directive or an assembly reference?)      C:\Users\ISogunro\Documents\Visual Studio 2008\WebSites\partNum\Default.aspx.cs      31      52      C:\...\partNum\
0
 
psv1973Commented:
That should have been RootWeb
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Ignore my last post.

I'm actually getting the same error as above.

invalidoperationexception was unhandled by user code
operation is not valid due to the current state of the object.
0
 
psv1973Commented:
On the following link:
http://msdn.microsoft.com/en-us/library/ms467435.aspx

I found this example:

SPWeb mySite = SPContext.Current.Web;
SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items;

SPListItem item = listItems.Add();

item["Title"] = TextBox2.Text;
item["Stock"] = Convert.ToInt32(TextBox3.Text);
item["Return Date"] = Convert.ToDateTime(TextBox4.Text);
item["Employee"] = TextBox5.Text;

item.Update();

Open in new window



If your code is run from the site ( i.e. not a tiemr job or sothing like that) can you try using SPContext.Current.Web;
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
If someone doesn't mind, could you create a list in your SharePoint environment that has 3 columns:
1)Title
2)Part Number
3)Price

Then try my code below.  I'm starting to wonder if it's the environment I'm in.  Just doesn't make sense why it's not working.
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      
        string errVal = "";
         try  
          {              
             SPSecurity.RunWithElevatedPrivileges(delegate()
             {
                 using (SPSite mySiteCollection = new SPSite("http://landis/Lists/Parts/"))
                {
                    using (SPWeb myWeb = mySiteCollection.RootWeb)
                    {
                        myWeb.AllowUnsafeUpdates = true;
                        SPList PartsList = myWeb.Lists["Parts"];
                        SPListItem newPartItem = PartsList.Items.Add();

                        newPartItem["Title"] = title.Text;
                        newPartItem["Part Number"] = Convert.ToInt16(partNum.Text);
                        newPartItem["Price"] = Convert.ToDecimal(price.Text);

                        newPartItem.Update();
                        myWeb.AllowUnsafeUpdates = false;
                    }
                }
             });
         }
         catch (Exception f) 
         { 
             errVal = f.Message; 
         }
 


    }
}

Open in new window

0
 
psv1973Commented:
That code worked for me. I stepped through the code witht he debuger and no problems at all. I added the button to a VisualWebpart project.
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
DANG!
It might be my environment.
I'm with a certain branch of the government and they just implement HOST BASED SECURITY SYSTEM(HBSS) which seems to be breaking everything.  That's just a guess though.
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
>> I added the button to a VisualWebpart project. <<
Did you use the exact same code I used?

>> SPWeb mySite = SPContext.Current.Web; <<
This won't work for me because I'm creating from a .aspx page.  see below:


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" Text="Title"></asp:Label>
        <asp:TextBox ID="title" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Part Number"></asp:Label><asp:TextBox ID="partNum" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label3" runat="server" Text="Price"></asp:Label><asp:TextBox ID="price" runat="server"></asp:TextBox>
        <br />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    
    </div>
    </form>
</body>
</html>

Open in new window

0
 
psv1973Commented:
Yes, the code is the same ( I just changed the Url and list name)
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
How did you get the VisualWebPart project?
0
 
psv1973Commented:
I created it in Visual Studio.

You can install the web part through the wsp attached if you like.
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Did you do it with visual studio 2008 on SharePoint 2007?
0
 
psv1973Commented:
VS 2010
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
I will try the code when I get home in my SharePoint environment to make sure it's not my work environment.
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
This worked for me
try  
          {              


                 using (SPSite mySiteCollection = new SPSite(@"http://localhost:8716/sites/SolutionPortal"))
                 {
                        SPWeb myWeb = mySiteCollection.OpenWeb("SolutionSiteTemplate");
                        myWeb.AllowUnsafeUpdates = true;
                        SPList PartsList = myWeb.Lists["Parts"];
                        SPListItemCollection col = PartsList.Items;
                        Int32 itest = col.Count;
                        SPListItem newPartItem = col.Add();

                        newPartItem["Title"] = title.Text;
                        newPartItem["Part Number"] = Convert.ToInt16(partNum.Text);
                        newPartItem["Price"] = Convert.ToDecimal(price.Text);

                        newPartItem.Update();
                        itest = col.Count;
                        myWeb.AllowUnsafeUpdates = false;
                        myWeb.Close();
                }
         }
         catch (Exception f) 
         { 
             errVal = f.Message; 
         }
    }

Open in new window

0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
My solution also worked which is the last comment
0
All Courses

From novice to tech pro — start learning today.