Add Script Editor and Content Editor Web Part via powershell

当前位置 : 首页 > 网页制作 > CSS > Add Script Editor and Content Editor Web Part via powershell

Add Script Editor and Content Editor Web Part via powershell

来源: 作者: 时间:2016-01-28 09:27
Add Script Editor Web Part :I had coding a function about adding scripteditorwebpart to page, here is the examplt to call function:AddScriptEditorWebPart http: localhost
Add Script Editor Web Part :
I had coding a function about adding scripteditorwebpart to page, here is the examplt to call function:
 
AddScriptEditorWebPart "http://localhost" "/SitePages/Home.x" "JSFile.js"
 
 
function AddScriptEditorWebPart($siteUrl, $pageUrl, $jsLink){  
$webPartProperty_Visible = $true  
$web = get-spweb $siteUrl  
$defaultPage = $web.GetFile($pageUrl)  
  
# Get the LimitedWebPartManager  
$webpartmanager=$defaultPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)  
  
#Create fancy GUID  
$lvwpGuid1 = [System.Guid]::NewGuid().ToString()  
$lvwpKey = "g_" + $lvwpGuid1.Replace("-","_")  
  
# Instantiate wp  
$lvwp =New-Object Microsoft.SharePoint.WebPartPages.ScriptEditorWebPart   
$lvwp.ID = $lvwpKey  
$code = "$"  
$lvwp.Content =  @"  
<script src="http://code.jquery.com/jquery-2.0.3.min.js" ></script>  
<script type="text/javascript" src="$jsLink"></script>  
"@  
  
$lvwp.Title = "DocumentReadyForNewOrUpdatePost"   
$lvwp.Visible = $webPartProperty_Visible   
$lvwp.ChromeType = "None"  
$lvwp.HorizontalAlign = "Center"   
# Add the web part  
$webpartmanager.AddWebPart($lvwp, "Main", 0);  
  
# Update the web  
$web.Update();  
   
write-host "success"  
$web.Dispose()    
write-host "Done"  
}  

 

 
Add Content Editor Web Part :
AddScriptEditorWebPart "http://localhost" "/SitePages/Home.aspx" "file.txt"
 
 
function AddContentEditorWebPart($siteUrl, $pageUrl, $conetntLink){  
$webPartProperty_Visible = $true  
$web = get-spweb $siteUrl  
$defaultPage = $web.GetFile($pageUrl)  
  
# Get the LimitedWebPartManager  
$webpartmanager=$defaultPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)  
  
#Create fancy GUID  
$lvwpGuid1 = [System.Guid]::NewGuid().ToString()  
$lvwpKey = "g_" + $lvwpGuid1.Replace("-","_")  
  
# Instantiate wp  
$lvwp =New-Object Microsoft.SharePoint.WebPartPages.ContentEditorWebPart   
$lvwp.ID = $lvwpKey  
$code = "$"  
$lvwp.ContentLink =  @"  
$conetntLink  
"@  
$lvwp.Title = "DocumentReadyForNewOrUpdatePost"   
$lvwp.Visible = $webPartProperty_Visible   
$lvwp.ChromeType = "None"  
$lvwp.HorizontalAlign = "Center"   
  
# Add the web part  
$webpartmanager.AddWebPart($lvwp, "Main", 0);  
  
$web.Update();  
$web.Dispose()    
  
}  

 

 
 
 
Tag:
网友评论

<