0

I am working on remote event receiver, and i have this code:-

Folder folder0 = context.Web.GetFolderByServerRelativeUrl(context.Web.ServerRelativeUrl + "/library3/" + currentFilingSystemItem["DealName"].ToString());
context.Load(folder0.ListItemAllFields);
context.ExecuteQuery();
ListItem folderItem0 = folder0.ListItemAllFields;
if (folderItem0["SourceListItemID"].ToString() == listItemID.ToString())
{
Folder folder = context.Web.GetFolderByServerRelativeUrl(context.Web.ServerRelativeUrl + "/library3/" + currentFilingSystemItem["DealName"].ToString());
folder.MoveTo(context.Web.ServerRelativeUrl + "/ArchDocs/" + currentFilingSystemItem["DealName"].ToString());
folder.Update();
//code goes here..
ListItem folderItem = folder.ListItemAllFields;
context.Load(folderItem);//this will raise the error

so can anyone advice what is wrong with my code?

context.ExecuteQuery();

  • 1
    If you comment out the last context.load, does the MoveTo succeed? – willman Feb 26 at 23:36
  • 1
    Did you make a cut and paste mistake? The path of the folder you are getting inside your conditional is exactly the same as folder0. If that is intentional, you don't need to get the folder again, just use folder0. – willman Feb 26 at 23:38
  • @willman yes i have 2 references to the same folder, as i want to make sure that the second reference is for the moved folder... in all the cases why i am getting an error on my code? – SharePoint TestDev Feb 27 at 0:55
1

Try doing the MoveTo on your original reference, then after executing, grab the newly moved folder by its new path. Trying to perform an operation on a reference that is still pointing to the old location is likely the problem. Try this:

Folder folder0 = context.Web.GetFolderByServerRelativeUrl(context.Web.ServerRelativeUrl + "/library3/" + currentFilingSystemItem["DealName"].ToString());
context.Load(folder0.ListItemAllFields);
context.ExecuteQuery();
ListItem folderItem0 = folder0.ListItemAllFields;
if (folderItem0["SourceListItemID"].ToString() == listItemID.ToString())
{
    folder0.MoveTo(context.Web.ServerRelativeUrl + "/ArchDocs/" + currentFilingSystemItem["DealName"].ToString());
    folder0.Update();
    context.ExecuteQuery();

    Folder folder = context.Web.GetFolderByServerRelativeUrl(context.Web.ServerRelativeUrl + "/ArchDocs/" + currentFilingSystemItem["DealName"].ToString());
    context.Load(folder);
    context.ExecuteQuery();

    //code goes here..
    ListItem folderItem = folder.ListItemAllFields;
    context.Load(folderItem);
    context.ExecuteQuery();
}

Also, since attempting to do any operations on folder0 after it is moved would likely throw the same error, instead of getting a whole new reference, you can just updated folder0 to point to the new folder location, like this:

Folder folder0 = context.Web.GetFolderByServerRelativeUrl(context.Web.ServerRelativeUrl + "/library3/" + currentFilingSystemItem["DealName"].ToString());
context.Load(folder0.ListItemAllFields);
context.ExecuteQuery();
ListItem folderItem0 = folder0.ListItemAllFields;
if (folderItem0["SourceListItemID"].ToString() == listItemID.ToString())
{
    folder0.MoveTo(context.Web.ServerRelativeUrl + "/ArchDocs/" + currentFilingSystemItem["DealName"].ToString());
    folder0.Update();
    context.ExecuteQuery();

    folder0 = context.Web.GetFolderByServerRelativeUrl(context.Web.ServerRelativeUrl + "/ArchDocs/" + currentFilingSystemItem["DealName"].ToString());
    context.Load(folder0);
    context.ExecuteQuery();

    //code goes here..
    ListItem folderItem = folder0.ListItemAllFields;
    context.Load(folderItem);
    context.ExecuteQuery();
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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