locked
Setting SPField.Indexed to true in Powershell RRS feed

  • Question

  • $SPWeb = Get-SPWeb "https://mydev.company.com" $spListName = "/Lists/" + "myList"

    [Microsoft.SharePoint.SPList]$spList = $SPWeb.GetList(($SPWeb.ServerRelativeUrl.TrimEnd("/") + $spListName)) [Microsoft.SharePoint.SPField] $spField = $spList.Fields["Status"] $spField.Indexed = $true; $spField.Update() $spList.FieldIndexes.Add($spField); $spList.Update() $SPWeb.Dispose()


    I am using the above code in an attempt to create an index on the Status field of a workflow task list. When I write-host $splist.Title it appears correctly in the output with my list name. When I do the same for $spfield.Title it displays '.Title'  in the output. The script then errors out with " The property 'Indexed' cannot be found on this object." when it hits $spField.Indexed = $true;

    Why would this be happening?


    Love them all...regardless. - Buddha

    Wednesday, August 26, 2015 4:38 PM

Answers

  • Hi,

    The following PowerShell Script for your reference:

    $InternalFldName="Status"
    $listName="Workflow Tasks"
    
    $spweb = Get-SPWeb "http://sp2013sps:8080"
    $list = $spweb.Lists[$listName]
    
    try
    {
    	Write-Output "The searching  field $InternalFldName";
    
    	$fldToIndex =$list.Fields.GetFieldByInternalName($InternalFldName);                                                
    
    	Write-Output "The field $fldToIndex has been found";
    
    	try
    	{
    		$index= $list.FieldIndexes.Item($fldToIndex.Id)
    		Write-Output "The field $fldToIndex is already indexed"
    	}
    	catch
    	{
    		$fldToIndex.Indexed = $true;   
    		$list.FieldIndexes.Add($fldToIndex);
    		Write-Output ("The field {0} has been indexed" -f  $fldToIndex.Title);
    	}
    }
    catch
    {
    	$error[0]
    }

    More information:

    https://gallery.technet.microsoft.com/office/PowerShell-Script-for-0117b567

    Best Regards,

    Dennis Guo


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    • Proposed as answer by Patrick_Liang Sunday, September 6, 2015 6:24 AM
    • Marked as answer by Patrick_Liang Monday, September 7, 2015 4:52 AM
    Thursday, August 27, 2015 7:54 AM