We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

SPListItem item.delete

Isaac
Isaac asked
on
Medium Priority
1,247 Views
Last Modified: 2012-06-27
I have a webpart that displays a document library list.  It has a checkbox that allows users to select certain documents and then transfer to another document library by clicking the submit button.  When they click Submit, it should delete it out of the current document library.

The delete does happen but it still shows in my webpart.  When I click refresh, I get the error below because the listitem does not exist.

Value does not fall within the expected range.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Value does not fall within the expected range.

Source Error:


Line 27:                 if (complete.Checked)
Line 28:                 {
***Line 29:                         SPListItem item = sourceList.GetItemById(Convert.ToInt32((hdID.Value))); ***
Line 30:                         byte[] fileBytes = item.File.OpenBinary();
Line 31:                         string destUrl = destList.RootFolder.Url + "/
 

foreach (GridViewRow row in GridView1.Rows)
            {                         
                HiddenField hdID = (HiddenField)row.FindControl("hdID");
                CheckBox complete = (CheckBox)row.FindControl("complete");
                DropDownList ddlStatus = (DropDownList)row.FindControl("status");
                TextBox txtCaseNumber = (TextBox)row.FindControl("caseNumber");
                DropDownList ddlPriority = (DropDownList)row.FindControl("priority");                              

                if (complete.Checked)
                {
                        SPListItem item = sourceList.GetItemById(Convert.ToInt32((hdID.Value))); 
                        byte[] fileBytes = item.File.OpenBinary();
                        string destUrl = destList.RootFolder.Url + "/" + item.File.Name;
                        SPFile destFile = destList.RootFolder.Files.Add(destUrl, fileBytes, true /*overwrite*/);

                        // add the metadata to File            
                        SPListItem destItem = destFile.Item;
                        destItem["STATUS"] = ddlStatus.SelectedValue.ToString();
                        destItem["CASE NUMBER"] = txtCaseNumber.Text.ToString();
                        destItem["PRIORITY"] = ddlPriority.SelectedValue.ToString();
                        destItem["COMPLETE"] = complete.Checked.ToString();

                        destItem.Update();
                    
                        item.Delete();
                }
        }

Open in new window

Comment
Watch Question

Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
And to add to my point above, if you are dealing with files inside sharepoint document library, why not use SPFile.MoveTo   command to move the file directly instead of deleting it?
IsaacSharePoint Client Side Developer

Author

Commented:
I needed to move multiple selected documents and the way I found to do it was the code I have above.  How can I modify the above to use SPFile.MoveTo ?  It seems to be more efficient.
IsaacSharePoint Client Side Developer

Author

Commented:
Thanks Dude!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.