ค้นหาเว็บไซต์

SharePoint Online: จะปรับใช้ Design Package Solution (WSP) โดยใช้ PowerShell ได้อย่างไร


ความต้องการ: ปรับใช้โซลูชันแพ็คเกจการออกแบบ (WSP) ใน SharePoint Online

ข้อกำหนดเบื้องต้น: คุณต้องเปิดใช้งานฟีเจอร์โครงสร้างพื้นฐานการประกาศทั้งในระดับไซต์คอลเลกชันและระดับไซต์ก่อนที่จะปรับใช้แพ็คเกจการออกแบบใดๆ

จะปรับใช้แพ็คเกจโซลูชันโดยใช้ PowerShell ได้อย่างไร

คุณอาจมีไฟล์ WSP แพ็คเกจโซลูชันสำหรับเทมเพลตไซต์หรือแพ็คเกจการออกแบบสำหรับการสร้างแบรนด์ หรือโซลูชัน Sandbox อื่นๆ นี่คือวิธีการทำงานของ SharePoint Online Solution Deployment:

อัปโหลดไฟล์ WSP ไปยังแกลเลอรีโซลูชันและเปิดใช้งานโซลูชัน:

ใช้ขั้นตอนต่อไปนี้เพื่อปรับใช้โซลูชัน WSP ใน SharePoint Online

  1. เข้าสู่ระบบในฐานะผู้ดูแลไซต์คอลเลกชัน >> นำทางไปยังคอลเลกชันไซต์ SharePoint Online ของคุณ (ไซต์หลัก) >> คลิกที่ "การตั้งค่าไซต์"
  2. ในหน้าการตั้งค่าไซต์ คลิกลิงก์ "โซลูชัน" ใต้ส่วน "แกลเลอรีนักออกแบบเว็บไซต์" ซึ่งจะเปิดหน้าโซลูชันตามหน้าจอด้านล่าง ในหน้าโซลูชัน คลิกที่ปุ่ม "อัปโหลดโซลูชัน" จาก Ribbon

  3. เรียกดูและเลือกไฟล์ WSP จากดิสก์ภายในเครื่องของคุณ และอัปโหลดโซลูชัน

  4. หลังจากคลิกที่ปุ่ม "ตกลง" SharePoint จะอัปโหลดไฟล์ WSP และนำเสนอเพจที่คุณสามารถเปิดใช้งานโซลูชันได้ ดังที่แสดงด้านล่าง ใช้เวลาสักครู่ในการเปิดใช้งานโซลูชัน

  5. สุดท้ายนี้ โซลูชันจะถูกเปิดใช้งานในไซต์ SharePoint Online ของคุณ!

PowerShell เพื่อปรับใช้แพ็คเกจโซลูชันใน SharePoint Online:

สคริปต์ PowerShell นี้ปรับใช้แพ็คเกจโซลูชันที่กำหนดและเปิดใช้งานโซลูชัน


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\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

บทความที่เกี่ยวข้อง