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

[玩转系统] SharePoint Online:如何使用 PowerShell 部署设计包解决方案 (WSP)?

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

SharePoint Online:如何使用 PowerShell 部署设计包解决方案 (WSP)?


要求:在 SharePoint Online 中部署设计包解决方案 (WSP)。

先决条件:在部署任何设计包之前,您必须在网站集和网站级别激活发布基础结构功能!

如何使用 PowerShell 部署解决方案包?

您可能有用于网站模板的解决方案包 WSP 文件或用于品牌的设计包,或其他一些沙箱解决方案。 SharePoint Online 解决方案部署的工作原理如下:

将 WSP 文件上传到解决方案库并激活解决方案:

使用以下步骤在 SharePoint Online 中部署 WSP 解决方案。

  1. 以网站集管理员身份登录 >> 导航到您的 SharePoint Online 网站集(根网站)>> 单击“网站设置”。
  2. 在网站设置页面上,单击“Web Designer Gallery”部分下的“Solutions”链接。这将打开解决方案页面,如下图所示。在解决方案页面上,单击功能区中的“上传解决方案”按钮。

    [玩转系统] SharePoint Online:如何使用 PowerShell 部署设计包解决方案 (WSP)?

  3. 从本地磁盘浏览并选择 WSP 文件,然后上传解决方案。

    [玩转系统] SharePoint Online:如何使用 PowerShell 部署设计包解决方案 (WSP)?

  4. 单击“确定”按钮后,SharePoint 会上传 WSP 文件并显示一个页面,您可以从中激活解决方案,如下所示。激活解决方案需要一段时间。

    [玩转系统] SharePoint Online:如何使用 PowerShell 部署设计包解决方案 (WSP)?

  5. 最后,该解决方案将在您的 SharePoint Online 网站中激活!

    [玩转系统] SharePoint Online:如何使用 PowerShell 部署设计包解决方案 (WSP)?

用于在 SharePoint Online 中部署解决方案包的 PowerShell:

此 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 Deploy-SPOSolution()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $WSPFilePath,
        [Parameter(Mandatory=$False)] [Int] $MajorVersion = 1,
        [Parameter(Mandatory=$False)] [Int] $MinorVersion = 0
    )

    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 the Solution Gallery
        Write-Host -f Yellow "Uploading WSP File to Solution Gallery..."
        $SolutionGallery =  $Ctx.Web.Lists.GetByTitle("Solution Gallery")
        $SolutionGalleryRootFolder = $SolutionGallery.RootFolder
        $Ctx.Load($SolutionGallery)
        $Ctx.Load($SolutionGallery.RootFolder)
        $Ctx.ExecuteQuery()

        #Get WSP File Name from Path
        $WSPFileName = $WSPFilePath.Substring($WSPFilePath.LastIndexOf("\")+1)

        #Upload the WSP to the solution gallery        
        $WSPFileStream = New-Object System.IO.FileStream($WSPFilePath, [System.IO.FileMode]::Open)
        $FileCI = New-Object Microsoft.SharePoint.Client.FileCreationInformation
        $FileCI.Overwrite = $True
        $FileCI.ContentStream = $WSPFileStream
        $FileCI.URL = $WSPFileName
        $UploadedFile = $SolutionGallery.RootFolder.Files.Add($FileCI)
        $Ctx.Load($UploadedFile)
        $Ctx.ExecuteQuery()
        Write-Host -f Green "`tUploaded WSP File to Solution Gallery!"   

        #Install the solution
        Write-Host -f Yellow "Installing Solution..."
        $WSP = New-Object Microsoft.SharePoint.Client.Publishing.DesignPackageInfo
        $WSP.PackageGuid = [System.Guid]::Empty
        $WSP.PackageName = $WSPFileName
        $WSP.MajorVersion = $MajorVersion
        $WSP.MinorVersion = $MinorVersion

        #Install the solution from the file url - This creates a solution according to the Major and Minor Version
        $WSPFileURL = $SolutionGallery.RootFolder.ServerRelativeUrl + "/" + $WspFileName;
        [Microsoft.SharePoint.Client.Publishing.DesignPackage]::Install($Ctx, $Ctx.Site, $WSP, $WSPFileURL)
        $Ctx.ExecuteQuery()
        Write-Host -f Green "`tInstalled the Solution Successfully!"   

        #Activate the solution
        Write-Host -f Yellow "Activating the Solution..."
        [Microsoft.SharePoint.Client.Publishing.DesignPackage]::Apply($Ctx, $Ctx.Site, $WSP)
        $Ctx.ExecuteQuery()
        Write-Host -f Green "`tActivated the Solution Successfully!"

        #Remove the original wsp file uploaded
        $UploadedSolutionFile = $SolutionGallery.rootFolder.Files.GetByUrl($WSPFileURL)
        $UploadedSolutionFile.DeleteObject();
        $ctx.ExecuteQuery()

        Write-Host -f Green "`n*** WSP Deployment has been successfully completed!***"
        }
    catch {
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
}

#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/creditpipeline"
$WSPFilePath  = "C:\Users\salaudeen\Desktop\Crescent-Projects-v2.wsp"

#Get Credentials to connect
$Cred = Get-Credential

#call the function to deploy the solution
Deploy-SPOSolution -SiteURL $SiteURL -WSPFilePath $WspFilePath

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

取消回复欢迎 发表评论:

关灯