更新 Sharepoint 列表项
我收到以下错误...
System.NullReferenceException:未将对象引用设置为对象的实例。 在 Microsoft.SharePoint.SPListItem.get_UniqueId() 在 Program.cs 中的 ConsoleApplication1.Program.Main(String[] args):第 21 行
行运行以下代码
using (SPSite site = new SPSite("http://site/"))
{
using (SPWeb web = site.OpenWeb())
{
try
{
SPList list = web.Lists["ListName"]; // 2
SPListItem item = list.Items.Add();
Guid itemId = item.UniqueId;
SPListItem itemUpdate = web.Lists["ListName"].Items[itemId];
itemUpdate["PercentComplete"] = .45; // 45%
itemUpdate.Update();
}
catch (Exception e)
{
Console.WriteLine(e);
Console.ReadLine();
}
}
}
有什么问题?
I got following error...
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.SharePoint.SPListItem.get_UniqueId()
at ConsoleApplication1.Program.Main(String[] args) in Program.cs:line 21
running following code
using (SPSite site = new SPSite("http://site/"))
{
using (SPWeb web = site.OpenWeb())
{
try
{
SPList list = web.Lists["ListName"]; // 2
SPListItem item = list.Items.Add();
Guid itemId = item.UniqueId;
SPListItem itemUpdate = web.Lists["ListName"].Items[itemId];
itemUpdate["PercentComplete"] = .45; // 45%
itemUpdate.Update();
}
catch (Exception e)
{
Console.WriteLine(e);
Console.ReadLine();
}
}
}
What's the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您尝试更改刚刚插入的列表项的值,您应该这样做:
您只需要使用
list.Items[uniqueId]
或更快的list.GetItemByUniqueId(uniqueId)< /code> 如果您需要查找要更新的特定项目;使用
SPQuery
类可以完成什么操作。If you're trying to alter values for a just inserted list item, you should go with:
You just need to use
list.Items[uniqueId]
or fasterlist.GetItemByUniqueId(uniqueId)
if you needs to find a particular item to update; what can be accomplished by usingSPQuery
class.鲁本的答案是正确的,但几乎没有错误(可能只适合我),因此我稍微调整了一下,然后就工作正常了。下面是我使用的代码,如果有人需要的话
Ruben's answer was correct but was getting few errors (may be its was only for me) therefore i tweaked little bit and then it was working fine. Below is the code which i used if anyone needs it
在获取UniqueID之前尝试对列表调用Update()
Try calling Update () on the list before getting the UniqueID
我最好的问题是,当您这样做时,您的项目尚未在列表中创建:
首先执行 item.Update() ,然后再请求 uniqueId 和/或从列表中获取项目。
PS:我认为没有理由应该获取第二个 SPItem 对象来更新“PercentComplete”信息。
My best quess is that your item is not yet created in the list when you do:
First do a item.Update() before requesting the uniqueId and/or getting the item back from a list.
PS : I see no reason why you should get a second SPItem object for updating the 'PercentComplete' information.