本文介绍了使用 PowerShell 和 OData API 将文件上传到 SharePoint 2010的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件上传到 SharePoint 2010:

I'm attempting to upload a file to SharePoint 2010:

Function Add-Attachments()
{

    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$True)]
        [int]$Id,
        [Parameter(Mandatory=$True)]
        [string[]]$Paths
    )

    BEGIN {}
    PROCESS {

        $url = "http://server/resource/_vti_bin/listdata.svc/TheList($Id)/Attachments"

        Foreach ($Path in $Paths) {

            Write-Verbose "Attaching $Path ..."
            $headers = @{
                'Slug' = "TheList|$Id|$(Split-Path $path -Leaf)"
            }

            $Payload = @{filename=(Split-Path $path -Leaf);filecontent=([IO.File]::ReadAllBytes($path))}

            Invoke-WebRequest -Uri $url -Method Post -UseDefaultCredentials -Body $Payload -Headers $headers

        } # Foreach

    } # PROCESS
    END {}

}

Add-Attachments -Id 1234 -Paths 'C:UsersgandalfDesktop	est.txt' -verbose

我收到一条错误消息:

调用-WebRequest: 发生错误处理这个请求.在C:UsersgandalfDocumentsWindowsPowerShellScriptsSPSharePoint2010.ps1:382字符:13+ Invoke-WebRequest -Uri $url -Method Post -UseDefaultCredentials -Bod ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest],网络异常+ FullQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

** 编辑 **

文件的原始内容:

Lorem ipsum dolor 坐 amet,consectetur adipiscing 精英.Donec adiam莱克图斯.sed 坐 amet ipsum mauris.Maecenas congue ligula ac quamviverra nec consectetur ante hendrerit.Donec et mollis dolor.Praesent et diam eget libero egestas mattis 坐在 amet vitae augue.南tincidunt congue enim, ut porta lorem lacinia consectetur.完成libero sed arcu vehicula ultricies 非侵权人.Lorem ipsum dolor 坐amet, consectetur adipiscing 精英.Aenean ut gravida lorem.乌龟felis, pulvinar a semper sed, adipiscing id dolor.佩伦特式拍卖师nisi id magna consequat sagittis.Curabitur dapibus enim 坐 amet elitpharetra tincidunt feugiat nisl imperdiet.Ut convallis libero in urnaultrices accumsan.Donec sed odio eros.Donec viverra mi quis quam在malesuada arcu rhoncus 的pulvinar.暨社会 natoque penatibus etmagnis dis parturient montes, nascetur ridiculus mus.在鲁鲁姆accumsan ultricies.Mauris vitae nisi at sem facilisis semper ac in估计

该脚本确实在服务器上创建了一个文件,但内容如下:

The script does create a file on the server, but with this content:

filename=test.txt&filecontent=Lorem+ipsum+dolor+sit+amet%2c+consectetur+adipiscing+elit.+Donec+a+diam+lectus.+Sed+sit+amet+ipsum+mauris.+Maecenas+congue+ligula+ac+quam+viverra+nec+consectetur+ante+hendrerit.+Donec+et+mollis+dolor.+Praesent+et+diam+eget+libero+egestas+mattis+sit+amet+vitae+augue.+Nam+tincidunt+congue+enim%2c+ut+porta+lorem+lacinia+consectetur.+Donec+ut+libero+sed+arcu+vehicula+ultricies+a+non+tortor.+Lorem+ipsum+dolor+sit+amet%2c+consectetur+adipiscing+elit.+Aenean+ut+gravida+lorem.+Ut+turpis+felis%2c+pulvinar+a+semper+sed%2c+adipiscing+id+dolor.+Pellentesque+auctor+nisi+id+magna+consequat+sagittis.+Curabitur+dapibus+enim+sit+amet+elit+pharetra+tincidunt+feugiat+nisl+imperdiet.+Ut+convallis+libero+in+urna+ultrices+accumsan.+Donec+sed+odio+eros.+Donec+viverra+mi+quis+quam+pulvinar+at+malesuada+arcu+rhoncus.+Cum+sociis+natoque+penatibus+et+magnis+dis+parturient+montes%2c+nascetur+ridiculus+mus.+In+rutrum+accumsan+ultricies.+Mauris+vitae+nisi+at+sem+facilisis+semper+ac+in+es​​t.

我错过了什么?我需要包括内容长度吗?设置 MIME 类型?

What am I missing? Do I need to include the content length? Set the MIME type?

推荐答案

为了创建附件资源,必须指定以下属性:

In order to create an attachment resource the following properties have to be specified:

Endpoint Uri: http://server/site/_vti_bin/listdata.svc/entityset(itemid)/Attachments
Method: POST
Headers:
   Slug: "entityset|itemid|name"
   ContentType: */*
Body: content

话虽如此,我的结论是指定的 body 参数 ($payload) 在提供的示例中无效.

Having said that, my conslution that the specified body parameter ($payload) is invalid in the provided example.

以下示例演示了如何通过 SharePoint 2010 REST 接口上传附件文件:

The following example demonstrates how to upload attachment file via SharePoint 2010 REST Interface:

Function Add-Attachments()
{

    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$True)]
        [string]$WebUrl,
        [Parameter(Mandatory=$True)]
        [string]$ListName,
        [Parameter(Mandatory=$True)]
        [int]$ItemId,
        [Parameter(Mandatory=$True)]
        [string]$SourcePath
    )

    BEGIN {}
    PROCESS {
        $endpointUri = New-Object System.Uri("$WebUrl/_vti_bin/listdata.svc/$ListName($ItemId)/Attachments")
        $fileName = (Split-Path $SourcePath -Leaf)
        $fileContent = ([IO.File]::ReadAllBytes($SourcePath))
        $headers = @{
                 'Slug' = "$ListName|$ItemId|$fileName";
        }

        Invoke-WebRequest -Uri $endpointUri -Method Post -UseDefaultCredentials -Body $fileContent -Headers $headers -ContentType "*/*"

    } # PROCESS
    END {}

}

用法:

Add-Attachments -WebUrl "http://contoso.intranet.com/" -ListName "Tasks" -ItemId 1 -SourcePath "C:UsersuserDocumentsSharePointUserGuide.docx" -verbose

更新

通过 Fiddler 进行一些分析后,确定正确的端点 url 应该是:

After performing some analysis via Fiddler, it was determined that the proper endpoint url should be:

/_vti_bin/listdata.svc/Attachments HTTP/1.1

代替:

/_vti_bin/listdata.svc/Tasks(<id>)/Attachments HTTP/1.1

修改后的例子

Function Add-Attachments()
{

    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$True)]
        [string]$WebUrl,
        [Parameter(Mandatory=$True)]
        [string]$ListName,
        [Parameter(Mandatory=$True)]
        [int]$ItemId,
        [Parameter(Mandatory=$True)]
        [string]$SourcePath
    )

    BEGIN {}
    PROCESS {
        $endpointUri = New-Object System.Uri("$WebUrl/_vti_bin/listdata.svc/Attachments")
        $fileName = (Split-Path $SourcePath -Leaf)
        $fileContent = ([IO.File]::ReadAllBytes($SourcePath))
        $headers = @{
                 'Slug' = "$ListName|$ItemId|$fileName";
        }

        Invoke-WebRequest -Uri $endpointUri -Method Post -UseDefaultCredentials -Body $fileContent -Headers $headers -ContentType "*/*"

    } # PROCESS
    END {}

}

这篇关于使用 PowerShell 和 OData API 将文件上传到 SharePoint 2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-10 01:02