Posts tonen met het label webpart. Alle posts tonen
Posts tonen met het label webpart. Alle posts tonen

dinsdag 19 maart 2013

SharePoint 2010: Show lync presence in custom webpart

My customer asked for a contact details webpart with the feature if the contact is available on Lync or not. It isn’t difficult to get the contact details from a user profile in contrast to get the lync presence. I spend a few hours to get how it works in SharePoint 2010 and finally I found this blog post of Martin Kearn. With a little bit of fine tuning I rewrote the function to let it work with the UserProfile property instead of a SPFieldUserValueCollection.

maandag 11 maart 2013

SharePoint 2010 Contact Details WebPart

A customer asked me to write a webpart to have the possibility to show contact details of a user based on the userprofile. Someone told me that this webpart could be useful in other projects too. I reviewed my code an made it generic so it can be used in every SharePoint 2010 where are userprofiles present. It’s also expanded with localization. The webpart is available in English, Dutch and French.

woensdag 6 februari 2013

Add webparts to pages via PowerShell

This week I created some webparts. Those webparts must be added to more than 1000 pages in SharePoint. It’s an awful job to do it manually, so I created a script in PowerShell that do the job for me Glimlach

Code Snippet
  1. Add-PSSnapin Microsoft.SharePoint.PowerShell
  2. Start-SPAssignment -Global    # This cmdlet takes care of the disposable objects to prevent memory leaks
  3.  
  4. function AddCustomWebPart($web,$page,[string]$webpart_nameSpace,[string]$webpartTitle,[int]$index)
  5. {
  6.     $comment = $webpartTitle + " WebPart Added"
  7.  
  8.     $webpartmanager=$web.GetLimitedWebPartManager($page.Url,  [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
  9.     
  10.     $wpsOnPage = $webpartmanager.WebParts | % { $_ }
  11.     foreach($wp in $wpsOnPage)
  12.     {
  13.         if($wp -ne $null)
  14.         {
  15.             if($wp.Title -eq $webpartTitle)
  16.             {
  17.                 Write-Host "webpart " $wp.Title " exists on page. "
  18.                 $webpartmanager.DeleteWebPart($webpartmanager.WebParts[$wp.ID]);
  19.             }
  20.         }
  21.     }
  22.     
  23.     $webpart = new-object $webpart_nameSpace
  24.     $webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::Default;
  25.     $webpart.Title = $webpartTitle
  26.     $webpart.Width = "300px"
  27.     
  28.     $webpartmanager.AddWebPart($webpart, "RightColumn", $index);    
  29. }
  30.  
  31. function CheckInAndPublishPage($comment)
  32. {
  33.     " Checking in page"
  34.     $page.CheckIn($comment)
  35.  
  36.     # Publish
  37.     if($page.listItem.ParentList.EnableMinorVersions -eq $true -and $publishingPage.ListItem.File.MinorVersion -ne 0)
  38.     {
  39.             " Publishing"
  40.             $page.listItem.File.Publish($comment)
  41.     }
  42.  
  43.     # If moderation is being used handle the approval    
  44.     if ($page.listItem.ParentList.EnableModeration)
  45.     {
  46.  
  47.         $modInformation = $page.listItem.ModerationInformation
  48.         
  49.         " Moderation on, Current Status: " + $modInformation.Status
  50.  
  51.         # Check for pending approval
  52.         if($modInformation.Status -ne [Microsoft.SharePoint.SPModerationStatusType]::Approved)
  53.         {
  54.             " Approving"
  55.             $page.ListItem.File.Approve($comment)
  56.         }
  57.     }
  58. }
  59.  
  60. $webUrl = "http://intranet/sites/subweb/"
  61. $web = Get-SPWeb $webUrl
  62.     
  63. $list = $web.Lists["Pagina's"]
  64. Write-Host $list.Title
  65. $pages = $list.Items
  66.  
  67. foreach($item in $pages)
  68. {
  69.     $page =  [Microsoft.SharePoint.Publishing.PublishingPage]::GetPublishingPage($item)
  70.     Write-Host "Page Url: " $item.Url
  71.     Write-Host "Page ContentType: " $item.ContentType.Name
  72.     
  73.     $page.CheckOut()
  74.         
  75.     AddCustomWebPart $web $page "SharePoint.Intranet.WebParts.Bijlagen" "Bijlagen" 3
  76.     AddCustomWebPart $web $page "SharePoint.Intranet.WebParts.ProcesbeschrijvingBijlagen" "Procesbeschrijving bijlagen" 4
  77.     
  78.     CheckInAndPublishPage("Bijlage webparts toegevoegd.")
  79. }
  80.  
  81. # Clean up
  82. $web.Close()
  83.  
  84. Write-Host Done.
  85.  
  86. # Add an empty line
  87. Write-Host ""
  88.  
  89. # Clean up
  90. Write-Host "Cleaning up..."
  91. Stop-SPAssignment -Global
  92. Remove-PsSnapin Microsoft.SharePoint.PowerShell
  93.  
  94. Write-Host "Press any key to continue ..."
  95. $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Isn’t it wonderful? The script also check if the webpart is already on the page or not. If it’s already added than it will be deleted and added again to respect the order of the webparts. For some reason SharePoint doesn’t respect the order.

If someone knows why SharePoint ignores the order of the webparts feel free to let me know.

donderdag 9 augustus 2012

Add jQuery library to a webpart via code

How to load your jQuery library at the same time as your webpart?

Add the jQuery library reference via code to your webpart.

protected override void Render(HtmlTextWriter writer)
{
 writer.AddAttribute(HtmlTextWriterAttribute.Src, "/_layouts/1043/STYLES/myUZA/js/jquery-1.4.2.min.js");
 writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/javascript");
 writer.RenderBeginTag(HtmlTextWriterTag.Script);
 writer.RenderEndTag();
}

Another option is to do it in a full javascript block. You write the javascript code, put it in a string and register the javascript block via C# code:

//Load the jQuery library if it's not yet loaded
string js = @"<script type=""text/javascript"">
<">if (typeof jQuery == 'undefined') {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '/_layouts/1043/STYLES/myUZA/js/jquery-1.4.2.min.js';
document.getElementsByTagName('head')[0].appendChild(script);
}
</script>"
;

/// Register the javascript block
if
(!Page.ClientScript.IsClientScriptBlockRegistered(jsKey))
{
this.Page.ClientScript.RegisterClientScriptBlock(GetType(), "jQueryLib", js, false);
}