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.
maandag 11 maart 2013
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 ![]()
- Add-PSSnapin Microsoft.SharePoint.PowerShell
- Start-SPAssignment -Global # This cmdlet takes care of the disposable objects to prevent memory leaks
- function AddCustomWebPart($web,$page,[string]$webpart_nameSpace,[string]$webpartTitle,[int]$index)
- {
- $comment = $webpartTitle + " WebPart Added"
- $webpartmanager=$web.GetLimitedWebPartManager($page.Url, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
- $wpsOnPage = $webpartmanager.WebParts | % { $_ }
- foreach($wp in $wpsOnPage)
- {
- if($wp -ne $null)
- {
- if($wp.Title -eq $webpartTitle)
- {
- Write-Host "webpart " $wp.Title " exists on page. "
- $webpartmanager.DeleteWebPart($webpartmanager.WebParts[$wp.ID]);
- }
- }
- }
- $webpart = new-object $webpart_nameSpace
- $webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::Default;
- $webpart.Title = $webpartTitle
- $webpart.Width = "300px"
- $webpartmanager.AddWebPart($webpart, "RightColumn", $index);
- }
- function CheckInAndPublishPage($comment)
- {
- " Checking in page"
- $page.CheckIn($comment)
- # Publish
- if($page.listItem.ParentList.EnableMinorVersions -eq $true -and $publishingPage.ListItem.File.MinorVersion -ne 0)
- {
- " Publishing"
- $page.listItem.File.Publish($comment)
- }
- # If moderation is being used handle the approval
- if ($page.listItem.ParentList.EnableModeration)
- {
- $modInformation = $page.listItem.ModerationInformation
- " Moderation on, Current Status: " + $modInformation.Status
- # Check for pending approval
- if($modInformation.Status -ne [Microsoft.SharePoint.SPModerationStatusType]::Approved)
- {
- " Approving"
- $page.ListItem.File.Approve($comment)
- }
- }
- }
- $webUrl = "http://intranet/sites/subweb/"
- $web = Get-SPWeb $webUrl
- $list = $web.Lists["Pagina's"]
- Write-Host $list.Title
- $pages = $list.Items
- foreach($item in $pages)
- {
- $page = [Microsoft.SharePoint.Publishing.PublishingPage]::GetPublishingPage($item)
- Write-Host "Page Url: " $item.Url
- Write-Host "Page ContentType: " $item.ContentType.Name
- $page.CheckOut()
- AddCustomWebPart $web $page "SharePoint.Intranet.WebParts.Bijlagen" "Bijlagen" 3
- AddCustomWebPart $web $page "SharePoint.Intranet.WebParts.ProcesbeschrijvingBijlagen" "Procesbeschrijving bijlagen" 4
- CheckInAndPublishPage("Bijlage webparts toegevoegd.")
- }
- # Clean up
- $web.Close()
- Write-Host Done.
- # Add an empty line
- Write-Host ""
- # Clean up
- Write-Host "Cleaning up..."
- Stop-SPAssignment -Global
- Remove-PsSnapin Microsoft.SharePoint.PowerShell
- Write-Host "Press any key to continue ..."
- $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.
- public class SPListItemCollectionAdapter : List<SPListItem>
- {
- private SPListItemCollection _listItemCollection;
- /// <summary>
- /// Get a generic list of SPListItems and convert it
- /// </summary>
- /// <param name="listItemCollection">SPListItemCollection collection of items</param>
- public SPListItemCollectionAdapter(SPListItemCollection listItemCollection)
- {
- _listItemCollection = listItemCollection;
- Refresh();
- }
- /// <summary>
- /// Convert a SPListItemCollection into a generic list
- /// </summary>
- private void Refresh()
- {
- this.Clear();
- foreach (SPListItem item in _listItemCollection)
- {
- this.Add(item);
- }
- }
- }
Now you can use the class above like:
- private SPListItemCollectionAdapter GetItems(SPList list, string query)
- {
- SPQuery q = new SPQuery();
- q.Query = query;
- return new SPListItemCollectionAdapter(list.GetItems(q));
- }
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.
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
List of all the required ContentPlaceHolders:
woensdag 5 december 2012
SharePoint list view group by content type
SharePoint 2010 ViewEdit Group By Content Type
Actually it’s nothing more than adding some javascript with a script link to each page.