Thursday, October 21, 2010

DisableEventFiring is obsolete in SharePoint 2010

You can check this in msdn. However DisableEventFiring()/EnableEventFiring() still works as per Reflector.
Recommended way now is to use new property EventFiringEnabled. This gives us an option to check current status and save it. I found a great post from Adrian Henke and modified his code using this new feature.



The class DisabledItemEventsScope disable/enable event firing for the current thread and could be used in that manner at any place including code behind for the page or web part



BTW: There are couple useful SPList extensions to manage SPListItemEventReceiver collection

Monday, October 11, 2010

SPUrl for WSS 3.0 or SharePoint Foundation

No, expression like <% $SPUrl:~sitecollection/Style Library/mystyles.css %> doesn't work for WSS or SharePoint Foundation. This is part of Publishing Infrastructure and you have to buy MOSS or SharePoint Server license to get it.

So, let's create our own ExpressionBuilder. I didn't implement 'language' support - obviously I need just Site and SiteCollection.


...
using System.Web.Compilation;
using Microsoft.SharePoint;

...
public class WSSUrlExpressionBuilder : ExpressionBuilder
{
public override CodeExpression GetCodeExpression
(System.Web.UI.BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
{
CodeTypeReferenceExpression thisType = new CodeTypeReferenceExpression(base.GetType());

CodePrimitiveExpression expression = new CodePrimitiveExpression(entry.Expression.Trim().ToString());

string evaluationMethod = "GetKeyValue";

return new CodeMethodInvokeExpression(thisType, evaluationMethod, new CodeExpression[] { expression });
}

public static object GetKeyValue(string expression)
{
SPWeb web = SPContext.Current.Web;

string key = "~SiteCollection";
if (expression.IndexOf(key, StringComparison.InvariantCultureIgnoreCase) == 0)
return web.Site.Url+expression.Substring(key.Length);

key = "~Site";
if (expression.IndexOf(key, StringComparison.InvariantCultureIgnoreCase) == 0)
return web.Url+ expression.Substring(key.Length);

return expression;
}

}
...


You need to put this class to some assembly and register it with your prefix(e.g. WSSUrl) in Web.Config


<configuration>
<system.web>
<compilation>
<expressionBuilders>
<add expressionPrefix="WSSUrl"
type="{your Namespace}.WSSUrlExpressionBuilder,{Assembly Name}"/>
</expressionBuilders>
</compilation>
</system.web>
</configuration>


Assembly Name should be fully qualified if you put the assembly into GAC.

Thursday, October 7, 2010

SharePoint: How to get document icon image

There is a method in SPUtility which makes it much easier. It takes image file name from docicon.xml, and should work Ok even if the file name is not follow the pattern ic{doc ext}.gif


string docIcon = SPUtility.ConcatUrls("/_layouts/images/",
SPUtility.MapToIcon(item.Web,
SPUtility.ConcatUrls(item.Web.Url,item.Url), "", IconSize.Size16));


There is a good post about SPUtility.MapToIcon vs SPFile.GetIcon

Friday, September 10, 2010

SharePoint 2010: How to add back QuickLaunch to the web part page.

Actually this is not adding but just removing hiding functionality:

1. Remove this code/css from the page

<SharePoint:UIVersionedContent ID="WebPartPageHideQLStyles" UIVersion="4" runat="server">
<ContentTemplate>
<style type="text/css">
body #s4-leftpanel {
display:none;
}
.s4-ca {
margin-left:0px;
}
</style>
</ContentTemplate>
</SharePoint:UIVersionedContent>


2. Remove this place holder from the page

<asp:Content ContentPlaceHolderId="PlaceHolderLeftNavBar" runat="server"></asp:Content>

Thursday, September 2, 2010

Restart SPTimerV4 service remotely in the SharePoint farm

There is a PowerShell script which restarts SPTimerV4 service remotely on every application server within a farm. This is useful when deployment of a SPTimerJob has been done.

It should be run on one of the application servers under SharePoint 2010 Management Shell, and you have to have enough permissions of course.

foreach ($server in (get-spserver | Where {$_.Role -eq "Application"}) )
{
write-host $server.Name
$service = Get-Service -computer $server.Name -name SPTimerV4
if ($service.Status -ne "Stopped")
{
$service.Stop()
Do
{
start-sleep -s 5 | out-null
write-host '.' -noNewLine
$service = Get-Service -computer $server.Name -name SPTimerV4
}
While ($service.Status -ne "Stopped")
write-host $service.Status
}
else
{
write-host "Stopped already"
}
if ($service.Status -ne "Running")
{
$service.Start()
Do
{
start-sleep -s 5 | out-null
write-host '.' -noNewLine
$service = Get-Service -computer $server.Name -name SPTimerV4
}
While ($service.Status -ne "Running")
write-host $service.Status
}
}

It shows the state of the service for each server. So, you can go and restart it manually if something was going wrong.

Tuesday, August 31, 2010

Hide SharePoint 2010 Ribbon Control from public users

There are some posts about how to hide SharePoint 2010 Ribbon from users with least permissions. One example is here. But it trims the ribbon from the page, which could cause some issues. Most known is a scroll bar disappearing.
BTW: there is a perfect article about SharePoint 2010 Ribbon Control from its designer.


There is the way how to real hide it depending of current user permissions (owner permission in this example). Just add this to the head section on master page.

<style type="text/css">
div#s4-ribbonrow {
display:none;
}
</style>

<Sharepoint:SPSecurityTrimmedControl runat="server"
Permissions="AddAndCustomizePages">
<style type="text/css">
div#s4-ribbonrow {
display:block;
}
</style>
</Sharepoint:SPSecurityTrimmedControl>