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

时间:2012-03-27 05:58:58

标签: c# sharepoint

在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。

2 个答案:

答案 0 :(得分:3)

您可以按服务器相对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;
            }
        }
    }
}

答案 1 :(得分:2)

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

如何使用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;
    }
}

要点: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);