Update Sharepoint List Item

前端 未结 4 922
耶瑟儿~
耶瑟儿~ 2020-12-15 00:31

I got following error...

System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.SharePoint.SPListItem.get

相关标签:
4条回答
  • 2020-12-15 00:59

    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.

    0 讨论(0)
  • 2020-12-15 01:11

    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();}
    
    0 讨论(0)
  • 2020-12-15 01:14

    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;
    
    0 讨论(0)
  • 2020-12-15 01:18

    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.

    0 讨论(0)
提交回复
热议问题