question

BakulPatel-9047 avatar image
0 Votes"
BakulPatel-9047 asked MichaelHan-MSFT commented

Sharepoint 2016: [ArgumentOutOfRangeException: Specified argument was out of the range of valid values.] when using a File.MoveTo

Hi. Hope someone can help with this:
In Sharepoint 2016 I have the following code:


                 for (int i = 0; i < this.ItemIds.Count(); i++)
                 {
                     try
                     {
                         SPListItem listItem = this.List.GetItemById(this.ItemIds[i]);

                         SPFile file = CommonFunctions.GetFile(listItem.Url);
                         if (!file.ParentFolder.UniqueId.Equals(folder.UniqueId))
                         {
                             file.MoveTo($"{folder.Url}/{file.Name}", false);
                         }

                     }
                     catch
                     {
                         errorMessage = "Failed to move one or more files to the selected location.";
                     }
                 }

The files copy over to the new folder. However, I get the following error, and I am not able to debug where this error is coming from as it does this after the function exited. Stepping through the code does not show where the issue is.
The full error is:
[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.]
Microsoft.SharePoint.SPListItemCollection.get_Item(Int32 iIndex) +37686419
Microsoft.SharePoint.SPListItem.EnsureItemIsValid() +783
Microsoft.SharePoint.SPListItem.GetValue(SPField fld, Int32 columnNumber, Boolean bRaw, Boolean bThrowException) +74
Microsoft.SharePoint.SPListItem.get_File() +153
Microsoft.SharePoint.WebControls.SaveButton.OnBubbleEvent(Object source, EventArgs e) +1069
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5028

sharepoint-dev
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@BakulPatel-9047, What's the value of this.ItemIds ? Please share more details about it.

0 Votes 0 ·

this.ItemIds is defined in the class as:
public int[] ItemIds { get; set; }

It gets set on a form as:
this.SaveButton2.ItemIds = this.Request["IDs"].Split(',').Select(Int32.Parse).ToArray();

which is a list of the ListItem ids i.e.

EditForm.aspx?ID=140&IDs=140,141,142

Is that enough info?

0 Votes 0 ·
     protected void Page_Load(object sender, EventArgs e)
     {
         SPListItem listItem1 = this.ItemHiddenVersion.ListItem;

         if (listItem1.Folder != null)
         {
             onetIDFolderForm.Visible = true;
             onetIDListForm.Visible = false;
             sharepointListItemEditScript.Visible = false;
         }
         else
         {
             onetIDFolderForm.Visible = false;
             onetIDListForm.Visible = true;
             List<int> itemIds = this.Request["IDs"]?.Split(',').Select(Int32.Parse).ToList();

             if (itemIds?.Count > 1)
             {
                 itemIds.ForEach(delegate (int id)
                 {
                     try
                     {
                         SPListItem listItem = this.SaveButton2.List.GetItemById(id);
                         this.NamesListBox.Items.Add(new ListItem(listItem.Name, id.ToString()));
                     }
                     catch { }
                 });

                 this.SaveButton2.ItemIds = this.Request["IDs"].Split(',').Select(Int32.Parse).ToArray();
                 this.NamePlaceHolder.Visible = false;
                 this.NamesPlaceHolder.Visible = true;
             }
             else
             {
                 this.SaveButton2.ItemIds = new int[0];
                 this.NamePlaceHolder.Visible = true;
                 this.NamesPlaceHolder.Visible = false;
             }
         }
     }
0 Votes 0 ·
Show more comments

Could it be that some value in ItemIds are not the ListItem id?

0 Votes 0 ·

0 Answers