1
votes

I'm trying to create a link to a document in SharePoint 2010 using PowerShell 2.0. I've already enabled other content types and added the 'Link to a Document' content type to the document library in question.

The document that I'm trying to link to is in a different shared document list on another web in the same site collection. The actual file is nested in a subfolder called "PM". The file types may range from excel files to word files to PDFs.

I've tested the process manually (Shared Documents -> New Document -> Link to a Document -> ...) and it worked fine (as was indicated by the document icon with an arrow over the bottom right corner when I was done), but I cannot seem to get it to work with PowerShell. Any ideas?

This is the only non-PowerShell solution that I've come accross so far: http://howtosharepoint.blogspot.com/2010/05/programmatically-add-link-to-document.html

1

1 Answers

0
votes

I got it working finally by porting the aforementioned solution. There's superfluous detail here, but the gist of it is easy enough to parse out:

# Push file links out to weekly role page
$site = New-Object Microsoft.SharePoint.SPSite($roleURL)
$web = $site.OpenWeb()
$listName = "Shared Documents"
$list = $web.Lists[$listName]
$fileCollection = $list.RootFolder.files
ForEach ($doc in $docLoadList) {
    $parsedFileName = $d.Split("\")
    $index = $parsedFileName.Length
    $index = $index - 1
    $actualFileName = $parsedFileName[$index]
    $existingFiles = Get-ExistingFileNames $homeURL
    If ($existingFiles -Match $actualFileName) {
        $existingFileObject = Get-ExistingFileObject $actualFileName $homeURL
        $docLinkURL = Get-ExistingFileURL $actualFileName $homeURL
        # Create new aspx file
        $redirectFileName = $actualFileName
        $redirectFileName += ".aspx"
        $redirectASPX = Get-Content C:\Test\redirect.aspx
        $redirectASPX = $redirectASPX -Replace "#Q#", $docLinkURL
        $utf = new-object System.Text.UTF8Encoding
        $newFile = $fileCollection.Add($redirectFileName, $utf.GetBytes($redirectASPX), $true)
        # Update the newly added file's content type
        $lct = $list.ContentTypes["Link to a Document"]
        $lctID = $lct.ID
        $updateFile = $list.Items | ? {$_.Name -eq $redirectFileName}
        $updateFile["ContentTypeId"] = $lctID
        $updateFile.SystemUpdate()
        $web.Dispose()
    }
}

I may end up stringing together the .aspx file in the script too at some point...