简体   繁体   中英

Error add item to list item in Sharepoint 2013 “InvalidOperationException was unhandled”

I was trying to add item to list item in Sharepoint using C# but then it got this error

An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.SharePoint.dll

the error pointed out in line

using (SPSite site = new SPSite(SiteUrl))

Here's the code :

 private const string SiteUrl = "http://sp2013train2:12877/test/";
    private const string ListName = "Category";

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        using (SPSite site = new SPSite(SiteUrl))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPList list = web.Lists.TryGetList(ListName);

                SPListItem newItem = list.Items.Add();
                // notice that Project Name Column still referring to Title column even though we have changed that 
                newItem["Title"] = textBox1.Text;
                newItem.Update();
            }
        }
    }

Any help will be helpful, thank you in advance

Try the following:

web.AllowUnsafeUpdates = true; 
SPList list = web.Lists.TryGetList(ListName);
SPListItem newItem = list.Items.Add();
// notice that Project Name Column still referring to Title column even though we have changed that 
newItem["Title"] = textBox1.Text;
newItem.Update();
web.allowUnsafeUpdates = false;

I solve the same problem by adding dll from ISAPI folder. I have taken wrong microsoft.sharepoint.dll

Regards, Prafulla

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM