0

I have a choice field of type radio button. When the user selects the radio button I want to change the image depending up on the selected value. Below is the screen shot for the same enter image description here Now when the User selects "Good" I want a calculated column to display Green and when they select "Bad" display Red. I was able to achieve the same with the below script.

Problem is when the question is big and have single Quotes like (') it is not working. Is there any better way where we can check in the if condition up to "GooD:","BAD:"? Please help me.

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
Start-SPAssignment -Global
$url = "http://santosh.test.com/sites/vendor"
$web = get-spweb $url
$fieldName="VendorRatingStatus"
if($field -ne $nul)
{
    Write-Host $field.Title "Field already avilable in the site" -foregroundcolor red
}
$web.fields.add($fieldName, "Calculated", $true)
$SPField = $web.fields.getfield($fieldName)
$SPField.Required = $false 
$SPField.OutputType="Number"
$SPField.Formula='=IF([Vendor]="GOOD:Vendor who deliver's a good Quality","<img src=http://santosh.test.com/sites/vendor/SiteCollectionImages/_t/GreenButton_png.jpg>",IF([Vendor]="BAD:Deliver's Bad Quality","<img src=http://santosh.test.com/sites/vendor/SiteCollectionImages/_t/RedButton_png.jpg>",IF(ISBLANK([Vendor]),"<img src=http://santosh.test.com/sites/vendor/SiteCollectionImages/_t/IntialButton_jpg.jpg>","")))'
$SPField.update()

1 Answer 1

3

It seems like there are really a few answers to this question --

First: If you want your Powershell script to work with the multiple single quotes, you can use Powershell character escaping to escape the single quotes that are really apostrophes (i.e. "deliver's"). If you add a back-tick (`) in front of the apostrophes, Powershell will not see them as the string delimiter (since you used a single quote at the beginning of that particular string). So your formula line would look like this:

$SPField.Formula='=IF([Vendor]="GOOD:Vendor who deliver`'s a good Quality","<img src=http://santosh.test.com/sites/vendor/SiteCollectionImages/_t/GreenButton_png.jpg>",IF([Vendor]="BAD:Deliver`'s Bad Quality","<img src=http://santosh.test.com/sites/vendor/SiteCollectionImages/_t/RedButton_png.jpg>",IF(ISBLANK([Vendor]),"<img src=http://santosh.test.com/sites/vendor/SiteCollectionImages/_t/IntialButton_jpg.jpg>","")))'

Second: If you want to shorten the actual formula for the calculated column to behave more like if [Vendor] contains "GOOD" or if [Vendor] contains "BAD", according to this MSDN page about calculated field formulas, you can use IF, FIND and ISNUMBER to achieve the equivalent of "contains". So I guess it would be something along the lines of

IF(ISNUMBER(FIND("GOOD", [Vendor])), "yes", "no")

Third: The word "delivers" does not actually have an apostrophe in it: the way you have written it is incorrect. If you take out the apostrophe from both of your "delivers" in your formula (and of course in your radio button labels as well), you will no longer be running into the problem of multiple single quotes.

1
  • Thanks Cristy Your Second solution has solved the problem.. Awsome man :-).... Commented Aug 5, 2014 at 4:43

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.