Category Archives: SharePoint-Development

Changing the page layout of a search result page via Powershell in SharePoint 2010

This is a “quick hack” to change the layout of a search result page.

When the Publishing-Feature is disabled you are not able to change the layout via the UI. If you add a new page via “Create page” chances are high that you get a layout you don’t want.

Powershell comes to the rescue …

Add-PSSnapin Microsoft.SharePoint.Powershell -ea SilentlyContinue
clear
 
$w = Get-SPWeb http://path/to/website
$pages = $w.Lists["Pages"]
 
foreach($p in $pages.Items)
{
  # Condition can be arbitrary
  if($p.Title -eq "My Title")
  {
      $p.File.CheckOut()
      $p["PublishingPageLayout"] = "http://server/_catalogs/masterpage/searchresults.aspx"
      $p.Update()
      $p.File.CheckIn("Update page-layout via powershell")
  }
}

Bug with Internet Explorer and grid RowEditing-plugin when using scoped-theme

I came across this rare bug in the current development of a project. It makes heavy use of the RowEditing plugin of Extjs‘ grid. Because the Extjs environment is used within SharePoint 2010 I have to use the scoped theme to avoid “damage” to the SharePoint UI.

After inserting a record into the grid via RowEditing and pressing cancel the RowEditings’ editor window is not shown. You guess it right: only when using Internet Explorer (7, 8, 9). After testing it showed that the css itself is not the reason. So debugging the Extjs-source where the setting scopeResetCSS is evaluated…

The final result is a workaround applied via Ext.override to solve the “problem”.

if(Ext.isIE) {
  Ext.AbstractComponent.override({
    onRender: function(container, position) {
      var me = this,
        el = me.el,
        styles = me.initStyles(),
        renderTpl, renderData, i;
 
      position = me.getInsertPosition(position);
 
      if(!el) {
        if(position) {
          el = Ext.DomHelper.insertBefore(position, me.getElConfig(), true);
        }
        else {
          el = Ext.DomHelper.append(container, me.getElConfig(), true);
        }
      }
      else if(me.allowDomMove !== false) {
        if(position) {
          container.dom.insertBefore(el.dom, position);
        } else {
          container.dom.appendChild(el.dom);
        }
      }
 
      if(Ext.scopeResetCSS && !me.ownerCt) {
 
        if(el.dom == Ext.getBody().dom) {
          el.parent().addCls(Ext.baseCSSPrefix + 'reset');
        }
        else {
          if(me.$className != 'Ext.grid.RowEditor') {
            me.resetEl = el.wrap({
              cls: Ext.baseCSSPrefix + 'reset'
            });
          }
        }
      }
 
      me.setUI(me.ui);
 
      el.addCls(me.initCls());
      el.setStyle(styles);
      me.el = el;
 
      me.initFrame();
 
      renderTpl = me.initRenderTpl();
      if(renderTpl) {
        renderData = me.initRenderData();
        renderTpl.append(me.getTargetEl(), renderData);
      }
 
      me.applyRenderSelectors();
 
      me.rendered = true;
    }
  });
}

The woraround itself is only this:

if(me.$className != 'Ext.grid.RowEditor') {
  me.resetEl = el.wrap({
    cls: Ext.baseCSSPrefix + 'reset'
  });
}

BTW: I “love” IE!

Enable-SPFeature does not trigger the feature receiver

Today I encountered the same problem as stated in a blog post by Christopher Maish. When activating the feature via powershell the associated feature receiver was not called.

Trying the suggested solution in the blog post drove me to the cause of this problem. When using stsadm I got an “access denied” error.
I did run my script via Powershell ISE within Windows Server 2008.

So the real problem is caused, again, by UAC (User Account Control). When starting the Powershell ISE with “Run as administrator” and executing the script the feature receiver is now called as expected. Even when using Enable-SPFeature!

Using powershell to quickly debug SPSiteDataQuery

Writing CAML-Queries is still no fun. Even with SharePoint 2010… So you often need to test your queries before they actually work.

In case you quickly want to test if your SPSiteDataQuery is correct you can use powershell to “debug” it. Just create an instance of the site-collection and target the root-web with your query-instance.

$site = Get-SPSite -Identity http://my-site-collection
 
$q = New-Object -typeName Microsoft.SharePoint.SPSiteDataQuery
$q.Query = "<Where><And><Eq><FieldRef Name='ContentType'/><Value Type='Text'>MyCT</Value></Eq><BeginsWith><FieldRef Name='Url'/><Value Type='URL'>/path</Value></BeginsWith></And></Where>"
$q.ViewFields = "<FieldRef Name='ID'/><FieldRef Name='ContentType'/>"
$q.Webs = "<Webs Scope='SiteCollection' />"
 
$site.RootWeb.GetSiteData($q)

This is way faster than doing it in VS…

Using a C# preprocessor directive for testing custom timer-jobs

If your are writing custom timer-jobs for your SharePoint solution it will happen that you write jobs which are only executed once a day or even less often. As a developer you cannot wait that long…

You could of course comment in and out (via ctrl + k, ctrl + c / ctrl + k, ctrl + u) the SPSchedule used by the timer-job during debug-mode. But this is error-prone.

When using a C# preprocessor directive you cannot forget to switch back to the “production schedule” as this is done automatically (and even with syntax-highlighting depending on the active configuration!) when selecting the “release build”.


#if (DEBUG)
var schedule = new SPMinuteSchedule();
schedule.BeginSecond = 1;
schedule.EndSecond = 59;
schedule.Interval = 5;
#else
var schedule = new SPDailySchedule();
schedule.BeginHour = 7;
schedule.EndHour = 7;
schedule.BeginMinute = 1;
schedule.EndMinute = 59;
#endif

This code tells the compiler to use the SPMinuteSchedule when compiling the “debug build” and to use the SPDailySchedule when compiling in “release build”. All automatically!