Jun 07 2020 06:43 PM
Good Day, how can I retrieve the unique "User ID" of the "Created By" or "Modified By" User?
#Get SQL column Definition for SharePoint List Field
Function Get-ColumnDefinition([Microsoft.SharePoint.SPField]$Field)
{
$ColumnDefinition=""
Switch($Field.Type)
{
"Boolean" { $ColumnDefinition = '['+ $Field.InternalName +'] [bit] NULL '}
"Choice" { $ColumnDefinition = '['+ $Field.InternalName +'] [nvarchar](MAX) NULL '}
"Currency" { $ColumnDefinition = '['+ $Field.InternalName +'] [decimal](18, 2) NULL '}
"DateTime" { $ColumnDefinition = '['+ $Field.InternalName +'] [datetime] NULL '}
"Guid" { $ColumnDefinition = '['+ $Field.InternalName +'] [uniqueidentifier] NULL '}
"Integer" { $ColumnDefinition = '['+ $Field.InternalName +'] [int] NULL '}
"Lookup" { $ColumnDefinition = '['+ $Field.InternalName +'] [nvarchar] (500) NULL '}
"MultiChoice" { $ColumnDefinition = '['+ $Field.InternalName +'] [nText] (MAX) NULL '}
"Note" { $ColumnDefinition = '['+ $Field.InternalName +'] [nText] NULL '}
"Number" { $ColumnDefinition = '['+ $Field.InternalName +'] [decimal](18, 2) NULL '}
"Text" { $ColumnDefinition = '['+ $Field.InternalName +'] [nVarchar] (MAX) NULL '}
"URL" { $ColumnDefinition = '['+ $Field.InternalName +'] [nvarchar] (500) NULL '}
"User" { $ColumnDefinition = '['+ $Field.InternalName +'] [nvarchar] (255) NULL '}
default { $ColumnDefinition = '['+ $Field.InternalName +'] [nvarchar] (MAX) NULL '}
}
return $ColumnDefinition
}
#Function to get the value of given field of the List item
Function Get-ColumnValue([Microsoft.SharePoint.SPListItem] $ListItem, [Microsoft.SharePoint.SPField]$Field)
{
$FieldValue= $ListItem[$Field.InternalName]
#Check for NULL
if([string]::IsNullOrEmpty($FieldValue)) { return 'NULL'}
$FormattedValue = ""
Switch($Field.Type)
{
"Boolean" {$FormattedValue = Format-BooleanValue($FieldValue)}
"Choice" {$FormattedValue = Format-StringValue($FieldValue)}
"Currency" {$FormattedValue = $FieldValue}
"DateTime" {$FormattedValue = Format-DateValue($FieldValue)}
"Guid" { $FormattedValue = Format-StringValue($FieldValue)}
"Integer" {$FormattedValue = $FieldValue}
"Lookup" {$FormattedValue = Format-LookupValue($FieldValue) }
"MultiChoice" {$FormattedValue = Format-StringValue($FieldValue)}
"Note" {$FormattedValue = Format-StringValue($Field.GetFieldValueAsText($ListItem[$Field.InternalName]))}
"Number" {$FormattedValue = $FieldValue}
"Text" {$FormattedValue = Format-StringValue($Field.GetFieldValueAsText($ListItem[$Field.InternalName]))}
"URL" {$FormattedValue = Format-StringValue($FieldValue)}
"User" {$FormattedValue = Format-UserValue($FieldValue) }
#Check MMS Field
"Invalid" { if($Field.TypeDisplayName -eq "Managed Metadata") { $FormattedValue = Format-MMSValue($FieldValue) } else { $FormattedValue =Format-StringValue($FieldValue)} }
default {$FormattedValue = Format-StringValue($FieldValue)}
}
Return $FormattedValue
}
Jun 07 2020 07:51 PM
@SamTech : Try below code:
$CreatedBy=$item["Author"]
$CreatedByObj = New-Object Microsoft.SharePoint.SPFieldUserValue($web, $CreatedBy)
$CreatedByUserID = $CreatedByUserObj.User.ID;
Hope it helps !
Jun 08 2020 08:00 AM
@Ashish_Kohale I have added this suggested code but did not get any output for Author. Please advice where to add or modify the existing code
Jun 08 2020 10:24 AM
@SamTech : Try replacing "Author" with Created By"