5

I'm trying to get columns in a list from the display name, however this isn't always working. Here is where I try to get the column, and check to see if it's null. I've tried it on display names without and with spaces. Sometimes it works, sometimes it doesn't. The display names haven't been changed.

                $column = $list.Fields[$columnName]
                if($column -ne $null)

Thanks for your help.

11

Since SPFieldCollection.Item property expects DisplayName to be passed, it should work properly, for example:

$field = $list.Fields["Created By"]

From another hand, SPFieldCollection.GetField method returns the field with the specified internal, display, or static name from the collection

How to return encoded Xml name via PowerShell

function EncodeName($name)
{
   $encodedName = [System.XML.XmlConvert]::EncodeName($name)
   return $encodedName 
}

Example: resolve field by Display Name

$fieldIntName = EncodeName("Post Category") #try to resolve field InternalName from DisplayName (works for fields created via UI)  
$field = $list.Fields.GetField($fieldIntName)  #get field by InternalName
2

You need to provide Internal Names instead of Display Names for the fields.. Most importantly the space gets replaced with _x0020_ in the Internal Name.. Following table shows the list of characters with their codes

Character    Internal Hex Code

    ~             _x007e_

    !             _x0021_

    @             _x0040_

    #             _x0023_

    $             _x0024_

    %             _x0025_

    ^             _x005e_

    &             _x0026_

    *             _x002a_

    (             _x0028_

    )             _x0029_

    _                _

    +             _x002b_

    -             _x002d_

    =             _x003d_

    {             _x007b_

    }             _x007d_

    :             _x003a_

    "             _x0022_

    |             _x007c_

    ;             _x003b_

    ‘             _x0027_

    \             _x005c_

    <             _x003c_

    >             _x003e_

    ?             _x003f_

    ,             _x002c_

    .            _x002e_

    /            _x002f_

    `            _x0060_

  [Space]        _x0020_

You can also run following Powershell command to get the list of Fields with Internal Names

$web = Get-SPWeb http://sharepoint/sites/training/salestraining
$list = $web.Lists["Announcements"]
$list.fields | select Title, InternalName, StaticName | sort title | ft -AutoSize

References:

SharePoint Internal Names
SharePoint Finding Display and Internal Names

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.