question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: 'spo file add' submitting $null or blank values as 'true'

See original GitHub issue

Description:

When adding a file and attempting to set a field using a variable, if the variable is $null or empty the value passed through is the string ‘true’.

Steps to reproduce:

$path = 'C:\MS365.jpg'

Scenario A)

Variable set to $null

$nullTitle = $null

o365 spo file add --webUrl https://contoso.sharepoint.com/sites/project-x --folder 'Shared Documents' --path $path --FileLeafRef 'Null Title.jpg' --Title $nullTitle

Expected

New file added to destination with Title attribute of '' (empty string)

Actual

New file added to destination with Title attribute of 'true'

Scenario B)

Variable set to ‘’ (empty string)

$emptyTitle = ''

o365 spo file add --webUrl https://contoso.sharepoint.com/sites/project-x --folder 'Shared Documents' --path $path --FileLeafRef 'Empty String Title.jpg' --Title $emptyTitle

Expected

New file added to destination with Title attribute of '' (empty string)

Actual

New file added to destination with Title attribute of 'true'

Scenario C) [Working as Expected]

Variable set to ‘not null or empty’

$stringTitle = 'not null or empty'

o365 spo file add --webUrl https://contoso.sharepoint.com/sites/project-x --folder 'Shared Documents' --path $path --FileLeafRef 'String Title.jpg' --Title $stringTitle

Expected/Actual

New file added to destination with Title attribute of 'not null or empty'

Additional notes:

This bug is particularly nasty when trying to pass an empty or null variable into a Date/Time, Number, or Lookup field as it will result in an incorrect type error.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ohthreesixtyfivecommented, Dec 15, 2018

I had a look into it, and it seems that the problem might be beyond our reach. When you pass a PowerShell $null as an option value, it gets removed from the list of arguments passed into Node.js. So running a command like o365 spo web get -u $a (where $a is $null) passes an array of arguments such as o365, spo, web, get, -u. Nothing is passed in place of the $null argument, which makes the given option behave like a switch parameter (basically like --debug) which explains the true value you’re seeing. I don’t think there is much we can do here without changing how parameters are parsed and trying to change default values for parameters that aren’t meant as a switch, which could be tricky. I wish I had better news.

That’s a bummer. Thanks for looking into it all the same!

On a related note:

The workaround I described above seemed way too inefficient, as it was iterating n number of times for each n number of objects to build a unique string, so I went back to the drawing board to come up with something cleaner.

Since iex worked just fine, I simply turned my existing ‘hardcoded’ command from earlier and set that as a string. Using a regular expression, I was able to match and remove options that had $null or empty values. I’ll post it here in case anyone could use it in the future:

### for example of this regular expression visit https://regexr.com/44vuq ###
$NullorEmptyPattern = '((-|--)\w+\s{2}|(-|--)\w+\s{1}$)'

$path = "C:\MS365.jpg"

$notNull = 'Not Null'
$Title = "$null"
$Value = "$notNull"

$execute = "o365 spo file add --webUrl https://contoso.sharepoint.com/sites/project-x --folder 'Shared Documents' --path $path --FileLeafRef 'MS365.jpg' --NotNullValue $Value --Title $Title"

Write-Host String With Null or Empty Values Included -F DarkYellow
Write-Host $execute
Write-Host

### use regular expression pattern to match and remove any null or empty options ###
$executeO365SPOFileAdd = ($execute -replace $NullorEmptyPattern, '')

Write-Host String With Null or Empty Values Excluded -F DarkGreen
Write-Host ($executeO365SPOFileAdd)

### execute o365 file add with $null or empty options removed ###
iex $executeO365SPOFileAdd

Some items to consider and known limitations of this implementation:

  • The RegEx will match --Help, --Verbose, and --Debug; if you require these options you’ll want to append it to your execution string (eg: iex ($executeO365SPOFileAdd + ' --Debug') )
  • The RegEx is looking for a command option (whose beginning is defined by ‘-’ or ‘–’), followed by any number of characters (the option name) which are then followed by 2 empty spaces. There is also special consideration for options that only have 1 empty space and occur at the end of the string. As a result, if you are encapsulating your $null or empty variable with quotes (eg: "'$null'" ), it will not be matched.

Feel free to let me know if you have any questions or recommendations on how to make this even cleaner!

Thank you again for taking a look at this Waldek, I really appreciate your efforts! ⭐️

1reaction
ohthreesixtyfivecommented, Dec 11, 2018

Thank you for the detailed repro steps. I’ll have a look at it. Am I correct to assume that you’ve spotted the issue in PowerShell?

You’re very welcome. Your assumption is correct, this issue has been spotted in PowerShell using non-immersive mode. Apologies for not including it in the original issue report.

Looking forward to your fix, in the meantime I’m working out how to dynamically build the parameters passed to ‘spo file add’ to only include non-null attributes.

Best regards,

Ramiro

Read more comments on GitHub >

github_iconTop Results From Across the Web

Solved: Update file property to empty - null not working
Solved: At the end of my flow, I am trying to set one file property to empty/blank. The file is maintained in a...
Read more >
Set default values for blank fields in Zaps - Zapier
Set default values for blank fields in Zaps · 1. Add Formatter to your Zap · 2. Set up the Default Value text...
Read more >
SharePoint Online: How to Change Required Field Settings in ...
When users try to save an item or file without filling in the required field, they'll see an error message “You can't leave...
Read more >
Bug listing with status UNCONFIRMED as at 2022/12/24 17 ...
Bug :128538 - "sys-apps/coreutils: /bin/hostname should be installed from coreutils ... an empty value when cross compiling" status:UNCONFIRMED resolution: ...
Read more >
SharePoint Column Formatting - Highlight Null Values
That's all you have to do to get color coding for null/blank values. In the JSON, you reference a null/blank value by using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found