【问题标题】:SharePoint: Script to automatically import documents from a network share on a regular scheduleSharePoint:定期从网络共享中自动导入文档的脚本
【发布时间】:2014-03-14 16:44:38
【问题描述】:

我是 SharePoint 开发的新手。

我正在尝试自动化以下任务:

  • 将文档从网络共享上传到 SharePoint 文档库。
  • 还需要将每个文件的相关元数据放入 SharePoint 列。 (将取自同一位置的单个 CSV 文件,其中映射了文件名)
  • 此过程需要按照预定义的计划自动运行(例如:每天/每小时一次等)

我想知道:

  • 实现这一目标的最佳方法是什么? (在 Windows 任务调度程序上运行的 PowerShell 脚本、创建服务等)
  • 编码的起点或任何已经可用的代码,因为看起来这可能是一个非常常见的要求。

【问题讨论】:

    标签: sharepoint powershell sharepoint-2010 windows-services


    【解决方案1】:

    通常 StackOverflow 不适合提出此类问题。但是,由于我刚刚创建了一个用于将文件从 CSV 上传到 SharePoint 的脚本,所以我想我可以分享它。

    免责声明: 我不保证这将适用于您的环境,或者它不会搞砸。审查。明白它。使用风险自负。

    可以轻松更改脚本以添加/更改列,(在#Add Column Information 部分下)

    CSV 文件格式:

    SubFolder,Name,Column1,Column2
    

    如果您的源文件位于源文件夹的根目录中,则 SubFolder 可以为空。 Name 是文件名,Column1 和 Column2 是 SharePoint 列。

    将以下代码保存到两个 PowerShell 文件中。 ImportSPfromCSV.ps1 包含逻辑,Run_ImportSPfromCSV.ps1 包含运行它的代码。

    要运行,请在 SharePoint 服务器(或具有 SharePoint PowerShell 模块的服务器)上设置计划任务并使用以下命令运行:

    powershell.exe -file "C:\scripts\Run_ImportSPfromCSV.ps1"
    

    Run_ImportSPfromCSV.ps1

        # Load  Script
        . .\ImportSPfromCSV.ps1 
    
        # Run Function
        Import-SPData -CSVFilePath "CSVFile.csv" -SourceFolder "C:\temp" -SiteURL "http://SharePointWebApp/sites/SiteCollection" -Library "Shared Documents" -Approve $true -CheckIn $true -Overwrite $false -OverwriteFields $true
    

    ImportSPfromCSV.ps1

    <#
    .SYNOPSIS
        Imports items into SharePoint library from a CSV File
    .DESCRIPTION
        Imports items that are saved to disk into SharePoint library 
        using and tagging the information from a CSV File
    .NOTES
        NAME:           ImportSPfromCSV.ps1
        AUTHOR:         Kristoph Minchau
        CREATED:        May 2, 2013
        LASTEDIT:   2014-03-06
        VERSION:        1.10
        KEYWORDS:       SharePoint
    
        REVISION LOG:
        [2013/05/01] - km - v1.00 ** Created **
        [2014-03-06] - km - v1.10 * Minor edits
    
    .EXAMPLE
        # Load  Script
        . .\ImportSPfromCSV.ps1 
    
        # Run Function
        Import-SPData -CSVFilePath "CSVFile.csv" -SourceFolder "C:\temp" -SiteURL "http://SharePointWebApp/sites/SiteCollection" -Library "Shared Documents" -Approve $true -CheckIn $true -Overwrite $false -OverwriteFields $true
    .LINK
        Import-SPData
    #>
    
    
    Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
    
    
    
    <#
    .SYNOPSIS
        Get SharePoint file
    .DESCRIPTION
        Gets a SharePoint File
    .PARAMETER SPFileName
        The file name to Upload to the SharePoint library if different than original File name
        ex. "NewName.docx"
        [String]
    .PARAMETER Web
        The SharePoint web site
        [Microsoft.SharePoint.SPWeb]
    .PARAMETER Library
        The Destination Library on SharePoint
        Default value = "Retail Banking"
    .INPUTS
        System.String SPFileName
    .OUTPUTS
        System.String Return SPFile
    .EXAMPLE
        $Web = Get-SPWeb "http://SharePointWebApp/sites/SiteCollection"
        Get-SPFile -Web $Web -Library "Shared Documents" -SPFileName "File1.doc"
    .LINK
        Get-SPFile
    #>
    Function Get-SPFile {
        [CmdletBinding()]
        Param(
            [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
                [string]$SPFileName,
            [Parameter(Position=1, Mandatory=$true)]
                [Microsoft.SharePoint.SPWeb]$Web,
            [Parameter(Position=2, Mandatory=$true)]
                [string]$Library = "Shared Documents"
        ) #end param
        Process
        {
            Try
            {
                # Get Library
                $docLibrary = $web.Lists[$Library]
                $folder = $docLibrary.RootFolder
    
                # Assign the new document name, and build the destination SharePoint path
                $SPFilePath = ($folder.URL + "/" + $SPFileName)
    
                #Check if the file exists and the overwrite option is selected before adding the file
                $spFile = $web.GetFile($SPFilePath)
    
                #Return SP File
                Write-Output $spFile
    
            } #end Try
            Catch
            {
                Write-Error $Error[0]
            }
        } #end Process
    } #end Get-SPFile
    
    
    
    <#
    .SYNOPSIS
        Adds a file to SharePoint Document Library.
    .DESCRIPTION
        Adds a file to SharePoint Document Library, optionally checks it in, approve it, or overwrite it.
    .PARAMETER FilePath
        The file to Upload
        ex. "c:\Test.docx"
        [String]
    .PARAMETER SPFileName
        The file name to Upload to the SharePoint library. 
        Rename the File to something that is SharePoint/URL friendly (e.g. remove special characters "/?=<>" etc.)
        ex. "NewName.docx"
        [String]
    .PARAMETER Web
        The SharePoint web site
        [Microsoft.SharePoint.SPWeb]
    .PARAMETER Library
        The Destination Library on SharePoint
        [String]
    .PARAMETER Approve
        If approval workflow is enabled, when uploading the document, approve the uploaded document
        [bool] Default value = $true
    .PARAMETER CheckIn
        If workflow is enabled, when uploading the document, CheckIn the uploaded document
        [bool] Default value = $true
    .PARAMETER Overwrite
        When uploading the document, if a document with the same name is encountered, overwrite the document?
        [bool] Default value = $false
    .INPUTS
        N/A - Pipeline Inputs
    .OUTPUTS
        SP File
    .EXAMPLE
        $Web = Get-SPWeb "http://SharePointWebApp/sites/SiteCollection"
        Add-SPFileToLibrary -File "Test.docx" -SPFileName "NewName.docx" -Web $Web -Library "Shared Documents" -Approve $true -CheckIn $true -Overwrite $false
    .LINK
        Add-SPFileToLibrary
    #>
    Function Add-SPFileToLibrary {
        [CmdletBinding()]
        Param(
            [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
                [string]$FilePath,
            [Parameter(Position=1, Mandatory=$true)]
                [string]$SPFileName,
            [Parameter(Position=2, Mandatory=$true)]
                [Microsoft.SharePoint.SPWeb]$Web,
            [Parameter(Position=3, Mandatory=$true)]
                [string]$Library,
            [Parameter(Position=4, Mandatory=$true)]
                [bool]$Approve = $true,
            [Parameter(Position=5, Mandatory=$true)]
                [bool]$CheckIn = $true,
            [Parameter(Position=6, Mandatory=$true)]
                [bool]$Overwrite = $false
        ) #end param
        Process
        {
            Try
            {
                # Get Library
                $docLibrary = $web.Lists[$Library]
                $folder = $docLibrary.RootFolder
    
                # Assign the new document name, and build the destination SharePoint path
                $SPFilePath = ($folder.URL + "/" + $SPFileName)
    
                #Check if the file exists and the overwrite option is selected before adding the file
                if ((!$web.GetFile($SPFilePath).Exists) -or ($Overwrite))
                {
                    $Message = "Uploading...   $FilePath   ...To...   $SPFilePath"
                    Write-Debug $Message
                    Write-Verbose $Message
    
                    #Support for -WhatIf parameter
                    if($PsCmdlet.ShouldProcess($Message))
                    {
                        Write-Host $Message
    
                        #Upload file
                        $File = Get-ChildItem $FilePath
                        $spFile = $folder.Files.Add($SPFilePath, $File.OpenRead(), $true)  #Upload with overwrite = $true (SP file path, File stream, Overwrite?)
    
                        $spItem = $spFile.Item
    
                        #Check in file to document library (if required)
                        #MinorCheckIn=0, MajorCheckIn=1, OverwriteCheckIn=2
                        If ($CheckIn) {
                            If ($spFile.CheckOutStatus -ne "None") {
                                If ($Overwrite){
                                    #$spFile.CheckIn("File copied from " + $filePath, 2)
                                    $spFile.CheckIn("Version 1.0", 2)
                                }
                                Else
                                {
                                    #$spFile.CheckIn("File copied from " + $filePath, 1)
                                    $spFile.CheckIn("Version 1.0", 1)
                                }
                                Write-Host "$spfile.Name Checked In"
                            }
                        } #end CheckIn
    
                        #Approve file (if required)
                        If ($Approve)
                        {
                            If ($spItem.ListItems.List.EnableModeration -eq $true)
                            {
                                #$spFile.Approve("File automatically approved after copying from " + $filePath)
                                $spFile.Approve("Approved")
                                If ($spFile.Item["Approval Status"] -eq 0)
                                {
                                    Write-Host "$spfile.Name Approved"
                                }
                                Else
                                {
                                    Write-Host -Background Yellow -Foreground Black "Warning: $spfile.Name Not Approved"
                                }
                            }
                        } #end Approve
    
                        #Return SP File
                        Write-Output $spFile
    
                    }# end upload file
                }
                Else
                {
                    If($web.GetFile($SPFilePath).Exists)
                    {
                        Write-Verbose "File Exists and Overwrite = $false returning file"
                        # File Exists and Overwrite = $false, Try finding and returning the file
                        $spFile = Get-SPFile -Web $web -Library $Library -SPFileName $SPFileName
    
                        #Return SP File
                        Write-Output $spFile
                    }
                    else
                    {
                        # File does not exist, and Overwrite = $false, we can't return anything, Throw Warning
                        Write-Host -Background Yellow -Foreground Black "Warning: $SPFilePath does not Exist and Overwrite is set to False. No SP File handle returned"
                        Write-Output $null
                    }
                }# end File Exists and Overwrite Check
    
            } #end Try
            Catch
            {
                Write-Error $Error[0]
            }
        } #end Process
    } #end Add-SPFileToLibrary
    
    
    
    
    
    <#
    .SYNOPSIS
        Imports files into SharePoint library from a CSV File
    .DESCRIPTION
        Imports files that are saved to disk into SharePoint library 
        using and tagging the information from a CSV File
    .PARAMETER CSVFilePath
        The CSV import file to use to import the data from
        Default value = "CSVFile.csv"
    .PARAMETER SourceFolder
        The Source Folder for locating the Public Folder files
        Default value = "."
    .PARAMETER SiteURL
        The Site URL on SharePoint
        Default value = "http://SharePointWebApp/sites/SiteCollection"
    .PARAMETER Library
        The Destination Library on SharePoint
        Default value = "Shared Documents"
    .PARAMETER Approve
        If approval workflow is enabled, when uploading the document, approve the uploaded document
        [bool] Default value = $true
    .PARAMETER CheckIn
        If workflow is enabled, when uploading the document, CheckIn the uploaded document
        [bool] Default value = $true
    .PARAMETER Overwrite
        When uploading the document, if a document with the same name is encountered, overwrite the document?
        [bool] Default value = $false
    .PARAMETER OverwriteFields
        If the document is already uploaded, overwrite the fields with the fields from the CSV file
        [bool] Default value = $true
    .INPUTS
        N/A - Pipeline Inputs
    .OUTPUTS
        System.String Returns
    .EXAMPLE
        #Default Initial Import of all data
        Import-SPData -CSVFilePath "CSVFile.csv" -SourceFolder "C:\temp" -SiteURL "http://SharePointWebApp/sites/SiteCollection" -Library "Shared Documents" -Approve $true -CheckIn $true -Overwrite $false -OverwriteFields $true
    .LINK
        Import-SPData
    #>
    Function Import-SPData {
        [CmdletBinding()]
        Param(
            [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
                [string]$CSVFilePath = "CSVFile.csv",
            [Parameter(Position=1, Mandatory=$true)]
                [string]$SourceFolder = ".",
            [Parameter(Position=2, Mandatory=$true)]
                [string]$SiteURL = "http://SharePointWebApp/sites/SiteCollection",
            [Parameter(Position=3, Mandatory=$true)]
                [string]$Library = "Shared Documents",
            [Parameter(Position=4, Mandatory=$true)]
                [bool]$Approve = $true,
            [Parameter(Position=5, Mandatory=$true)]
                [bool]$CheckIn = $true,
            [Parameter(Position=6, Mandatory=$true)]
                [bool]$Overwrite = $false,
            [Parameter(Position=7, Mandatory=$true)]
                [bool]$OverwriteFields = $true
        ) #end param
        Process
        {
            Try
            {
                #Get Web
                $web = Get-SPWeb $SiteUrl
    
                Write-Host -Background Black -Foreground Green "Uploading Documents..."
    
                # Loop through the CSV file entries
                Import-Csv $CSVFilePath | ForEach-Object{
    
                    # File Path
                    if($_.Subfolder)
                    {
                        #Subfolder exists
                        $FilePath = ($SourceFolder + "\" + $_.Subfolder + "\" + $_.Name)
                    }
                    else
                    {
                        #Subfolder does not exist
                        $FilePath = ($SourceFolder + "\" + $_.Name)
                    }
    
                    # Assign the new document name, and build the destination SharePoint path
                    $SPFileName = $_.Name
    
                    # Add the file to the SP Library. Don't approve or check in yet because we have to change the column values.
                    $spFile = Add-SPFileToLibrary -FilePath $FilePath -SPFileName $SPFileName -Web $web -Library $Library -Approve $Approve -CheckIn $CheckIn -Overwrite $Overwrite
    
                    if($spFile -And $OverwriteFields)
                    {
                        $spItem = $spFile.Item
    
                        $Message = "Changing column values for $($_.Name) ..."
                        Write-Debug $Message
                        Write-Verbose $Message
    
                        #Support for -WhatIf parameter
                        if($PsCmdlet.ShouldProcess($Message))
                        {
                            Write-Host $Message
    
                            #Add Column Information
                            Try
                            {
                                # Column1
                                Write-Verbose "Setting Column1 to $($_.Column1)"
                                $spItem["Column1"] = $($_.Column1)
    
                                # Column2
                                Write-Verbose "Setting Column1 to $($_.Column2)"
                                $spItem["Column2"] = $($_.Column2)
    
                                #Update document with new column values
                                $spItem.SystemUpdate($false)
    
                                Write-Host "...Complete"
                            }
                            Catch
                            {
                                Write-Error "Error Changing Column information"
                                Write-Error $Error[0]
                            }
                        }#end Change Column values
                    }
                    else
                    {
                        if($OverwriteFields)
                        {
                            # No file handle returned
                            Write-Host -Background DarkRed -ForeGround Yellow "Error: File not uploaded or no File handle returned: $_.Name"
                        }
                        else
                        {
                            # File handle returned, but OverwriteFields = $false
                            Write-Verbose "Not modifying any columns for $_.Name"
                        }
                    }#end if $spFile is set to something and OverwriteFields = $true
                } #end CSV loop
            } #end Try
            Catch
            {
                Write-Error $Error[0]
            }
        } #end Process
    } #end function Import-SPData
    

    【讨论】:

    • 很棒的详细答案!感谢分享!
    • 您能告诉我如何更改脚本上传文档的用户吗?谢谢
    • 不幸的是,使用 SharePoint PowerShell cmdlet,您无法指定要在脚本中使用的用户。它假定运行 PowerShell 提示符的任何人都具有上传文档的适当权限。
    • 这是我在 SO 上看到的最好的答案之一。细节非常好,并且是很好地使用所有 PowerShell 函数参数的特殊道具。我确实从中学到了一些东西。我们确实为所有输入参数使用了一个 XML 文件,但除此之外,这个脚本非常适合我们。
    猜你喜欢
    • 1970-01-01
    • 2017-12-21
    • 2011-09-26
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多