使用Powershell上载文件并将信息插入到自定义列到Sharepoint库

时间:2014-09-22 15:06:11

标签: powershell sharepoint

我正在学习powershell来创建一个脚本,将文件上传到SharePoint内的子站点库。我设法让它工作并将.doc文件上传到我的库,但是我也想要同时填写其他列中指定的元数据。我没有使用SharePoint管理单元,而是使用webclient功能。这是简单的powershell脚本

# create the Variable Path and Pass the source folder path

$path = "THE FILE"

# create the Variable destination and pass the URL of the SharePoint List
$destination = "SharePoint SubSite"

# Store the current user default credentials in the Variable Credentials
$credentials = [System.Net.CredentialCache]::DefaultCredentials;

# Create the object of the Webclient 

$webclient = New-Object System.Net.WebClient;

$webclient.Credentials = $credentials;

$webclient.UploadFile($destination + “/” + "Filename", “PUT”, TheFile)

此代码有效,但我不知道如何使用webclient函数将元数据传递给自定义列。上传文件时,我注意到只更新了前两列,但自定义字段留空。

如果可能,请告诉我。

非常感谢你的帮助

1 个答案:

答案 0 :(得分:1)

这里基本上有三个选项(假设使用了SharePoint 2010):

如何通过PowerShell使用SharePoint 2010 REST接口

以下函数演示了如何使用SharePoint 2010 REST界面执行CRUD操作:

<#
.Synopsis
    Sends an HTTP or HTTPS request to a SharePoint 2010 REST-compliant web service.
.DESCRIPTION
    This function sends an HTTP or HTTPS request to a Representational State
    Transfer (REST)-compliant ("RESTful") SharePoint Online web service.
.EXAMPLE
   Invoke-SPRestMethod -Url "https://contoso.sharepoint.com/_vti_bin/ListData.svc/Projects"
#>
Function Invoke-SPRestRequest()
{
Param(
[Parameter(Mandatory=$True)]
[String]$WebUrl,

[Parameter(Mandatory=$True)]
[String]$ListName,

[Parameter(Mandatory=$False)]
[int]$ItemId,

[Parameter(Mandatory=$False)]
[String]$QueryOptions,

[Parameter(Mandatory=$False)]
[Microsoft.PowerShell.Commands.WebRequestMethod]$Method = [Microsoft.PowerShell.Commands.WebRequestMethod]::Get,

[Parameter(Mandatory=$False)]
[System.Net.ICredentials]$Credentials,

[Parameter(Mandatory=$False)]
[String]$Payload,

[Parameter(Mandatory=$False)]
[String]$ETag,

[Parameter(Mandatory=$False)]
[String]$XHTTPMethod,

[Parameter(Mandatory=$False)]
[System.String]$Accept = "application/json;odata=verbose",

[Parameter(Mandatory=$False)]
[String]$ContentType = "application/json;odata=verbose"
)

    #Construct Endpoint URL 
    $endpointUrl = $WebUrl + "/_vti_bin/listdata.svc/" + $ListName
    if($ItemId){
        $endpointUrl = $endpointUrl + "(" + $ItemId + ")"
    }
    if($QueryOptions){
        $endpointUrl = $endpointUrl + $QueryOptions
    }


    $client = New-Object System.Net.WebClient
    if($Credentials) {
        $client.Credentials = $Credentials
    } 
    $client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
    $client.Headers.Add("Content-Type",$ContentType)
    $client.Headers.Add("Accept",$Accept)
    if($Method -eq "Get") {
        $result = $client.DownloadString($endpointUrl) | ConvertFrom-Json
    }
    elseif ($Method -eq "Post") {
        if($ETag) { 
           $client.Headers.Add("If-Match", $ETag)
        } 
        if($XHTTPMethod) { 
            $client.Headers.Add("X-HTTP-Method", $XHTTPMethod)
        }
        if($Payload) {
             $client.UploadString($endpointUrl,$Method,$Payload) 
        }
        else {
             $client.UploadString($endpointUrl,$Method) 
        }   
    }
    $client.Dispose()
    return $result
}

要点:Invoke-SPRestRequest.ps1

实施例

以下示例演示如何将文件上载到SharePoint 2010并设置元数据属性。它包括:

  1. 上传文件操作
  2. 查找与上传文件相关联的上传列表项
  3. 更新列表项属性
  4. 代码:

    $UserName = "username"
    $Password = Read-Host -Prompt "Enter the password"    
    $WebUrl = "https://contoso.sharepoint.com/project"
    $FolderUrl = "/project/Shared Documents/Archive"
    
    
    Function Find-ListItem([string]$WebUrl,[System.Net.ICredentials]$Credentials,[string]$ListName,[string]$QueryOptions)
    {
        $result = Invoke-SPRestRequest -WebUrl $WebUrl -ListName $ListName -QueryOptions $QueryOptions -Credentials $Credentials 
        return $result.d.results
    }
    
    
    
    Function Update-ListItem([string]$WebUrl,[System.Net.ICredentials]$Credentials,[string]$ListName,[int]$ItemId)
    {
        $itemPayload = @{ 
           "DocumentStatusValue" = "Close"; 
        } | ConvertTo-Json
        Invoke-SPRestRequest -WebUrl $WebUrl -ListName $ListName -ItemId $ItemId -Credentials $Credentials -Method Post -Payload $itemPayload -ETag "*" -XHTTPMethod "MERGE"
    }
    
    
    Function Upload-File([string]$WebUrl,[System.Net.ICredentials]$Credentials,[string]$FolderUrl, [string]$FileName)
    {
        $client = New-Object System.Net.WebClient
        #$client.Credentials = $Credentials
        $client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
    
        $siteUri = New-Object Uri($WebUrl)
        $fileUri = New-Object Uri($siteUri, ($FolderUrl + "/" + [System.IO.Path]::GetFileName($FileName)))
    
        $result = $client.UploadFile($fileUri, "PUT", $FileName)
        $client.Dispose()
    }
    
    
    
    
    #1.Upload a File
    Upload-File -WebUrl $WebUrl -FolderUrl $FolderUrl  -FileName "D:\tmp\SharePoint User Guide.docx"
    
    #2.Find an associated List Item for a File 
    $query = "?`$filter=Path eq '" + $FolderUrl + "'"
    $ListItem = Find-ListItem -WebUrl $WebUrl  -ListName "Documents" -QueryOptions $query
    
    #3.Update List Item properties
    Update-ListItem -WebUrl $WebUrl -ListName "Documents" -ItemId $ListItem.Id
    

    参考

    SharePoint Foundation REST Interface