【问题标题】:How can I access a sharepoint url-based directory from C#?如何从 C# 访问基于 url 的共享点目录?
【发布时间】:2020-07-24 20:21:37
【问题描述】:

我可以通过将此 url 放入文件资源管理器来访问 sharepoint 文件夹:

http://wss.sharepointsite.com/sites/tech/Lists/email/Attachments

如果我把它放在浏览器中,它只会将我重定向回技术主页。

我试过这个,只是得到一个“路径格式不正确”的错误。

DirectoryInfo di = new DirectoryInfo("http://wss.sharepointsite.com/sites/tech/Lists/email/Attachments");

我需要能够遍历每个文件夹和文件以创建用于搜索的数据库。

【问题讨论】:

  • 我相信您可能需要查看 Sharepoint API CSOM 并使用 API 进行调用。有一些方法可以遍历 Sharepoint 列表。
  • 这能回答你的问题吗? iterate a sharepoint list

标签: c# sharepoint directory


【解决方案1】:

您可以使用 SharePoint Online CSOM 库编写 PowerShell 脚本来下载列表的附件:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
Function Download-ListAttachments()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $DownloadDirectory
    )   
   Try {
        #Setup Credentials to connect
        $Cred = Get-Credential
        $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
     
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred
 
        #Get All Items from the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()
         
        #Create download directory if it doesn't exist
        If (!(Test-Path -path $DownloadDirectory))       
        {           
            New-Item $DownloadDirectory -type directory         
        }
         
        #Iterate through each list item
        Foreach($Item in $ListItems)
        {
            #Get All attachments from the List Item
            $AttachmentsColl = $Item.AttachmentFiles
            $Ctx.Load($AttachmentsColl)
            $Ctx.ExecuteQuery()
 
            #Get attachment for each list item
            ForEach($Attachment in $AttachmentsColl)
            {
                #Download attachment
                $FileContent = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Ctx, $Attachment.ServerRelativeUrl)
                $FileName= $DownloadDirectory+$Item.id.ToString()+"_"+$Attachment.FileName
                $FileStream = [System.IO.File]::Create($FileName)
                $FileContent.Stream.CopyTo($FileStream)
                $FileStream.Close()
           }
        }
 
        write-host  -f Green "List Attachments Downloaded Successfully!"
    }
    Catch {
        write-host -f Red "Error Downloading List Attachments!" $_.Exception.Message
    }
}
 
#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$ListName="Projects"
$DownloadDirectory="C:\Temp\"
 
#Call the function to copy list items
Download-ListAttachments -SiteURL $SiteURL -ListName $ListName -DownloadDirectory $DownloadDirectory

在下载文件夹中,将包含带有“1,2,3,4...”的子文件夹,代表 ListItem Id:

参考:

SharePoint Online: Download Attachments from List using PowerShell

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    相关资源
    最近更新 更多