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 :-)
string websiteUrl = "http://something.com";
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).