0

How to change the JSLink property of the Blog posts Web part in Default.aspx page of a blog site using powershell ? I tried the code below

$SPsite = Get-SPWeb -Identity "http://blogpostsiteurl"  
  $webpartmanager = $SPsite.GetLimitedWebPartManager("/default.aspx",   
 [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)    

  for($i=0;$i -lt $webpartmanager.WebParts.Count;$i++)  {   
  if($webpartmanager.WebParts[$i].title -eq "Posts")   
  {    
     $wp=$webpartmanager.WebParts[$i];  
     $wp.JSLink="~sitecollection/_catalogs/masterpage/blogpost.js"; 
     $webpartmanager.SaveChanges($wp);  
     break;   
  }   
 }   
  $SPsite.Update();   
  $SPsite.Dispose();  

But i get the error

Exception calling "GetLimitedWebPartManager" with "2" argument(s): "Value does
not fall within the expected range."

Can someone help me with what I am doing wrong?

Thanks!

1 Answer 1

0

After some research , I got it

   $SiteURL = "http://blogsiteurl"  


    Function SetJSLinkPropertyOnPostsWebPart($SiteURL)
    { 

        $pagePath = "/default.aspx"
        $pageUrl = $SiteURL + $pagePath
        $web = Get-SPWeb $SiteURL

        $spWebPartManager = $web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

        foreach ($webpart in ($spWebPartManager.WebParts | Where-Object {$_.Title -eq "Posts"}))
        {
         $webpart.JSLink="~sitecollection/_catalogs/masterpage/blogpost.js"; 
         $spWebPartManager.SaveChanges($webpart);  
         break; 
        }
    }

    SetJSLinkPropertyOnPostsWebPart $SiteURL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.