How to disable column edit function but allow API to add file

Leung1975 296 Reputation points
2020-09-28T13:18:14.383+00:00

In SharePoint Online Document library, we have metadata with column 1,2,3,4. A REST API is used to add document to the columns. We want column 2,3,4 read only ( column 1 still editable). I used PnP PowerShell to make column 2,3,4 read only, as below,

$Docs= Get-PnPList -Identity Lists/ByItemID
$Number = Get-PnPField -List "ByItemID" -Identity "Number"
$Number.ReadOnlyField = $true
$Number.UpdateAndPushChanges($true)
$Docs.Context.ExecuteQuery();

but this affects REST API to add document to the columns 2,3,4. i.e. REST API cannot add document to the column 2,3,4. Any solution or suggestion on this case? Thanks

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,011 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jerryzy 10,566 Reputation points
    2020-09-29T06:24:15.347+00:00

    Hi @Leung1975 ,

    Rest API can only update field when ReadOnlyField property set to false, please check this similiar thread:

    Update readonly field using REST API

    A workaround would be using SharePoint Online CSOM to update Read-Only field value:

    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"    
    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"    
        
    $siteURL = "https://tenant.sharepoint.com"    
    $userId = "user@tenant.onmicrosoft.com"    
    $pwd = Read-Host -Prompt "Enter password" -AsSecureString    
    $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)    
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)    
    $ctx.credentials = $creds    
    try{    
        $lists = $ctx.web.Lists    
        $list = $lists.GetByTitle("doc1")    
        $listItem = $list.GetItemById(7)    
        $listItem["Column2"] = "UpdateForReadOnly2"   
        $listItem["Column3"] = "UpdateForReadOnly3"  
        $listItem["Column4"] = "UpdateForReadOnly4"   
        $listItem.Update()    
        $ctx.load($listItem)        
        $ctx.executeQuery()    
    }    
    catch{    
        write-host "$($_.Exception.Message)" -foregroundcolor red    
    }    
    

    28928-snipaste-2020-09-29-14-22-08.png


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful