none
List Content Type doesn't update via FieldLinks.Add()

    질문

  • Hello everyone.

    This issue puzzles me, because by every means it should work, but it doesn't.

    I have a simple powershell script, who adds missing fieldlinks from site content type to list content type

    (FieldExist is a custom ps function - it just checks if fieldlink exist or not)

    foreach($sourceFieldLink in $sourceCT.FieldLinks)
                    {
                        $flag=FieldExist -ct $listCT -fl $sourceFieldLink
                        if (-Not $flag)
                        {
                            $fl=New-Object -TypeName Microsoft.Sharepoint.SPFieldLink -ArgumentList $sourceCT.Fields[$sourceFieldLink.Id]
                            $listCT.FieldLinks.Add($fl)
                            $listCT.Update()
                        }
    
                    }

    And this code works on dev enviroment - list content type updates just fine, but on test enviroment it fails. Only difference is that at dev list content type locates on same web (root) as site content type, but at test enviroment list content type hosted on subweb.

    There are no errors whatsoever, and the most amazing thing - FieldLinks collection is actually updated, but Fields stays the same. This is really baffles me, because every source i could find tells me that SPContentType Fields collection is just a view for FieldLinks collection.

    I really need a hand here.

    Thanks in advance

     

    2018년 5월 11일 금요일 오전 11:58

모든 응답

  • Hi,

    I did a test based on your description with the script below:

    $web = Get-SPWeb "http://sp/sites/dev"
    $sourceCT=$web.ContentTypes["testct"]
    
    $subweb=Get-SPWeb "http://sp/sites/dev/sub1"
    $listCT=$subweb.ContentTypes["listCT"]
    
    
    foreach($sourceFieldLink in $sourceCT.FieldLinks)
    {
                      
        $fl=New-Object -TypeName Microsoft.Sharepoint.SPFieldLink -ArgumentList $sourceCT.Fields[$sourceFieldLink.Id]
        $listCT.FieldLinks.Add($fl)
        $listCT.Update()
                        
    }

    In the script above testct is located in the root site and listCT located in the sub site sub1, and the fields in the listCT is updated, see the capture for the two Content Type fields settings:

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    2018년 5월 14일 월요일 오전 8:30
  • But it doesn't work in test enviroment, although it works on dev, just like in your tests

    Any idea what can cause this issue?

    2018년 5월 14일 월요일 오후 1:47
  • Hi,

    How about checking the ULS log after running the script in test environment ? This issue is more related to environment as the code is no problem as I tested.

    You can check the ULS log with ULS Viewer:

    https://www.microsoft.com/en-sg/download/details.aspx?id=44020

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    2018년 5월 15일 화요일 오전 2:20
  • Thanks, i'll chack logs and report about my progress
    2018년 5월 15일 화요일 오후 12:33
  • Hi,

    How is everything going ? Feel free to reply if there is any updates.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    2018년 5월 18일 금요일 오후 2:00