更新 Sharepoint 列表项

发布于 2024-08-07 19:11:19 字数 858 浏览 10 评论 0原文

我收到以下错误...

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 技术交流群。

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

发布评论

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

评论(4

北渚 2024-08-14 19:11:19

如果您尝试更改刚刚插入的列表项的值,您应该这样做:

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

您只需要使用 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:

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 SPQuery class.

云淡风轻 2024-08-14 19:11:19

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

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

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();}
翻身的咸鱼 2024-08-14 19:11:19

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

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

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

 SPList list = web.Lists["ListName"]; // 2        
 SPListItem item = list.Items.Add();
 item["Title"] = "Test";
 item.Update ();
 list.Update ();
 Guid itemId = item.UniqueId;
流绪微梦 2024-08-14 19:11:19

我最好的问题是,当您这样做时,您的项目尚未在列表中创建:

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

首先执行 item.Update() ,然后再请求 uniqueId 和/或从列表中获取项目。

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文