如何以编程方式更新infopath表单库项目字段?

发布于 2024-09-19 08:23:54 字数 960 浏览 2 评论 0原文

我成功地使用共享点对象模型从 infopath 更新库项目的字段之一(布尔类型),就像它是列表项目一样。

但对于另一个文本类型的字段,相同的代码只会被执行,但不会更改字段值!!!!

我正在使用以下代码,该代码适用于该布尔字段,但适用于 string 类型的另一个字段,不确定为什么它不起作用。有什么想法吗?

SPSecurity.RunWithElevatedPrivileges(delegate()

{ 
SPWeb web;

SPSite site = new SPSite("http://sharepointsite"); 
web = site.OpenWeb();

SPList formLibList = web.Lists["FormLibraryName"];

SPQuery query = new SPQuery(); query.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + titleName + "</Value></Eq></Where>"; 
web.Site.WebApplication.FormDigestSettings.Enabled = false;

web.AllowUnsafeUpdates = true; 
SPListItemCollection col = formLibList.GetItems(query);

if (col.Count > 0) 
{

col[0]["CustomerName"] = "test customer name"; 
col[0].Update();

}

web.Site.WebApplication.FormDigestSettings.Enabled = true; web.AllowUnsafeUpdates = false; 
});

谢谢,

尼基尔

I was successfully able to update one of the fields (which was of type boolean) from infopath for library item using sharepoint object Model as if it was a list item.

But for another field which is of type text, the same code just gets executed but does not change the field value !!!!

I am using following code, which works for that boolean field but for another field of type string , not sure why it is not working. Any idea ?

SPSecurity.RunWithElevatedPrivileges(delegate()

{ 
SPWeb web;

SPSite site = new SPSite("http://sharepointsite"); 
web = site.OpenWeb();

SPList formLibList = web.Lists["FormLibraryName"];

SPQuery query = new SPQuery(); query.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + titleName + "</Value></Eq></Where>"; 
web.Site.WebApplication.FormDigestSettings.Enabled = false;

web.AllowUnsafeUpdates = true; 
SPListItemCollection col = formLibList.GetItems(query);

if (col.Count > 0) 
{

col[0]["CustomerName"] = "test customer name"; 
col[0].Update();

}

web.Site.WebApplication.FormDigestSettings.Enabled = true; web.AllowUnsafeUpdates = false; 
});

Thanks,

Nikhil

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北凤男飞 2024-09-26 08:23:55

我必须声明 SPListItem 并设置它,而不是直接修改列表项集合。

I had to declare SPListItem and set it instead of directly modifying list item collection.

蓝海 2024-09-26 08:23:55

这不是您问题的答案(您自己已经找到了解决方案),但您可能希望将 SPSite 和 SPWeb 对象放在 using 块中。在您的示例代码中,您没有处理它们,这会导致内存泄漏。正确的方法是这样的:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite("http://sharepointsite"))
    {
        using (SPWeb web = site.OpenWeb())
        {
            // the rest of your code
        }
    }
});

It's not an answer to your question (you already found the solution yourself), but you may want to put your SPSite and SPWeb objects in a using block. In your example code you are not disposing them, which results in a memory leak. The correct way would be like this:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite("http://sharepointsite"))
    {
        using (SPWeb web = site.OpenWeb())
        {
            // the rest of your code
        }
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文