Creating iGoogle UI for SharePoint 2010/SharePoint 2013 - Part V : SharePoint 2010 Integration


Series Content

  1. Creating iGoogle UI for SharePoint 2010/SharePoint 2013 - Part I : Overview, Concept, HTML Structure & Jquery Basics

  2. Creating iGoogle UI for SharePoint 2010/SharePoint 2013 - Part II : Dragging, Dropping, Sorting and Collapsing

  3. Creating iGoogle UI for SharePoint 2010/SharePoint 2013 - Part III : Saving WebPart states using Cookies

  4. Creating iGoogle UI for SharePoint 2010/SharePoint 2013 - Part IV : Control Adapters

  5. Creating iGoogle UI for SharePoint 2010/SharePoint 2013 - Part V : SharePoint 2010 Integration


Overview

In Part Five, we will take the previous posts and show you how to get it into SharePoint 2010. I’ll show how to create the Visual Studio Project, and then deploy the assets into SharePoint to create a working example.

Visual Studio Project & Assets

For this post will be using Visual Studio 2010 as our development platform. As part of my default development build i like to have the following VS plug-ins installed.
For this post, we will be using Visual Studio 2010 as our development platform. As part of my default development build, I like to have the following VS plug-ins installed:

SPI’s & Features

Our project will contain the following SPI’s (SharePoint Item) to deploy the required assets.
  • Module – This will deploy an iGoogle.aspx page with the configured WebPart Zones, and some dummy content editor webparts (CEWP) to show a working example.
  • Mapped Folder – A Mapped folder to the /_layouts directory will be used to deploy the css, images, and javascript files required and created from the first three parts of this series.
  • Control Adapter – Although technically not an SPI, the control adapter code from the previous post will also be created.
The Project will contain only a single feature which will deploy all the assets required for the iGoogle interface. This will be a SITE scoped feature with an event receiver to manage the addition of values to the compact.browser file.

Visual Studio

The first step for this and pretty much every other SharePoint Project is to fire up Visual Studio 2010 and create a new SharePoint 2010 Project. Call the project LifeInSharePoint.iGoogle. On the next screen we would also like to create this as a FARM solution. Sandbox solutions will not work as control adapters cannot be deployed using a Sandbox Solution.
Now that we have a project created, we first need to create some folders to contain our SPI’s. I like to organise my folders in a manner that I feel makes it easier to understand, so I will first create a Common folder which will contain a sub folder called ControlAdapters. NOTE: I do not have spaces in my folder names as visual studio will replace them with “_” in namespaces. I will now create another top level folder called Root and within this I will create another folder called Content. These two folders will contain the module that will deploy the iGoogle.aspx page and place WebParts onto the page. To ensure that we can access the images, js and css from anywhere, we will place them in the /_layouts folder. To deploy these to the Layouts folder from Visual Studio is very simple. Firstly you will need to right click on the project in Visual Studio > Add > SharePoint “Layouts” Mapped Folder.
This will create you a project named sub folder which we can use to place our css etc. Once this has been done your folder structure should look like this.
Now that I have the basic folder structure, I will now create a new a new class file for my ControlAdapter called WebPartRenderControlAdapter.cs. For the info on how to create and what goes into this class file, please see the previous post where I go into a lot more detail. Creating iGoogle UI for SharePoint 2010/SharePoint 2013 - Part IV : Control Adapters
The next step is to add the CSS, JS, and Image that we created in the first three parts of this blog series. (These will be available at the end of this post) in the supplied zip file.

Adding Content & Pages

Next, we need to create the root content module. This module contains two items. The first is the Elements.xml file which will contain the XML required to deploy our page, and the second item is the default.aspx page which we will provision. This default.aspx page contains the HTML snippets from the first couple of posts in this series as well as the references to our javascript and css which we are storing above in the /_layouts folder. Below is a snippet from within the default.aspx page.


<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
<table cellspacing="0" border="0" width="100%">
<tr class="s4-die">
<td class="ms-pagebreadcrumb">
<asp:SiteMapPath SiteMapProvider="SPContentMapProvider" id="ContentMap" SkipLinkText="" NodeStyle-CssClass="ms-sitemapdirectional" runat="server"/>
</td>
</tr>
<tr>
<td class="ms-webpartpagedescription"><SharePoint:ProjectProperty Property="Description" runat="server"/></td>
</tr>
<tr>
<td>
<table width="100%" cellpadding="0" cellspacing="0" style="padding: 5px 10px 10px 10px;">
<tr>
<td valign="top">
<div class='column' id='leftCol'>
<WebPartPages:WebPartZone runat="server" FrameType="TitleBarOnly" ID="Left" Title="iGoogle Left" />
</div>
<div class='column' id='middleCol'>
<WebPartPages:WebPartZone runat="server" FrameType="TitleBarOnly" ID="Center" Title="iGoogle Center" />
</div>
<div class='column' id='rightCol'>
<WebPartPages:WebPartZone runat="server" FrameType="TitleBarOnly" ID="Right" Title="iGoogle Right" />
</div>
</td>
</tr>
<tr>
<td colspan="7">
<div id='toolbox-wrapper'>
<div id='toolbox-controls'>
<div class='badge'></div>
<p class="slide"><a href="#" class="btn-slide">Toolbox</a></p>
<p class="reset"><a href="#" class="btn-reset">Reset Widgets</a></p>
</div>
<div class='column' id='toolbox'>
</div>
<div style='clear:both;'></div>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<link rel="stylesheet" type="text/css" media="all" href="/_layouts/LifeInSharePoint.iGoogle/css/style.css" />
<!-- Load the jQuery Libraries -->
<script src="/_layouts/LifeInSharePoint.iGoogle/js/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="/_layouts/LifeInSharePoint.iGoogle/js/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
<script type="text/javascript" src="/_layouts/LifeInSharePoint.iGoogle/js/iColorPicker.js"></script>
<script type="text/javascript" src="/_layouts/LifeInSharePoint.iGoogle/js/jquery.cookie.js"></script>
<script type="text/javascript" src="/_layouts/LifeInSharePoint.iGoogle/js/jquery.corner.js"></script>
<!-- script file to add your own JavaScript -->
<script type="text/javascript" src="/_layouts/LifeInSharePoint.iGoogle/js/script.js"></script>
</asp:Content>

As you can see I have made some small changes by placing our three columns within a table to keep things nice and neat. The script references have also been updated to point to our deployed assets. The elements.xml file is very simple. It takes the default.aspx page and deploys it to the root of the current site creating an iGoogle.aspx page at that location.



<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
<Module Name="Root" Url="" Path=""> 
   <!-- iGoogle PAGE -->      
<File Name="iGoogle.aspx" Url="Root.Content.Pages/default.aspx" NavBarHome="True">      
<Property Name="Title" Value="SharePoint iGoogle Interface" />      
<Property Name="ContentType" Value="Document" /> 
      </File>
</Module>
</Elements>


As you can see there is not a lot to it. We are setting the name of the deployed file to iGoogle.aspx and the Url in this case is the relative url within the project, NOT the location it will appear on the site, a common mistake I have made many times. If you wanted to place the page in another location you can modify URL and Path attributes in the <module> tag to point to another location. Since we want to place the page on the root, these are left blank.

Adding WebParts

The final addition to this elements.xml file is to add some default WebParts on to the page. For this demo we are going to use some Content Editor WebParts which will have some dummy Lorem Ipsum text within. (You can replace the xml with some other WebParts if you like, as long as you know the XML) The XML element you need to add WebParts on to the page is the <AllUsersWebPart> Node. This node has attributes which we use to define the order on the page, as well as the WebPart Zone the WebPart is to appear in. The Snippet below shows a single item.


<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
<Module Name="Root" Url="" Path=""> 
   <!-- iGoogle PAGE -->      
<File Name="iGoogle.aspx" Url="Root.Content.Pages/default.aspx" NavBarHome="True">      
<Property Name="Title" Value="SharePoint iGoogle Interface" />      
<Property Name="ContentType" Value="Document" />
        <AllUsersWebPart WebPartOrder="1" WebPartZoneID="Left">          
<![CDATA[          
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2"   
       xmlns:cewp="http://schemas.microsoft.com/WebPart/v2/ContentEditor"> 
         <Assembly>Microsoft.SharePoint,
 Version=14.0.0.0, 
Culture=neutral, PublicKeyToken=71e9bce111e9429c
</Assembly>          
<TypeName>
Microsoft.SharePoint.WebPartPages.ContentEditorWebPart
</TypeName>          
<Title>
Left Content Editor
</Title>          <FrameType>None</FrameType>          <cewp:Content>              
Duis sit amet turpis risus. Proin turpis orci, tristique porttitor venenatis eget, lacinia vitae dolor. Maecenas ultrices elementum est, ut dictum velit interdum vel. Maecenas vel urna eu quam rutrum aliquam eget quis dui. Nunc fermentum neque quis dolor facilisis hendrerit. Curabitur semper fermentum tortor, ut consequat turpis facilisis vel. Aliquam erat volutpat. Nam viverra molestie ipsum, vel tempus magna suscipit id.
                  </cewp:Content>
          </WebPart>       
 ]]>        
</AllUsersWebPart> 
     </File>
</Module>
</Elements>


You can also see from the code above that we are surrounding the WebPart XML with a <![CDATA[]]> tag which means that the text within will be ignored by the XML Parser.

Creating the Feature

Now that we have nearly all the pieces of the puzzle, the next step is to create a feature in our solution which will deploy the items to SharePoint. You should notice in your project there is a Web scoped default feature called Feature1. We need to rename our feature to something more meaningful, so in the Solution Explorer right click and rename the feature. My preference for naming Features is as follows:
SCOPE.ProjectName.FeatureName
The reason for this is that there is no quick and easy way to know the scope of a feature from glancing at the solution explorer as all icons are the same. Therefore in our solution the feature will be:
SITE.LifeInSharePoint.iGoogle.Assets
The next step is to double click on this feature and it should open the feature management screen on the left side of the window. Within this window you are able to change the Display Title and Description as well as manage the items in the feature. We will call our feature LifeInSharePoint.iGoogle, the description can be what ever you please and the Scope should be set to SITE. Finally add the Root.Content.Pages SPI into the feature and we are nearly complete.

Writing the Feature Receiver

For those who remember the last post, the control adapter requires an entry into the compact.browser file. This entry registers our control adapter for use and it would be very useful if this was added automatically as part of our deployment. To do this we will need to create a small feature receiver to do this for us. To add a receiver, right click on the feature and click the Add Event Receiver link.
We are only going to manage the addition of the code to the compact.browser and not the retraction from the solution. This can be added to your solution if you wish but to save time I will ignore it.
Our first step is to create two string constants which will contain the Control Adapter Type and the Assembly Name of the Solution. The Assembly name will only contain the first part as the full assembly name will be retrieved later through reflection.



private const string WEBPART_ADAPTER_CONTROL_TYPE = "System.Web.UI.WebControls.WebParts.WebPartZone";
private const string WEBPART_ADAPTER_TYPE = "LifeInSharePoint.iGoogle.Common.ControlAdapters.WebPartRenderControlAdapter,";

The next step is to uncomment the FeatureActivated method and add the following code in.



public override void FeatureActivated(SPFeatureReceiverProperties properties){    
// Load up the Compat.browser and add the the control adaptor    
// Get the site collection    
SPSite SiteCollection = (SPSite)properties.Feature.Parent; 
   UpdateCompatBrowser(SiteCollection);
}

This code simply gets the current Site Collection from the features property collection and then passes that SPSite object to the UpdateCompactBrowser method which is explained below in the code comments for each line.


/// <summary>
/// Add our control adaptor to the Compat.Browser file
/// </summary>
/// <param name="SiteCollection"></param>
private void UpdateCompatBrowser(SPSite SiteCollection){    
//Loop through each IISSetting in the current site collections Web App
    foreach (var IISSetting in SiteCollection.WebApplication.IisSettings)    
{        
//Create a path to the compact.browser        
String pathToCompatBrowser = IISSetting.Value.Path + @"\App_Browsers\compat.browser"; 
        //Load the Xml element
        XElement compatBrowser = XElement.Load
(pathToCompatBrowser);         
//Select the correct element  
      XElement existingElement = compatBrowser.XPathSelectElement(String.Format(                        "./browser[@refID = \"default\"]/controlAdapters/adapter[@controlType=\"{0}\" and @adapterType=\"{1}\"]",                        WEBPART_ADAPTER_CONTROL_TYPE,                        WEBPART_ADAPTER_TYPE + Assembly.GetExecutingAssembly().FullName));         
//If it cannot find the element then it will add it        
if (existingElement == null)        {            
// Get the node for the default browser.            

XElement controlAdapters = compatBrowser.XPathSelectElement("./browser[@refID = \"default\"]/controlAdapters");             
// Create and add the markup
            XElement newAdapter = new XElement("adapter");  
          newAdapter.SetAttributeValue("controlType", WEBPART_ADAPTER_CONTROL_TYPE); 
           newAdapter.SetAttributeValue("adapterType", WEBPART_ADAPTER_TYPE + Assembly.GetExecutingAssembly().FullName);      
       controlAdapters.Add(newAdapter);             
// Overwrite the old version of compat.browser with the new version           
 compatBrowser.Save(pathToCompatBrowser);        
}    
}
}


If we save all the items in the project, we are now ready to deploy our project to our site. When the feature activates it will run the code above which will make the necessary changes to the compact.browser file and our solution should work as expected.

Deployment & Testing

To deploy the solution we need to build the solution by right clicking on the project and clicking Build. After the project has been built and no errors are found, we can then deploy by again right clicking on the project and clicking Deploy. The default deployment configuration in Visual Studio will automatically activate the feature on the destination site. After deployment, navigate to the site and view the site collection features. We should see our feature deployed and activated.
If we now navigate to the root of the site collection and change the url to http://[SITE URL]/igoogle.aspx, then you should see our newly deployed interface with 5 different CEWP with some Lorem Ipsum text.
You should now be able to drag and drop these WebParts around the page, close, and change the colour. When you have finished and navigate away, refresh the page and the WebParts will remember their states. If you edit the page you will see how the Control Adapter does not render in edit mode enabling you to add new WebParts. You can see below that I have added a new Image WebPart to show how easy it is to create new “Widgets”.
NOTE: It is important to understand that this interface is designed for “Rollup” style WebParts. Due to how SharePoint 2010 and the Ribbon works with WebParts you may find some OOTB WebParts do not function fully. (Calendar WebPart, ListViewWebPart) The reason for some WebParts not working is that we are replacing the Chrome around the WebParts with our custom HTML (ControlAdapter). Many of the required ID’s etc are removed and therefore the Javascript that works with the Ribbon & Ajax fails. I am working on this and will post an update when I find a solution.

Summary

In this post we have outlined how to get the iGoogle interface into a SharePoint environment. Using a Visual Studio 2010 Project we have deployed css, images and javascript, created and deployed a Control Adapter, and added a page full of WebParts on to a site. I hope this post gives you a stepping stone on how to implement something similar on your SharePoint Deployments. Below I have uploaded a link to my Solution ZIP file that you can use and test on your environments. I have not done lots of cross browser or different environment testing of the solution so should you find an issue let me know and I will try my best to find a solution. In the next post I will show you how you can use the techniques shown in this series to come up with some innovative designs and implementations.

Download

LifeInSharePoint.iGoogle.zip



Series Content

  1. Creating iGoogle UI for SharePoint 2010/SharePoint 2013 - Part I : Overview, Concept, HTML Structure & Jquery Basics

  2. Creating iGoogle UI for SharePoint 2010/SharePoint 2013 - Part II : Dragging, Dropping, Sorting and Collapsing

  3. Creating iGoogle UI for SharePoint 2010/SharePoint 2013 - Part III : Saving WebPart states using Cookies

  4. Creating iGoogle UI for SharePoint 2010/SharePoint 2013 - Part IV : Control Adapters

  5. Creating iGoogle UI for SharePoint 2010/SharePoint 2013 - Part V : SharePoint 2010 Integration