0

I am needing to add the LinkFilenameNoMenu ("Name(linked to document with edit menu") column to a view within a library on multiple SharePoint sites. This is what I have:

Add-PSSnapin Microsoft.SharePoint.PowerShell

$InvFile="ProjectURLs2.csv" 
# Get Data from Inventory CSV File 
$FileExists = (Test-Path $InvFile -PathType Leaf) 
if ($FileExists) { 
   "Loading $InvFile for processing..." 
   $tblData = import-csv $InvFile 
} else { 
   "$InvFile not found - stopping import!" 
  exit 
}

foreach ($row in $tblData)
{
$ProjectURL = $row."ProjectURL".ToString()

$web = Get-SPWeb "$ProjectURL"

$list = $web.Lists["Document Library"]

$View = $List.Views["All Documents without Folders"]

$field = $List.Fields["SPBuiltInFieldId.LinkFilenameNoMenu"]


$View.ViewFields.Add($field)


$view.Update()

}

I am getting

"Exception calling "Add" with "1" arguments(S): "Object reference not set to an instance of an object"" on the line for "$View.ViewFields.Add($field).

Any thoughts on how I should modify this so that I can add this column to my library views?

1 Answer 1

3

This error usually occurs in case the provided FieldName is not correct.

So In your script, try to do the following:

  • Remove this line $field = $List.Fields["SPBuiltInFieldId.LinkFilenameNoMenu"].
  • Provide the column name directly at $View.ViewFields.Add($field) as the following:

    • The field name for Name (linked to document with edit menu) is LinkFilename
    • The field name for Name (linked to document) is LinkFilenameNoMenu

So based on the corresponding column name, it should look like this

$View.ViewFields.Add("LinkFilenameNoMenu") #LinkFilenameNoMenu

The final script

Add-PSSnapin Microsoft.SharePoint.PowerShell

$InvFile="ProjectURLs2.csv" 
# Get Data from Inventory CSV File 
$FileExists = (Test-Path $InvFile -PathType Leaf) 
if ($FileExists) { 
   "Loading $InvFile for processing..." 
   $tblData = import-csv $InvFile 
} else { 
   "$InvFile not found - stopping import!" 
  exit 
}

foreach ($row in $tblData)
{
$ProjectURL = $row."ProjectURL".ToString()
$web = Get-SPWeb "$ProjectURL"
$list = $web.Lists["Document Library"]
$View = $List.Views["All Documents without Folders"]
$View.ViewFields.Add("LinkFilenameNoMenu")
$view.Update()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.