Duplicate list item in code behind (simple)


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. // http://www.example.com/page?nid=2
  4. string nid = HttpContext.Current.Request.QueryString["nid"];
  5.  
  6. if(!string.IsNullOrEmpty(nid))
  7. {
  8. using (SPSite site = SPContext.Current.Site)
  9. {
  10. using (SPWeb web = site.OpenWeb())
  11. {
  12. // needed for list item update
  13. web.AllowUnsafeUpdates = true;
  14. // get list
  15. SPList list = web.Lists["ListA"];
  16. // get list item by Id from url
  17. SPListItem item = list.GetItemById(Convert.ToInt32(nid));
  18. // create new list item
  19. SPListItem itemCopy = list.Items.Add();
  20. // copy attributes
  21. itemCopy["Title"] = item.Title;
  22. // update db
  23. itemCopy.Update();
  24. // set back to default
  25. web.AllowUnsafeUpdates = false;
  26. }
  27. }
  28. }
  29. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.