2

I have a Sharepoint Online site and I am trying to get the grip of Powershell.

I used Powershell with CSOM to create a list from the 'custom list' template. Then I Got the 'City' and 'Company' fields from the Available fields and added them to the list. I also added the fields to appear in the default view, and finally I simply added an item to the list.

I created (or actually re-used a sample I found online) another method which retrieves the list by name, traverses the list, retrieves each item and appends it to an array.

Code here:

$mQueryRowLimit = 200
#Specify tenant admin and site URL
$User = "[email protected]"
$SiteURL = "https://t.sharepoint.com"
$ListTitle = "Demo"

#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\SPServerDLLs\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\SPServerDLLs\Microsoft.SharePoint.Client.Runtime.dll"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

getAllListItems -_ctx $Context -_listName $ListTitle -_rowLimit $mQueryRowLimit
$Context.Dispose()

function getAllListItems($_ctx, $_listName, $_rowLimit)
{
    # Load the up list
    $lookupList = $_ctx.Web.Lists.GetByTitle($_listName)
    $_ctx.Load($lookupList)

    # Prepare the query
    $query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $query.ViewXml = "<View>
        <RowLimit>$_rowLimit</RowLimit>
    </View>"

    # An array to hold all of the ListItems
    $items = @()

    # Get Items from the List until we reach the end
    do
    {
        $listItems = $lookupList.getItems($query)
        $_ctx.Load($listItems)
        $_ctx.ExecuteQuery()
        $query.ListItemCollectionPosition = $listItems.ListItemCollectionPosition

        foreach($item in $listItems)
        {
            Try
            {
                # Add each item
                WriteHost $item['Company']
                $items += $item
            }
            Catch [System.Exception]
            {
                # This shouldn't happen, but just in case
                Write-Host $_.Exception.Message
            }
        }
    }
    While($query.ListItemCollectionPosition -ne $null)

    #return $items
    $_ctx.Dispose()
}

I am experiencing some issues with the above code.

WriteHost $item['Company']

This retrieves the content as supposed to.

WriteHost $item['City']

This returns an empty result, when in reality there is a value and I can view it from the list in the Sharepoint site.

Apart from that, when I change the syntax (for e.g. from Company to City), why does it require 2 runs before it actually gives a different result? (I am working in Windows Powershell ISE.)

2 Answers 2

1

If you added the column City from existing site columns, then in reality it is called 'WorkCity'.

Does your code work if you use WriteHost $item['WorkCity']?

Does WriteHost $item['City'] ever work? What do you mean by 2 runs to get different results?

4
  • Ok this worked. For future reference, from where could I have known that I should have used WorkCIty? To answer your question, I am noticing that when I make a change in the code, when I save and run, I get the result of the initial code, and when I run again, it works properly. This means that I have to always run twice before I get the expected result Commented Aug 18, 2016 at 15:34
  • About the twice run - some global variables that are keeping the values? For the columns - go to the list settings>click on column name> check the url> at the end you see Field=RealName. You can also use /_api to check for static/internal field names. Commented Aug 18, 2016 at 15:42
  • should I initialize them to null at the start? Isnt disposing of the context enough? Commented Aug 18, 2016 at 15:46
  • You don't have the issue if you call the script from PS, right? Nulls should help or re-opening ISE. I had it sporadically, only in ISE, but do not know any logical explanation, nor cannot find anything helpful in Google :/. Unless you are using $global:var1 or $env:var1, scopes (enviromental, global, script) do not explain this behaviour sufficiently, because the scope of the script is discarded when it finishes running. I'd say 'a bug', but feel free to open a new thread on that and post a link here. Commented Aug 18, 2016 at 16:11
0

When running PowerShell Scripts, be aware that a function can only be used when the code of the function was processes before the function is executed

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.