C# 更新 Sharepoint 列表项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1578361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:59:40  来源:igfitidea点击:

Update Sharepoint List Item

c#asp.netsharepointwss

提问by K-M

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

System.NullReferenceException:未将对象引用设置为对象的实例。在 Microsoft.SharePoint.SPListItem.get_UniqueId() 在 ConsoleApplication1.Program.Main(String[] args) 在 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?

有什么问题?

采纳答案by Rubens Farias

If you're trying to alter values for a just inserted list item, you should go with:

如果您尝试更改刚刚插入的列表项的值,您应该使用:

SPList list = web.Lists["ListName"];
//SPListItem item = list.Items.Add();
//item["PercentComplete"] = .45; // 45%
//item.Update();

SPListItemCollection items = list.GetItems(new SPQuery()
{
    Query = @"<Where>
                <Eq>
                   <FieldRef Name='Title' />
                   <Value Type='Text'>Desigining</Value>
                </Eq>
              </Where>"
});

foreach (SPListItem item in items)
{
    item["PercentComplete"] = .45; // 45%
    item.Update();
}

You just need to use list.Items[uniqueId]or faster list.GetItemByUniqueId(uniqueId)if you needs to find a particular item to update; what can be accomplished by using SPQueryclass.

如果您需要找到要更新的特定项目,您只需要使用list.Items[uniqueId]或更快list.GetItemByUniqueId(uniqueId);使用SPQuery类可以完成什么。

回答by axel_c

Try calling Update () on the list before getting the UniqueID

尝试在获取 UniqueID 之前调用列表上的 Update()

 SPList list = web.Lists["ListName"]; // 2        
 SPListItem item = list.Items.Add();
 item["Title"] = "Test";
 item.Update ();
 list.Update ();
 Guid itemId = item.UniqueId;

回答by K-M

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

鲁本的回答是正确的,但几乎没有错误(可能只适合我),因此我稍微调整了一下,然后就可以正常工作了。下面是我使用的代码,如果有人需要的话

 SPList list = web.Lists["ListName"];
                    //SPListItem item = list.Items.Add();
                    //item["PercentComplete"] = .45; 
                    // 45%//item.Update();
                   SPQuery oQuery = new SPQuery();

                        oQuery.Query = @"<Where>               
                                    <Eq>                   
                                        <FieldRef Name='Title' />                   
                                        <Value Type='Text'>Design</Value>              
                                    </Eq>            
                                  </Where>";
                        SPListItemCollection collListItems = list.GetItems(oQuery);
                        foreach (SPListItem item in collListItems)
                    {    item["PercentComplete"] = .55;   
                        item.Update();}

回答by Wout

My best quess is that your item is not yet created in the list when you do:

我最好的问题是,当您执行以下操作时,您的项目尚未在列表中创建:

Guid itemId = item.UniqueId;
SPListItem itemUpdate = web.Lists["ListName"].Items[itemId];

First do a item.Update() before requesting the uniqueId and/or getting the item back from a list.

在请求 uniqueId 和/或从列表中取回项目之前,首先执行 item.Update() 。

PS : I see no reason why you should get a second SPItem object for updating the 'PercentComplete' information.

PS:我认为没有理由您应该获得第二个 SPItem 对象来更新“PercentComplete”信息。