Sunday, October 30, 2011

SharePoint: CAML get all calendar items from including recurring items

Situation:
I want to get all calendar items from today onwards including any recurring event items.

Problem:
CAML and SharePoint can be a pain.

Solution:
SPQuery query = new SPQuery();
string queryString =
@"
    <Where>
     <DateRangesOverlap>
      <FieldRef Name=""EventDate""/>
      <FieldRef Name=""EndDate""/>
      <FieldRef Name=""RecurrenceID""/>
      <Value IncludeTimeValue=""TRUE"" Type=""DateTime"">
       <Now/>
      </Value>
     </DateRangesOverlap>
    </Where>
    <OrderBy>          
 <FieldRef Name=""EventDate"" Ascending=""TRUE""/>      
    </OrderBy>
";
query.Query = queryString;
query.ExpandRecurrence = true;

Note:
You should add a query.RowLimit so you don't get too many items back. But that's up to you.

Sunday, October 23, 2011

SharePoint Site Template custom Navigation

Ok this should be simple. It should be straight forward. I should have known better.

Scenario: I'm building a SharePoint 2010 web template.
What I want to do: I want to display two links in the top nav (Home & Shared Documents). I want this navigation to be available as soon as the Site collection is created. There's an obvious place in the onet.xml file to place this. But this area is poorly documented. I had to do a bit of trial and error to achieve what I wanted.

This is the solution to my problem but I have spent way too much time to achieve something so simple.

<NavBars>
 <NavBar Name="$Resources:core,nav_Home;" Separator="&amp;nbsp;&amp;nbsp;&amp;nbsp;" Body="&lt;a ID='onettopnavbar#LABEL_ID#' href='#URL#' accesskey='J'&gt;#LABEL#&lt;/a&gt;" ID="1002" />
 <NavBar Name="SharePoint Top Navbar" ID="1002">
  <NavBarLink Name="Shared Documents" Url="Shared%20Documents/">
  </NavBarLink>
 </NavBar>
</NavBars>

"SharePoint Top Navbar" is the top navigation nav bar. If you want to add links to the top navigation you need to add them in this node.

Sunday, October 9, 2011

Restoring a site collection and getting a "different version of Microsoft SharePoint Foundation" error

Scenario:
Restoring a site collection backup between 2 development environments. I'm using the following Powershell command:
Restore-SPSite -Identity http://spdev/sites/ctmh -Path c:\fpbackups\ctmh.bak
Where spdev is the name of my SharePoint Dev server.
ctmh.bak is the backup file.

Symptom:
Restore-SPSite : Your backup is from a different version of Microsoft SharePoint Foundation and cannot be restored to a server running the current version. The backup file should be restored to a server with version '4.1.9.0' or later.

Steps to get it working:
It seemed pretty straight forward although the version '4.1.9.0' seemed a little strange.
First step is to compare both environment's SharePoint version:
  1. SharePoint 2010 Central Administration
  2. Upgrade and Migration
  3. Check product and patch installation status
I looked up the version number on my favourite search provider and got the appropriate updates. Pretty straight forward really.

I forgot to run the "SharePoint 2010 Products Configuration Wizard" initially and the restore didn't work. So if the SharePoint versions are the same between environments it might be worth while running the wizard to check that the patches have been applied.