locked
PLease help in customization by c# code user control for save ,cancel edit,delete functionality in sharepoint style RRS feed

  • Question

  • HI please provide sample code for save,cancel,edit,delete

    i have custom form web part which i added on test server please let me know how to save my data cancel after saving navigate to allitem.aspx

    how to edit and delete on item level basis....

    PLease help

    Thank you so much in advance


    sudhanshu sharma Do good and cast it into river :)

    Friday, March 30, 2012 6:56 AM

Answers

  • Hi sudhanshuSharma,

    You can use SharePoint Object Model to Add and Cancel a SharePoint list item:
    Save
    SPWeb mySite = SPContext.Current.Web;
    SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items;
    
    SPListItem item = listItems.Add();
    
    item["Title"] = TextBox2.Text; //get the control value from your custom form 
    item.Update();
    }
    Cancle
    Response.Redirect(your url);//use this navigate to your alitem.aspx
    If you want to edit or delete the item on your custom form, you need add a link or button redirect to your custom page with the item id, then use SharePoint Object Model to get the list item value and assign value to your custom form to edit the list item.
                // Update the List item by ID
                SPListItem itemToUpdate = list.GetItemById(listItemId);
                itemToUpdate["Description"] = "Changed Description";
                itemToUpdate.Update();
     
                // Delete List item
                SPListItem itemToDelete = list.GetItemById(listItemId);
                itemToDelete.Delete();

    Thank,
    Lhan Han
    Monday, April 2, 2012 11:13 AM
    Moderator

All replies

  • Hi sudhanshuSharma,

    You can use SharePoint Object Model to Add and Cancel a SharePoint list item:
    Save
    SPWeb mySite = SPContext.Current.Web;
    SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items;
    
    SPListItem item = listItems.Add();
    
    item["Title"] = TextBox2.Text; //get the control value from your custom form 
    item.Update();
    }
    Cancle
    Response.Redirect(your url);//use this navigate to your alitem.aspx
    If you want to edit or delete the item on your custom form, you need add a link or button redirect to your custom page with the item id, then use SharePoint Object Model to get the list item value and assign value to your custom form to edit the list item.
                // Update the List item by ID
                SPListItem itemToUpdate = list.GetItemById(listItemId);
                itemToUpdate["Description"] = "Changed Description";
                itemToUpdate.Update();
     
                // Delete List item
                SPListItem itemToDelete = list.GetItemById(listItemId);
                itemToDelete.Delete();

    Thank,
    Lhan Han
    Monday, April 2, 2012 11:13 AM
    Moderator
  • HI Thanks Lhan,Can you help more for edit

    i got 1 input -

    1st need to pass query stringNewForm.aspx?mode="Add" or "edit"

    in user control custom code in page load pass this query string request.querystring...whr check if mode is add then go to newform.aspx if edit then same page i can use as edit..and thr write this code item.update please help me in detail...


    sudhanshu sharma Do good and cast it into river :)

    Tuesday, April 3, 2012 9:46 AM
  • Hi sudhanshuSharma,

    You can check if "Add" or "edit" on Page_Load Event and Save Event,and then call the different methods

    Thanks,
    Lhan Han
    Tuesday, April 3, 2012 9:53 AM
    Moderator
  • HI Lhan,

    How i will do state management....if edit all data should be in form ,controls should not be empty....


    sudhanshu sharma Do good and cast it into river :)

    Tuesday, April 3, 2012 1:04 PM
  • Hi sudhanshuSharma,

    You can use Asp.Net Validation Controls or use Javascript to validating that if your form control is empty:
    http://msdn.microsoft.com/en-us/library/aa479013.aspx
    http://www.tizag.com/javascriptT/javascriptform.php

    Thanks,
    Lhan Han
    Thursday, April 5, 2012 1:56 AM
    Moderator
  • Thanks Lhan For kind replies.


    sudhanshu sharma Do good and cast it into river :)

    Thursday, April 5, 2012 4:37 AM