Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

explanation:

I have fetched list items based on conditions. Like this.

var query = from SPListItem oItem in splist.Items
                                    where Convert.ToDateTime(oItem["Status"]).Equals("Completed")
                                    select oItem;


 foreach (SPListItem item in query)
 {
      // I get each SPListItem based on my condition
      // Now i want add/copy each list items to same list
 }

How do i add SPListItem whole object to same list?

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

You can try following:

foreach (SPListItem item in query)
{
          SPListItem newItem = list.Items.Add();
          foreach (SPField field in list.Fields)
          {
                  if (!field.ReadOnlyField)
                        newItem[field.Id] = item[field.Id];                            
           }
           newItem.Update();
 }
share|improve this answer
    
i have tried. it give exception... <nativehr>0x80070057</nativehr><nativestack></nativestack>Invalid data has been used to update the list item. The field you are trying to update may be read only. –  Hitesh Chandegara Jan 22 '13 at 9:56
    
Now it works fine... Thanks a lot nadeem.... :D –  Hitesh Chandegara Jan 22 '13 at 11:03
    
The error appears becoz... you need to add web.AllowUnSafeUpdates==true in the beginning.. then at the end make it as false –  Strider Jan 23 '13 at 4:35
add comment

try this:

foreach(SPListItem item in splist.Items)
    {
      foreach(SPField field in splist.Fields)
        {
          SPListItem newItem=splist.Items.Add();
          newItem[field.ID]=item[field.ID];                       
          newItem.Update();
        }
     }
share|improve this answer
    
I fetched list items and want to copy list items to existing list. and also many number of fields available in my fields. so it's time consuming. –  Hitesh Chandegara Jan 22 '13 at 6:52
    
check the above now –  Strider Jan 22 '13 at 7:09
    
i have tried. it give exception... <nativehr>0x80070057</nativehr><nativestack></nativestack>Invalid data has been used to update the list item. The field you are trying to update may be read only. –  Hitesh Chandegara Jan 22 '13 at 9:55
    
your answer helps me a lot... Thanks –  Hitesh Chandegara Jan 22 '13 at 12:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.