本文介绍了将 SharePoint 文件夹和内容移动到同一文档库中的不同位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在寻找一种使用 SharePoint 2010 客户端对象模型 (C#) 将文件夹及其所有内容移动到同一库中不同位置的方法.

I'm looking for a way to move a folder and all it's contents to a different location in the same library using the Client Object Model for SharePoint 2010 (C#).

例如,我们有一个项目文件夹(比如 12345),它的 URL 是

For example we have a folder for a project (say 12345) and it's URL is

http://sharepoint/site/library/2012/12345

其中 2012 年代表一年.我想以编程方式将 12345 文件夹移动到不同的年份,比如 2014 年,它可能已经存在但可能不存在.

where 2012 represents a year. I'd like to programmatically move the 12345 folder to a different year, say 2014 which probably exists already but may not.

我已经四处搜索,但我得到的解决方案似乎非常复杂,并且与将文件夹移动到不同的网站集相关,我希望因为它在同一个库中,所以可能有更简单的解决方案?我的一个想法是依赖 Explorer View 而不是 CSOM?

I've searched around but the solutions I'm getting seem extremely complicated and relevant to moving folders to different site collections, I'm hoping because it's in the same library there might be a simpler solution? One idea I have is to rely on Explorer View instead of CSOM?

非常感谢!

推荐答案

SharePoint CSOM API 中没有用于将带有文件的文件夹从一个位置移动到另一个位置的内置方法.

There is no built-in method in SharePoint CSOM API for moving Folder with Files from one location into another.

以下类表示如何将文件从源文件夹移动到目标文件夹:

The following class represents how to move files from source folder into destination folder:

public static class FolderExtensions
{


    public static void MoveFilesTo(this Folder folder, string folderUrl)
    {
        var ctx = (ClientContext)folder.Context;
        if (!ctx.Web.IsPropertyAvailable("ServerRelativeUrl"))
        {
            ctx.Load(ctx.Web, w => w.ServerRelativeUrl);
        }
        ctx.Load(folder, f => f.Files, f => f.ServerRelativeUrl, f => f.Folders);
        ctx.ExecuteQuery();

        //Ensure target folder exists
        EnsureFolder(ctx.Web.RootFolder, folderUrl.Replace(ctx.Web.ServerRelativeUrl, string.Empty));
        foreach (var file in folder.Files)
        {
            var targetFileUrl = file.ServerRelativeUrl.Replace(folder.ServerRelativeUrl, folderUrl);
            file.MoveTo(targetFileUrl, MoveOperations.Overwrite);
        }
        ctx.ExecuteQuery();

        foreach (var subFolder in folder.Folders)
        {
            var targetFolderUrl = subFolder.ServerRelativeUrl.Replace(folder.ServerRelativeUrl,folderUrl);
            subFolder.MoveFilesTo(targetFolderUrl);
        }
    }


    public static Folder EnsureFolder(Folder parentFolder, string folderUrl)
    {
        var ctx = parentFolder.Context;
        var folderNames = folderUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
        var folderName = folderNames[0];
        var folder = parentFolder.Folders.Add(folderName);
        ctx.Load(folder);
        ctx.ExecuteQuery();

        if (folderNames.Length > 1)
        {
            var subFolderUrl = string.Join("/", folderNames, 1, folderNames.Length - 1);
            return EnsureFolder(folder, subFolderUrl);
        }
        return folder;
    }
}

要点:

  • 允许确保目标文件夹是否存在
  • 如果是嵌套文件夹,则在移动文件时保留其结构

使用

var srcFolderUrl = "/news/pages";
var destFolderUrl = "/news/archive/pages";
using (var ctx = new ClientContext(url))
{
    var sourceFolder = ctx.Web.GetFolderByServerRelativeUrl(srcFolderUrl);
    sourceFolder.MoveFilesTo(destFolderUrl);
    sourceFolder.DeleteObject(); // delete source folder if nessesary
    ctx.ExecuteQuery();
}

这篇关于将 SharePoint 文件夹和内容移动到同一文档库中的不同位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:04