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

[玩转系统] SharePoint Online:使用 PowerShell 导出-导入网站列

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

SharePoint Online:使用 PowerShell 导出-导入网站列


要求: 在 SharePoint Online 中导出-导入网站栏。

SharePoint Online:使用 PowerShell 导出网站列

因此,您的 SharePoint Online 环境中有很多网站栏。并需要在环境之间导出和导入这些网站栏?通过 Web 浏览器界面执行此操作可能会很乏味,因此我编写了一些 PowerShell 脚本来使该过程变得更加容易。在这篇文章中,我将向您展示如何使用 PowerShell 在 SharePoint Online 网站之间(或不同的 SharePoint Online 租户之间)导出和导入网站列。

此 PowerShell 脚本将 SharePoint Online 网站特定组的所有网站列导出到 XML 文件:


#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 export site columns from a group
Function Export-SPOSiteColumns([String]$SiteURL, [String]$SiteColumnGroup,[String]$XMLFile)
{
    Try{
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    
        #Get All Site Columns of the Web
        $Ctx.Load($Ctx.Web)
        $Ctx.Load($Ctx.Web.Fields)
        $Ctx.ExecuteQuery()

        #Get the site column Group
        $SiteColumns = $Ctx.Web.Fields | where {$_.Group -eq $SiteColumnGroup}
        if($SiteColumns -ne $NULL)
        {
            #Wrap Field Schema XML inside <Fields> Element 
            Add-Content $XMLFile "`n<Fields>"

            #Loop through each site column of the group
            ForEach($Field in $SiteColumns)
            {
                Add-Content $XMLFile $Field.SchemaXml
            }
            #Closing Wrapper
            Add-Content $XMLFile "</Fields>"

            Write-host -f Green "Site Columns Exported Successfully!"
        }
        else
        {
            Write-host -f Yellow "Could not find the Site Column Group '$($SiteColumnGroup)' at '$($SiteURL)'"
        }
    }
    Catch {
    write-host -f Red "Error Exporting Site Columns!" $_.Exception.Message
    }
}

#Set Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$SiteColumnGroup="Crescent Projects"
$XMLFile="C:\Temp\CrescentSiteColumns.xml"

#Get Credentials to connect
$Cred= Get-Credential

#Call the function to export the Site Column group to XML
Export-SPOSiteColumns $SiteURL $SiteColumnGroup $XMLFile

执行后,此 PowerShell 脚本会将给定网站栏组中的所有网站栏导出到 XML 文件,如下所示:

[玩转系统] SharePoint Online:使用 PowerShell 导出-导入网站列

使用 PowerShell 从 XML 文件导入网站栏:

下一步是将它们导入另一个 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 Import site columns from a XML file
Function Import-SPOSiteColumns([String]$SiteURL, [String]$XMLFile)
{
    Try{
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    
        #Get XML file contents
        [xml]$FieldsXML = Get-Content($XMLFile)

        #Remove the "Version" Attribute from the XML
        $FieldsXML.Fields.Field.RemoveAttribute("Version")

        #Loop Through Each Field
        ForEach($Field in $FieldsXML.Fields.Field)
        {
            #Check if the web has the field already!
            $Ctx.Load($Ctx.Web.Fields)
            $Ctx.ExecuteQuery()

            $SiteColumn = $Ctx.Web.Fields | Where {$_.InternalName -eq $Field.Name}
            If($SiteColumn -eq $Null)
            {
                #Add Site column to Web
                $SiteColumn = $Ctx.Web.Fields.AddFieldAsXml($Field.OuterXml, $True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
                $Ctx.ExecuteQuery()
                Write-host "Site Column Added:" $($Field.Name)
            }
            Else
            {
                Write-host -f Yellow "Site Column '$($Field.Name)' Already Exists!"
            }
        }
        Write-host -f Green "Site Columns Imported Successfully!"
    }
    Catch {
    write-host -f Red "Error Importing Site Columns!" $_.Exception.Message
    }
}

#Set Config Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$XMLFile="C:\Temp\CrescentSiteColumns.xml"

#Get Credentials to connect
$Cred= Get-Credential

#Call the function to Import the Site Columns from XML
Import-SPOSiteColumns $SiteURL $XMLFile 

这是关于使用 PowerShell 在 SharePoint 本地导出和导入网站栏的另一篇文章:如何使用 PowerShell 在 SharePoint 中导出-导入网站栏?

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

取消回复欢迎 发表评论:

关灯