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.
dinsdag 19 maart 2013
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 ![]()
- 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.
donderdag 9 augustus 2012
Add jQuery library to a webpart via code
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:
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>";
if (!Page.ClientScript.IsClientScriptBlockRegistered(jsKey))