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.

dinsdag 8 januari 2013

SPListItemCollection with Linq

I like to use linq to find, sort, .. items in a list quick and easily, but it’s a pain to convert a SPListItemCollection into a generic list. Today I wrote a class that do the conversion for me easily.

Code Snippet
  1. public class SPListItemCollectionAdapter : List<SPListItem>
  2.     {
  3.         private SPListItemCollection _listItemCollection;
  4.  
  5.         /// <summary>
  6.         /// Get a generic list of SPListItems and convert it
  7.         /// </summary>
  8.         /// <param name="listItemCollection">SPListItemCollection collection of items</param>
  9.         public SPListItemCollectionAdapter(SPListItemCollection listItemCollection)
  10.         {
  11.             _listItemCollection = listItemCollection;
  12.  
  13.             Refresh();
  14.         }
  15.  
  16.         /// <summary>
  17.         /// Convert a SPListItemCollection into a generic list
  18.         /// </summary>
  19.         private void Refresh()
  20.         {
  21.             this.Clear();
  22.  
  23.             foreach (SPListItem item in _listItemCollection)
  24.             {
  25.                 this.Add(item);
  26.             }
  27.         }
  28.     }

Now you can use the class above like:

Code Snippet
  1. private SPListItemCollectionAdapter GetItems(SPList list, string query)
  2.         {
  3.             SPQuery q = new SPQuery();
  4.             q.Query = query;
  5.  
  6.             return new SPListItemCollectionAdapter(list.GetItems(q));
  7.         }

The only thing you need to do is declare a variable of the type SPListItemCollectionAdapter and create a new one.

Isn’t it easy?

dinsdag 18 december 2012

SharePoint 2010: Hiding the Recently Modified on Team Sites

I was asked today about hiding the Recenlty Modified menu from the quicklaunch of SharePoint 2010 Team Sites.

image

SharePoint 2010: Expand the User Profile info by default

Out of the box, the SharePoint 2010 User Profile page uses a bit of JavaScript to truncate the user’s info like Email, School etc. with a “more information” hyperlink that can be clicked to show all the info.  The user’s description text is also similarly semi-hidden on page load.

The following quick CSS hack can make both these detail areas fully visible by default, and also hide the “more information” / “hide information” hyperlink”:

SharePoint 2010: minimal.master

When you create a custom minimal.master it’s also required to have all the placeholders defined. We already know this from the v4.master but why are they needed when they are not used? They are necessary for the edit mode of the minimal masterpage. When you edit a minimal masterpage and you have only defined the minimal.master placeholder you get error messages like “Cannot find ContentPlaceHolder ‘PlaceHolderLeftNavBar’ in the master page '~masterurl/default.master', verify content control's ContentPlaceHolderID attribute in the content page.”

List of all the required ContentPlaceHolders:

woensdag 5 december 2012

SharePoint list view group by content type

By default it’s not possible to group your list items by contenttype. After some research on internet I founded a solution on codeplex:

SharePoint 2010 ViewEdit Group By Content Type

Actually it’s nothing more than adding some javascript with a script link to each page.