2

I am trying to create a calculated column through powershell script. Below is the Script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

 $site = Get-SPweb "http://test.com"

$mList=$site.Lists["TestCal"]

 $mList.Fields.Add("Count", "Calculated", 0)
 $SPField = $mList.Fields.GetField("Count")
 $SPField.OutputType="Number"
 $SPField.Formula="=LEFT(REPLACE(Title,SEARCH(".",Title),1,""),SEARCH(".",REPLACE(Title,SEARCH(".",Title),1,""))-1)"
$SPField.update()

$site.Dispose()

The calculated column gets created when I create it manually. But through powershell it gives me an error. Can any one tell me what is wrong with my script?

NOTE: If I use formula like this $SPField.Formula=[Title]+1 it is sucessful.

1 Answer 1

1

I was able to solve this. Not sure if this is a bug in sharepoint 2010. When ever you are using Replace command with Powershell like the one above in calculated column you need to pass the Formule in Single Quotes.Genrally you will pass with Double Quotes ..

For example in my scenario above:

$SPField.Formula="=LEFT(REPLACE(Title,SEARCH(".",Title),1,""),SEARCH(".",REPLACE(Title,SEARCH(".",Title),1,""))-1)"

Correct one replacing with single quotes

$SPField.Formula='=LEFT(REPLACE(Title,SEARCH(".",Title),1,""),SEARCH(".",REPLACE(Title,SEARCH(".",Title),1,""))-1)'

Hope it may be helpfful for some one

1
  • 1
    No bug in SP2010!!, you are building a String so any (closing) double quote will close your String definition. This applies for any programming language you use. Jul 20, 2015 at 8:06

Your Answer

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

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