
Hi Nhung Dang
Thanks for reaching out to Microsoft Q&A forum support
Based on your description, I understand that you're looking for a method to iterate through the fields of a SharePoint list and retrieve only the custom fields, while excluding the built-in SharePoint fields.
In SharePoint, lists contain both built-in fields (such as Title, Created, Modified, etc.) and custom fields that are added by users or developers to meet specific business needs. The challenge is that SharePoint does not explicitly label fields as "custom" or "built-in" However, built-in fields often have properties such as being hidden, read-only, or non-deletable. In contrast, custom fields are typically visible, editable, and can be deleted. These characteristics can be used to distinguish between the two types of fields programmatically.
To address this, you can use PowerShell with the SharePoint Client Object Model (CSOM) to iterate through the field collection and filter based on specific properties. Below is a sample PowerShell script that demonstrates how to retrieve only the custom fields:
# Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Path\To\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Path\To\Microsoft.SharePoint.Client.Runtime.dll"
# Connect to SharePoint
$siteUrl = "https://yourtenant.sharepoint.com/sites/yoursite"
$username = "******@yourtenant.com"
$password = ConvertTo-SecureString "yourpassword" -AsPlainText -Force
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
# Get the list
$list = $ctx.Web.Lists.GetByTitle("Your List Name")
$ctx.Load($list.Fields)
$ctx.ExecuteQuery()
# Iterate and filter custom fields
foreach ($field in $list.Fields) {
if (-not $field.Hidden -and -not $field.ReadOnlyField -and $field.CanBeDeleted) {
Write-Host "Custom Field: $($field.InternalName) - $($field.Title)"
}
}
Or you can try the option which following this instruction: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield.sourceid.aspx
Hope my answer will help you
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.