3

Salvete! Is it possible to add a webpart to a Wiki Home Page via Powershell? I have 100 sites for which I need to add a web-part to their home-pages. I know this is easily possible with normal web-part pages, but how about a wiki-home page?

2 Answers 2

5

Yes, it's possible. I had a blog post on this very topic, but the blog site has since been deactivated. Basically you do it like you would add a web part to any other Web Part Page. When it's time to specify the zone, use the "wpz" zone. Also, you need to add some markup to the Page's "WikiField" that resembles this (substituting your Web Part ID Guid where appropriate):

<div class="ms-rtestate-read ms-rte-wpbox">
  <div class="ms-rtestate-read {webpart ID guid}"
       id="div_{webpart ID guid}">
  </div>
  <div class="ms-rtestate-read"
       id="vid_{webpart ID guid}"
       style="display: none">
  </div>
</div>

This posting was similar to the content I presented in my (now deleted) posting: http://blog.mastykarz.nl/inconvenient-provisioning-web-parts-wiki-pages-sandboxed-solutions/

5
  • Ah, that would mean that if I have 100 sites with wiki home-pages, that I would have to manually edit every one of them ahead of time, right? However, this is a good answer.
    – bgmCoder
    Commented Jul 12, 2012 at 2:35
  • No, you could write some powershell to manipulate the page. The basic structure would be to loop through each site, get a reference to the SPFile representing the home page, get a reference to the SPLimitedWebPartManager, then add the web part, get a reference to the web part's ID, then update the WikiField property of the SPFile's ListItem with the markup showed. Where it might get tricky is identifying where to insert the markup.
    – Brian
    Commented Jul 12, 2012 at 2:42
  • In a normal web part page, you identify the web part order, in the WikiField you must put the markup for the web part (shown in answer I provided), in with the rest of the markup (and web parts) in the page. So if each home page has different markup, the best you may be able to do is insert it at the top or bottom of the markup. If each home page has the same markup (i.e. hasn't change since provisioned), you could locate the spot in markup using string manipulation to insert the web part markup.
    – Brian
    Commented Jul 12, 2012 at 2:44
  • Thank you for your comments. Too bad there's no way to contact you via email... I'd like to get your help on some other things...
    – bgmCoder
    Commented Jul 12, 2012 at 2:59
  • Best thing is to post here so the rest of the folks can get access to your questions and you can gain a wide variety of folks responding with (usually) good > great answers. SharePoint is a large platform, there is something to learn new every day with it, despite how long you've been working with it.
    – Brian
    Commented Jul 12, 2012 at 11:41
2

Below Code Reads the SiteUrls from Text file called SiteUrls.txt, and add library called "Oct Invoicing 2014" to the Wikipage called "Billing_Req.aspx"

Here we are taking variable i to check for the library already present on the page, if present we are incrementing i to 1 and else its default value =0

If i=0 means webpart is not present on the page and it will be added on the page.

in All the cases we are writing the output to D:\Webpartoutput.txt.

cls 

asnp "*sh*"

$file=Get-Content -Path D:\SiteUrls.txt

foreach($url in $file)
 {

$web= Get-SPWeb -Identity $url

$listName = "Oct Invoicing 2014"

$pageUrl= $web.GetFile("SitePages/Billing_Req.aspx");

if($pageUrl.Exists)
{

    try
     {

    $i=0; 

    $webpart = New-Object Microsoft.SharePoint.WebPartPages.XsltListViewWebPart

    $list=$web.Lists.TryGetList($listName)

    $webpart.ListId = $list.ID

    $webpart.ViewGuid = ($list.DefaultView.ID).ToString("B").ToUpper()  

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

    foreach($wpt in $webpartmanager.WebParts)
    {

         if($wpt.ListId -eq $list.ID)
         {
         $i=$i+1;
         break;

         }
    }

    if($i -eq "0")
    {

        $webpartmanager.AddWebPart($webpart,"",0)

       "Webpart Added Successfully" + $url + " " + $webpart.Title |  Out-File   -Append -FilePath D:\WebpartOuPut.txt
     }
     else
      {
         " Dude Webpart is already present on the site  : " + $url |  Out-File -Append -FilePath D:\WebpartOuPut.txt 
      }
  }

  catch [Exception]
      {

   $url + $_.Exception.Message | Out-File -Append -FilePath D:\WebpartOuPut.txt 
     }
    }
          else
    {
    "Page Url is invalid or not existing  : " + $url  | Out-File -Append -FilePath D:\WebpartOuPut.txt 
    }
 }

More details in my blog

1
  • Sorry to be so long to respond! What if the webpart is not a list? In particular, I want to add a pageviewer webpart and set its properties to load a particular page.
    – bgmCoder
    Commented Nov 15, 2014 at 22:13

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.