But what if you have to move items through folders in a list? Just look at this, maybe you'll find this useful: this is based on a simple concept, a list item is nothing but a "file", so you can easily do the work like this.
A special thank to Alessio Pau who wrote this code :-)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | string expectedFolderName = "expectedFolder" ; using (var ctx = new ClientContext(websiteUrl)) { Web web = ctx.Web; ctx.Load(web); ctx.ExecuteQuery(); string currentFolderName = item[ "FileDirRef" ].ToString(); string itemIDForURL = item.Id + "_.000" ; string itemURL = currentFolderName + "/" + itemIDForURL; // Check if the item is in the correct folder, if not, do the following if (!currentFolderName.EndsWith( string .Join( "/" , expectedFolderName))) { // This is just a custom method which creates the folder tree var correctFolder = CreateMultipleLevelFolder(expectedFolderName, listName); // Move the file Microsoft.SharePoint.Client.File itemFile = ctx.Web.GetFileByServerRelativeUrl(itemURL); ctx.Load(itemFile); ctx.ExecuteQuery(); itemFile.MoveTo(correctFolder.ServerRelativeUrl + "/" + itemIDForURL, MoveOperations.Overwrite); ctx.ExecuteQuery(); } } |
0 commenti:
Post a Comment
Because of a lot of SPAM about courses, I need to moderate all comments here.
I ensure you that I will answer whenever possible (if you are not a spammer).