错误在Sharepoint 2013中添加项目到列表项" InvalidOperationException未处理"

时间:2014-11-07 06:55:32

标签: c# visual-studio-2012 sharepoint sharepoint-2013

我试图使用C#在Sharepoint中添加项目到列表项,但后来又出现了这个错误

  

未处理的类型' System.InvalidOperationException'   发生在Microsoft.SharePoint.dll

中指出的错误
using (SPSite site = new SPSite(SiteUrl))

以下是代码:

 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();
            }
        }
    }

任何帮助都会有所帮助,谢谢你提前

2 个答案:

答案 0 :(得分:0)

尝试以下方法:

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;

答案 1 :(得分:0)

我通过从ISAPI文件夹添加dll来解决同样的问题。 我错了microsoft.sharepoint.dll

此致 Prafulla