当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 在 SharePoint 文档库中创建文件夹

作者:精品下载站 日期:2024-12-14 21:00:37 浏览:14 分类:玩电脑

PowerShell 在 SharePoint 文档库中创建文件夹


要求:在 SharePoint Server 文档库中创建一个新文件夹。

如何使用 PowerShell 在 SharePoint 文档库中添加文件夹?

通过使用 PowerShell,您可以在 SharePoint 中快速创建文件夹。本博文将了解如何使用 PowerShell 在 SharePoint 文档库中创建新文件夹。

要在 SharePoint Server 的文档库中创建文件夹,请使用以下 PowerShell 脚本:


$folder = $list.Folders.Add("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "New")
$folder.Update()

上面的脚本没有提供太多的容错能力,不是吗?

  • 如果我们正在寻找的特定库不存在怎么办?
  • 如果我们尝试添加的文件夹已存在于库中怎么办?

错误!因此,让我们在 PowerShell 中重新编写具有很少容错能力的代码!

[玩转系统] PowerShell 在 SharePoint 文档库中创建文件夹

如何使用 PowerShell 在 SharePoint 列表中创建文件夹和子文件夹?

以下是用于在 SharePoint 列表或库中创建文件夹和子文件夹的 PowerShell 代码片段:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Get the Web
$web=Get-SPWeb "https://sharepoint.company.com"
#Get the List/Library
$list=$web.Lists.TryGetList("SalesList")

If($list)  #Check If List exists!
{
    #Check Folder Doesn't exists in the Library!
    $folder = $list.ParentWeb.GetFolder($list.RootFolder.Url + "/" +"Sales List");
    #sharepoint powershell check if folder exists
    If($folder.Exists -eq $false)
    {
       #Create a Folder
       $folder = $list.AddItem([string]::Empty, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "Sales Sub-Item")
       $folder.Update();
    }
   
    #Create a Sub-Folder "APAC Sales Documents" inside "Sales Documents"    
    $Subfolder = $list.ParentWeb.GetFolder($folder.URL + "/" + "APAC Sales Documents");
    
    #Check if sub-folder doesn't exist already
    If ($Subfolder.Exists -eq $false)
    {
       #Create a Sub-Folder Inside "Sales Documents"
       $Subfolder = $list.AddItem($folder.URL, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "APAC Sales Documents")
       $Subfolder.Update();
    }
}

如何确保 SharePoint 库中的嵌套文件夹结构?例如。库/文件夹A/文件夹AA/文件夹AAA


#Get the Web
$web=Get-SPWeb "https://sharepoint.company.com"

#Get the List/Library
$list=$web.Lists.TryGetList("Documents")

#Function to Ensure FolderPath
Function Ensure-FolderPath($FolderPath)
{
    $SubFolders = $FolderPath -split "/"
    $RootFolder =  $SubFolders[0]
    $SubfolderPath = $RootFolder

    For($i =1; $i -le $SubFolders.count-1; $i++)
    {        
        $SubfolderPath = $SubfolderPath+"/"+$SubFolders[$i]
        $Folder = $Web.GetFolder($SubfolderPath)

        #Create Folder if it doesn't exists
        If($Folder.Exists -eq $false)
        {
            $TargetFolder = $Web.Folders.Add($SubfolderPath)
            Write-host "Folder Created:"$SubfolderPath
        }
    }
}

#Call the function to Ensure Folder Path
Ensure-FolderPath "Documents/Technical Design/Assorted/2020"

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯