The SharePoint Trench

Dug in with SharePoint

SharePoint Summit 2012 : SharePoint Training Event

SPS_2012_Webbanner_content_ad_v2The city of Cologne, Germany will host this years’ SharePoint Summit 2012. SharePoint Summit is not the classical conference – it’s a training event. Five Top-of-The-Line SharePoint experts will share knowledge in a two track half-day workshop style.

Unlike 60 minute sessions, participants will dive deep into the matter in the half day workshops.

 

Join us in Cologne, April 25 – 27 2012 for the one-of-a-kind SharePoint Summit 2012

 

www.sharepoint-summit.de

SharePoint 2010 new Synchronous "After” Event Model

It still is not a very commonly known enhancement, SharePoint 2010 introduces the possibility to run “after” events like ItemAdded, WebAdded and FieldAdded and so on, which normally are executed asynchronously, synchronously.

Figure 1 shows how events are executed. Up until SP 2010 the “After” events each where executed on a separate thread. This might present an issue when an event receiver needs the new item’s data and change the user experience based on that.

 

For example: There was no way of redirecting the user in an after event, because the thread running the HttpContext finished while the threads running the events still where running – also the HttpContext is not available in any other than the main thread of course.

 

 

Let’s elaborate on the example given: It is a common requirement to redirect the user to a custom page after a new item is created via NewForm.aspx.

There are several ways to do so. In SharePoint Designer 2007 there was an easy way using concatenated form actions.

Unfortunately this is not so easy in SharePoint 2010, due to form actions being defined as ribbon actions.[URL]

Another way is by deriving from SaveButton and creating a custom implementation that redirects with SPUtility.Redirect(..). This custom button implementation can be placed in a custom control template and used to render the respective list form. [URL]

While this works, it has a major drawback – it will not work in Sandboxed Solutions, due to the fact that we need to deploy a custom control template into the 14 hive.

 

The solution is creating an event receiver that redirects the user.Below is a sample implementation of an “ItemAdding” receiver that does just that:

   1: private readonly HttpContext _current;

   2:  

   3:       public ItemAddingRedirectHandler()

   4:       {

   5:           _current = HttpContext.Current;

   6:       }

   7:  

   8:       /// <summary>

   9:       /// Redirects the user to the edit page

  10:       /// </summary>

  11:       /// <param name="properties">Contains list event properties</param>   

  12:       [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]

  13:       public override void ItemAdding(SPItemEventProperties properties)

  14:       {

  15:           var eventHandlerClassName = this.GetType().FullName;

  16:           Trace.TraceInformation("[TIES]: Entering event handler " + eventHandlerClassName);

  17:           try

  18:           {

  19:               EventFiringEnabled = false;

  20:  

  21:               // get the list which item to be added

  22:               var objsite = new SPSite(properties.SiteId);

  23:               var objweb = objsite.OpenWeb(properties.RelativeWebUrl);

  24:               var objlist = objweb.Lists[properties.ListId];

  25:  

  26:               var itemToAdd = objlist.Items.Add();

  27:               //get item values from event receiver properties

  28:               foreach (SPField field in properties.List.Fields)

  29:               {

  30:                   var val = properties.AfterProperties[field.InternalName];

  31:                   if (val != null && val.ToString() != string.Empty)

  32:                       itemToAdd[field.Id] = val;

  33:               }

  34:  

  35:               //add item to list

  36:               itemToAdd.Update();

  37:               EventFiringEnabled = true;

  38:  

  39:               //redirect it to new destination (default Edit Form)

  40:               var urlParams = "?Id=" + itemToAdd.ID;

  41:               var editFormUrl = objlist.DefaultEditFormUrl + urlParams;

  42:               Trace.TraceInformation("[TIES]: Leaving event handler " + eventHandlerClassName);

  43:               SPUtility.Redirect(editFormUrl, SPRedirectFlags.Trusted, _current);

  44:  

  45:           }

  46:           catch (ThreadAbortException tex)//Catch, this will happen as the thread is aborted

  47:           {

  48:               Trace.TraceInformation("[TIES]: Error in event handler " + eventHandlerClassName + tex.Message + tex.StackTrace);

  49:           }

  50:           catch (Exception ex)

  51:           {

  52:               Trace.TraceInformation("[TIES]: Error in event handler " + eventHandlerClassName + ex.Message + ex.StackTrace);

  53:               properties.Cancel = true;

  54:               properties.ErrorMessage = "Error in redirect event";

  55:           }

  56:           finally

  57:           {

  58:               EventFiringEnabled = true;

  59:           }

  60:       }

It is important to note that we retrieve that Current HttpContext into a private field in the constructor! Don’t forget that as there is no way of accessing the Context in the ItemAdding method itself. Also the field that holds the reference to the context MUST NOT BE STATIC.

Also note that the values of the list item must be retrieved from the AfterProperties. The way it is implemented above is not the most elegant and safe,but it works.

 

—and DONE!

 

Figure 1 taken from:

http://extreme-sharepoint.com/2011/12/27/event-receivers-sharepoint-2010/

 

Happy SharePoint’ing

SharePoint 2010 Product Updates: SharePoint 2010 December 2011 Cumulative Update

As a christmas present the SharePoint Product team has realeased the december 2011 Cumulative Update Packages for SharePoint Foundation, SharePoint Server 2010 and Project Server 2010.

 

The updates and relevant KB articles detailing the contents can be found here

 

SharePoint Foundation 2010

KB: http://support.microsoft.com/default.aspx?scid=kb;EN-US;2597058

Download: http://support.microsoft.com/hotfix/KBHotfix.aspx?kbnum=2597058&kbln=en-us

 

SharePoint Server 2010

KB:  http://support.microsoft.com/default.aspx?scid=kb;EN-US;2597014

Download: http://support.microsoft.com/hotfix/KBHotfix.aspx?kbnum=2597014&kbln=en-us

 

The CU Packages are “uber” packages meaning the are really cumulative Smile . So SPS2010 CU contains SPF2010 and all previous CU’s and hotfixes.

 

Happy SharePoint’ing !

Generating CAML: Using Linq – to – SharePoint to generate complex CAML queries

Just a quick tip on mastering these complex projections and other stuff.

Use Linq-to-SharePoint to generate Entity classes, then set the LINQ Providers “Log” Property to log the generated CAML to a text file and finally – use that CMAL to create your queries.

Excellent post about this here >> http://ranaictiu-technicalblog.blogspot.com/2011/01/u2u-caml-query-builder-for-sharepoint.html

Get-SPWeb CmdLet Errors: Get-SPWeb fails with “Web not found”

Just a quick fix for those needing to adjust permissions to deploy solutions via SharePoint’s Powershell CmdLets.

Recently we had to do a deployment on a customer SharePoint Farm. As usual we had the administrator give our deployment account shell administration rights via add-spshelladmin – UserName [username].

That does the trick, but one thing remained mysterious – the Get-SPWeb Cmdlet would always fail with the error “Cannot find SPWeb object with Id ur URL”.

The solution:

add-spshelladmin accepts an additional parameter called –database, to give the user the respective roles not only on the SharePoint Configuration database but also on the respective SharePoint Web Application Content Database. See the following snippet for an example.

 

> $contentDB = get-spcontentdatabase -site http://server/site
> add-spshelladmin -UserName DOMAIN\Username -database $contentDB

 

Happy SharePoint’ing !

Real-World SharePoint: Troubleshoot Errors in Microsoft SharePoint Configuration Wizard

Lately we had a new farm setup by the very convenient AutoSPInstaller Script. Everything worked fine up until the point where a language pack and the corresponding SP to it was to be installed. After the SP installation psconfig (a.k.a. SharePoint Product Configuration Wizard) was executed and after some minutes run time reported an error and aborted the upgrade.

After some investigation we found that the WWW Publishing Service was stopped after psconfig errored out. Some deeper digging revealed that Windows Process Activation was also stopped – the WWW service depends on it.

Two errors lead us to the root of the problem – see screens below.

image

 

image

 

The first error – reported by IIS – stated that some application pool config could not be created. IIS has a directory for those XML files at C:\inetpub\temp\apppools. Some users report that this directory seems to be missing on some servers alltogether. In our case the issue was more complex. Our farm account does not have local admin rights, and by this has no access to the directory.

Two steps resolved the issue:

    • Grant WSS_ADMIN_WPG rights to the above directory
    • Give the application pool accounts “Local Activation” Rights for DCOM

The permissions can be set by typing “Component Services” into the start menu, that should show the Component Services Configuration Application. Launch it then open “Component Services” > Computers > My Computer > Properties (see below).

image

 

In the COM Security tab under Launch and Activation Permissions, give all your application pool identities the “Local Activation” permission – done.

 

Hope that helps – Happy SharePoint’ing!

August CU: August 11 Cumulative Update for SharePoint Server / Foundation is out!

The new August 11 CU for SharePoint Server 2010, SharePoint Foundation 2010 and Project Server 2010 is out.

There is big news as to how Microsoft changed the packaging. For the first time the August 11 CU is delivered as an “Uber” Package. This means – no more downloading of multiple packages. The SPF package includes SPF. The SP Server package contains both (SPS & SPF), the Project Server package inluces PS,SPS and SPF !

 

Get them all here:

SharePoint Foundation 2010 – August 2011 CU

SharePoint Server 2010 – August 2011 CU

Project Server 2010 – August 2011 CU

 

As always

 

Happy SharePoint’ing

Automated SharePoint Deployment: AutoSPInstaller

Installation and configuration of SharePoint Test and Staging System can be a cumbersome task.

 

AutoSPInstaller provides a framework to automate a complete farm setup. Every aspect of the farm configuration is configurable – from service accounts to service database names.

Once all files (SP Bits, Cumulative Updates, Office Web Apps, Service Packs,..) are in place AutoSPInstaller takes care of installing and configuring everything.

 

Get it here –> http://autospinstaller.codeplex.com/

SharePoint 2010 DataSheet View: Making DataSheet View work on 64-bit Systems

The Datasheet View in SharePoint lists is a very popular feature among end users. From a technical perspective the implementation as an ActiveX Control is not a good idea. JavaScript would be a the technology of choice here in the year 2011 – but that is another story.

One common issue with the datasheet view is that it refuses to work on client systems with a 64bit edition of Microsoft Office. A simple work around to this is to install the 32bit Data Connectivity Components. It is a ~25MB download, installation is only a matter of seconds – so that is the “least friction” solution to that. There are other solutions out there suggesting to install any 32bit 2007 Office Product (such as SharePoint Designer 2007) alongside the Office 2010 on the client, but that seems to much for just making datasheet view work Smile

 

Happy SharePoint’ing!