0

I'm a complete PowerShell noob and don't really have much exposure with it at all. I've googled for assistance but not having any joy with my specific issue.

I'm trying to get a SiteColumn that exists in a site collection and then apply that same site column to a list on a subsite in the same site collection.

Below is one of the examples i have tried with no joy and the error message below

$web = Get-SPSite http://siteURL/

$siteCol = $web.Fields.GetFieldByInternalName("ColumnName")

$subWeb = "SubSiteURL"
$spList = $subWeb.Lists["ListName"]

$spList.Fields.Add($siteCol)

$spList.Update()


You cannot call a method on a null-valued expression.
At line:7 char:1
+ $siteCol = $web.Fields.GetFieldByInternalName("ColumnName")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Cannot index into a null array.
At line:10 char:1
+ $spList = $subWeb.Lists["ListName"]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

You cannot call a method on a null-valued expression.
At line:12 char:1
+ $spList.Fields.Add($siteCol)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At line:13 char:1
+ $spList.Update()
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Can anyone point me towards the answer please?

Thanks.

  • 1
    Modify $web = Get-SPSite http://siteURL/ to $web = Get-SPWeb http://siteURL/ and $subWeb = Get-SPWeb "http://siteurl/subsiteurl" and check – Gautam Sheth Dec 5 '17 at 17:29
  • nice ! is it working for you ? – Gautam Sheth Dec 6 '17 at 5:24
  • Awesome :) , have added it as an answer below. Could you please accept it so that it wont show up in the unanswered section ? Thanks ! – Gautam Sheth Dec 7 '17 at 14:42
2

Modify your PS as below:

$web = Get-SPWeb http://siteURL/

$siteCol = $web.Fields.GetFieldByInternalName("ColumnName")

$subWeb = Get-SPWeb "http://siteurl/subsiteurl"

$spList = $subWeb.Lists["ListName"]

$spList.Fields.Add($siteCol)

$spList.Update()

You were getting an error because $web.Fields.GetFieldByInternalName method is available on the SPWeb object but you were using it in the SPSite object. It wouldn't work, hence changed it from SPSite to SPWeb.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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