在Sharepoint中的嵌套文件夹下创建文件

本文关键字:创建 文件 文件夹 嵌套 Sharepoint | 更新日期: 2023-09-27 18:29:09

在Sharepoint中的嵌套文件夹下创建文件的最佳方法是什么?我当前的方法

public string CreateSPFile(string spServerURL, string spDocumentLibraryURL, string folder, string fileName, Stream fileStream, bool overwrite)
{
    if (SPSite.Exists(new Uri(spServerURL)))
    {
        SPSite site = new SPSite(spServerURL);
        SPWeb oWebsite = site.OpenWeb();
        oWebsite.AllowUnsafeUpdates = true;
        SPFolder spFolder = oWebsite.Folders[spDocumentLibraryURL];
        if (!string.IsNullOrEmpty(folder))
        {
            spFolder.SubFolders[folder].Files.Add(fileName, fileStream, overwrite);
        }
        else
        {
            SPFileCollection files = spFolder.Files;
            spFolder.Files.Add(fileName, fileStream, overwrite);
        }
        oWebsite.AllowUnsafeUpdates = false;
        site.Close();
    }
}

正如你所看到的,如果我想在嵌套文件夹下创建一个文件,我需要修改我的代码。有什么更好的方法来处理这种保存嵌套文件夹的情况

根据我的项目结构,该文件可以像/DocumentLibrary/Folder1/Folder2/Folder3/file.txt。

在Sharepoint中的嵌套文件夹下创建文件

您可以通过与服务器相关的URL:加载文件夹

SPFolder folder = web.GetFolder("/DocumentLibrary/Folder1/Folder2/Folder3/");

使用这种方法,您不必逐个文件夹加载,并且您的代码适用于n个文件夹级别。

我已经更新了您的代码示例,并添加了一些关于SharePoint最佳实践的评论:

public string CreateSPFile(string spServerURL, string spDocumenttargetUrl, string folder, string fileName, Stream fileStream, bool overwrite)
{
    // I suggest skip this pre check since it internally opens a new site object
    // If you have to silenlty ignore non-existant SPSite you should catch a FileNotFoundException.
    if (SPSite.Exists(new Uri(spServerURL)))
    {
        // use the using construct to safely dispose the opened SPSite object
        using (SPSite site = new SPSite(spServerURL))
        {
            // SPWeb object opened with SPSite.OpenWeb() have to be disposed as well
            using (SPWeb web = site.OpenWeb())
            {
                web.AllowUnsafeUpdates = true;
                string targetUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, spDocumenttargetUrl);
                if (!String.IsNullOrEmpty(folder))
                {
                    targetUrl = SPUrlUtility.CombineUrl(targetUrl, folder);
                }
                SPFolder target = web.GetFolder(target);
                SPFileCollection files = target.Files;
                target.Files.Add(fileName, fileStream, overwrite);
                // no need to revert AllowUnsafeUpdates for newly opened webs
                // web.AllowUnsafeUpdates = false;
            }
        }
    }
}

要将文件上传到嵌套文件夹中,可以考虑以下方法:

  • 使用下面提供的方法EnsureFolder确保目标文件夹存在
  • 使用SPFileCollection.Add方法上载文件

如何使用SharePoint SSOM确保嵌套文件夹存在

internal static class SPWebExtensions
{
    /// <summary>
    /// Ensure SPFolder
    /// </summary>
    /// <param name="web"></param>
    /// <param name="listTitle"></param>
    /// <param name="folderUrl"></param>
    /// <returns></returns>
    public static SPFolder EnsureFolder(this SPWeb web, string listTitle, string folderUrl)
    {
        if (string.IsNullOrEmpty(folderUrl))
            throw new ArgumentNullException("folderUrl");
        var list = web.Lists.TryGetList(listTitle);
        return CreateFolderInternal(list, list.RootFolder, folderUrl);
    }

    private static SPFolder CreateFolderInternal(SPList list, SPFolder parentFolder, string folderUrl)
    {
        var folderNames = folderUrl.Split(new char[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
        var folderName = folderNames[0];
        var curFolder =
            parentFolder.SubFolders.Cast<SPFolder>().FirstOrDefault( f => System.String.Compare(f.Name, folderName, System.StringComparison.OrdinalIgnoreCase) == 0);
        if (curFolder == null)
        {
            var folderItem = list.Items.Add(parentFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder,
                                            folderName);
            folderItem.SystemUpdate();
            curFolder = folderItem.Folder;
        }

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

Gist:EnsureFolder.cs


以下示例演示了如何确保Documents库下存在以下文件夹结构,并将文件上载到其中:

Orders
     |
     A --
        |
        A1

示例:

var targetFolder = web.EnsureFolder("Documents", "Orders3/A/A1");
var fileContent = System.IO.File.ReadAllBytes(fileName);
var fileUrl = Path.GetFileName(fileName);
targetFolder.Files.Add(fileUrl, fileContent);