当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 将多行文本字段类型更改为富文本

作者:精品下载站 日期:2024-12-14 14:53:03 浏览:12 分类:玩电脑

SharePoint Online:使用 PowerShell 将多行文本字段类型更改为富文本


要求:在 SharePoint Online 中将多行文本字段类型更改为富文本。

如何将多行文本字段类型从纯文本更改为富文本?

如果多行文本字段类型设置为纯文本,则会限制用户可用的格式选项。但是,可以使用字段设置或 PowerShell 将多行文本字段类型更改为增强型富文本,PowerShell 提供附加格式选项,例如粗体、斜体和超链接。在本文中,我们将向您展示如何将多行文本字段类型更改为富文本。

在 SharePoint Online 中从纯文本切换到富文本或增强型富文本非常简单:

  1. 转至目标列所在的列表或站点。进入栏目设置。
  2. 根据您的要求,将列类型选择为“富文本(粗体、斜体、文本对齐、超链接)”或“增强型富文本(带有图片、表格和超链接的富文本)”。

    [玩转系统] SharePoint Online:使用 PowerShell 将多行文本字段类型更改为富文本

  3. 单击“确定”保存更改。

PowerShell 在 SharePoint Online 中将多行文本列类型设置为“富文本”:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Function to Set Multiple Lines of Text Field Type to "Rich Text"
Function Set-SPOMultilineTextType([String]$SiteURL,[String]$ListName, [String]$FieldInternalName)
{
    Try {
        #Get credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
    
        #Get the list & Fields
        $List = $Ctx.Web.lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $ListFields = $List.Fields
        $Ctx.Load($ListFields)
        $Ctx.ExecuteQuery()

        #Get the Field to Update
        $FieldToUpdate = $ListFields | Where {$_.InternalName -eq $FieldInternalName}
        
        If($FieldToUpdate -ne $NULL)
        {
            #Update the Field Settings
            $FieldToUpdate.RichText = $True
            $FieldToUpdate.Update()
            $Ctx.ExecuteQuery()
            write-host -f Green "Field '$($FieldToUpdate.Title)' Updated in List '$($List.Title)' Successfully!" $_.Exception.Message
        }
    }
    Catch {
        write-host -f Red "Error Updating Field Settings!" $_.Exception.Message
    }
}
#Config Parameters
$SiteURL="https://Crescent.sharepoint.com"
$ListName="Projects V1"
$FieldInternalName="Project_x0020_Description"

#Call the function
Set-SPOMultilineTextType -SiteURL $SiteURL -ListName $ListName -FieldInternalName $FieldInternalName

如果您想要将 SharePoint Online 网站中的所有多行文本字段转换为富文本列,该怎么办?您可以使用以下 PowerShell 脚本转换所有列表和库中的多行文本字段:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Function to convert Multiple Lines of Text Field Type to "Rich Text"
Function Set-AllSPOMultilineTextType([String]$SiteURL)
{
    Try {
        #Get credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
    
        #Get all lists
        $Lists = $Ctx.web.Lists
        $Ctx.Load($Lists)
        $Ctx.ExecuteQuery()    

        #Iterate through each list
        ForEach($List in $Lists | Where {$_.Hidden -eq $False})
        {
            #Get AllowDeletion Property to filter system lists
            $List.Retrieve("AllowDeletion")
            $Ctx.ExecuteQuery()

            If($List.AllowDeletion -eq $True)
            {
                #Get All List Fields
                $ListFields = $List.Fields
                $Ctx.Load($ListFields)
                $Ctx.ExecuteQuery()
            
                ForEach($Field in $ListFields)
                {

                    If($Field.TypeDisplayName -eq "Multiple lines of text" -and $Field.hidden -eq $False -and $Field.ReadOnlyField -eq $False)
                    {
                        #Update the Field Settings
                        $FieldToUpdate.RichText = $True
                        $FieldToUpdate.Update()
                        $Ctx.ExecuteQuery()
                        write-host -f Green "Field '$($Field.Title)' Updated in List '$($List.Title)' Successfully!" $_.Exception.Message
                    }
                }
            }
        }
    }
    Catch {
        write-host -f Red "Error Updating Field Settings!" $_.Exception.Message
    }
}
#Config Parameters
$SiteURL="https://Crescent.sharepoint.com"

#Call the function
Set-AllSPOMultilineTextType -SiteURL $SiteURL 

这将启用其他格式选项,例如字体颜色、表格等。

使用 PowerShell 将富文本字段转换为增强型富文本字段:

要将多行文本字段类型设置为增强型富文本字段,我们应该首先将字段从纯文本转换为“富文本”(以便填充“RichTextMode”属性),然后更改字段架构。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Function to Set Multiple Lines of Text Rich Text Field Type to "Enhanced Rich Text"
Function Set-SPOMultilineRichTextToEnhanced([String]$SiteURL, [String]$ListName, [String]$FieldInternalName)
{
    Try {
        #Get credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
    
        #Get the list & Fields
        $List = $Ctx.Web.lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $ListFields = $List.Fields
        $Ctx.Load($ListFields)
        $Ctx.ExecuteQuery()

        #Get the Field to Update
        $FieldToUpdate = $ListFields | Where {$_.InternalName -eq $FieldInternalName}
        
        If($FieldToUpdate -ne $NULL)
        {
            If($FieldToUpdate.RichText -eq $True)
            {
                #Enable Enhanced Rich Text
                $FieldToUpdate.SchemaXml = $FieldToUpdate.SchemaXml.Replace("RichTextMode=`"Compatible`"","RichTextMode=`"FullHtml`"")
                $FieldToUpdate.Update()
                $Ctx.ExecuteQuery()

                write-host -f Green "Field '$($FieldToUpdate.Title)' Updated in List '$($List.Title)' Successfully!" $_.Exception.Message
            }
            else
            {
                write-host -f Yellow "Field '$($FieldToUpdate.Title)' Should be a Rich Text Field in order to Make it Enhanced Rich Text!" $_.Exception.Message
            }
        }
        else
        {
            write-host -f Yellow "Field '$($FieldToUpdate.Title)' doesn't exist in the List!" $_.Exception.Message
        }
    }
    Catch {
        write-host -f Red "Error Updating Field Settings!" $_.Exception.Message
    }
}
#Config Parameters
$SiteURL="https://Crescent.sharepoint.com"
$ListName="Projects V1"
$FieldInternalName="Project_x0020_Description"

#Call the function
Set-SPOMultilineRichTextToEnhanced -SiteURL $SiteURL -ListName $ListName -FieldInternalName $FieldInternalName

完成这些步骤后,多行文本字段类型将设置为富文本,您可以开始使用新的格式选项来创建更具视觉吸引力和吸引力的内容。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯