Author Archives: Jeffrey Paarhuis

How to create an App Part in SharePoint 2013

March 22nd, 2013 | Posted by Jeffrey Paarhuis in Developing - (0 Comments)

The App Part in SharePoint 2013 is actually a Web Part version of an App. Like Web Parts in 2010 they reside in a small frame on the page. For the old Web Part this was some rendered ASPX, for an App Part this is an HTML-page in an IFrame.

In this post I want to explain, very shortly how to create an SharePoint-hosted App Part.

Build the App Part

I assume you have a developer site collection. If you haven’t yet, you can create one by selecting the Developer Site template under the Collaboration tab when creating a new site collection. This site collection has some build-in functionality that helps you deploy Apps from Visual Studio.

Ok, start off by creating a new SharePoint App in Visual Studio.  The App will be the container for the App Part.

Create an App with Visual Studio

In this scenario we are building a SharePoint-hosted App (Part) which means we can only use client-side code. If you want to know more about the different hosting options, please read this overview on MSDN.

Select App hosting options

Visual Studio creates an App with an hello-worldish script in the App.js. This script retrieves the display name of the current user and puts it in the <p id=”message”> on the default App page Default.aspx.


'use strict';

var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();

// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
$(document).ready(function () {
 getUserName();
});

// This function prepares, loads, and then executes a SharePoint query to get the current users information
function getUserName() {
 context.load(user);
 context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
}

// This function is executed if the above call is successful
// It replaces the contents of the 'message' element with the user name
function onGetUserNameSuccess()
 $('#message').text('Hello ' + user.get_title());
}

// This function is executed if the above call fails
function onGetUserNameFail(sender, args) {
 alert('Failed to get user name. Error:' + args.get_message());
}

You can run this app now and see that Visual Studio deploys the app to your developer site and starts debugging it.

Plain App

Did you know that you can debug your JavaScript straight in Visual Studio without the need of browser developer tools

We now add a Client Web Part item to the project. Despite of being called a Client Web Part, this will be the App Part.

Add Client Web Part

We want the wizard to create a new page for the App Part.

Create App Part page

This creates a Client Web Part and a page.

App Part Created

We can change the properties of the App Part, like the title, by modifying the Elements.xml.

When you open the App Part ASPX page you will notice that it looks different compared to the Default.aspx. Where the Default.aspx is using the SharePoint master page, the App Part Page is an empty page with no master page whatsoever. This is because the App Part Page will be shown in an IFrame, so we need an empty page. The only code Visual Studio generates out of the box are some JavaScript references and a script that will reference the right CSS.


<script type="text/javascript" src="../Scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/_layouts/15/MicrosoftAjax.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>

<script type="text/javascript">
 'use strict';

 // Set the style of the client web part page to be consistent with the host web.
 (function () {
  var hostUrl = '';
  if (document.URL.indexOf('?') != -1) {
   var params = document.URL.split('?')[1].split('&');
   for (var i = 0; i < params.length; i++) {
    var p = decodeURIComponent(params[i]);
    if (/^SPHostUrl=/i.test(p)) {
     hostUrl = p.split('=')[1];
     document.write('<link rel="stylesheet" href="' + hostUrl + '/_layouts/15/defaultcss.ashx" />');
     break;
    }
   }
  }
  if (hostUrl == '') {
   document.write('<link rel="stylesheet" href="/_layouts/15/1033/styles/themable/corev15.css" />');
  }
 })();
</script>

Now let’s create the same hello-worldish script on this page. Therefor we need the following script reference in the header:

<script type="text/javascript" src="../Scripts/App.js"></script>

And the following markup in the body:


<div>
 <p id="message">
  <!-- The following content will be replaced with the user name when you run the app - see App.js -->
  initializing...
 </p>
</div>

Run the App again, go to the developer site and add the App Part to the page.

Add App Part to page

Finally it would look something like this.

App Part on page

Happy coding!

Send test email from SharePoint

February 12th, 2013 | Posted by Jeffrey Paarhuis in Uncategorized - (0 Comments)

With PowerShell you can send a test email from within SharePoint.

Run the following code with SharePoint Management Shell

$email = "test@test.com"
$subject = "Test subject"
$body = "Test body"

$site = New-Object Microsoft.SharePoint.SPSite "http://sharepoint"
$web = $site.OpenWeb()
[Microsoft.SharePoint.Utilities.SPUtility]::SendEmail($web,0,0,$email,$subject,$body)

// A True or False will confirm the message has been sent or not

Enabling FBA and NTLM Claims-based authentication with PowerShell

January 16th, 2013 | Posted by Jeffrey Paarhuis in Uncategorized - (0 Comments)

On the interwebz I couldn’t find any way to set multiple Authentication Providers on a Web Application and I need to set the Authentication Providers to NTLM ánd FBA. The examples always show one Authentication Provider. Turns out the solution is very simple:

$NTLM = New-SPAuthenticationProvider -UseWindowsIntegratedAuthentication -DisableKerberos
$FBA = New-SPAuthenticationProvider -ASPNETMembershipProvider "MyMembershipProvider" -ASPNETRoleProviderName "MyRoleProvider"
$webApplication.UseClaimsAuthentication = $true
$webApplication.Update()
$webApplication.MigrateUsers($true)

Set-SPWebApplication -AuthenticationProvider $NTLM,$FBA -Identity $webApplication -Zone "Default"

Audiences in SharePoint Online

November 15th, 2012 | Posted by Jeffrey Paarhuis in Uncategorized - (0 Comments)

Audiences in SharePoint Online are as good as useless, because the audiences are compiled once a week. Unless you exactly know what you’re doing you can give it a try, but waiting a week to get your audiences compiled and afterwards finding out that you forgot one property is not very satisfying.

According to this forum, the compiling will be once a day in the future. So, lets wait for that.

http://community.office365.com/en-us/forums/154/p/966/2953.aspx#2953

 

Add custom filter to the SharePoint Refinement Panel

October 23rd, 2012 | Posted by Jeffrey Paarhuis in Configuring | Developing | Good to know - (0 Comments)

Just for my reference, or anyone else interested, a link to the perfect guide on creating a custom filter section in the refinement panel.

http://www.domorewithsearch.com/customizing-the-refinement-panel-for-sharepoint-2010-search/

Fix for List Event Receiver not working in SharePoint Online

August 20th, 2012 | Posted by Jeffrey Paarhuis in Developing - (0 Comments)

I created a List Event Receiver for my custom List Definition and wanted to set a view list properties programmatically in the EventReceiver. It worked perfectly in SharePoint 2010 but not in SharePoint Online.

To get the list I used the SPListEventProperties.List, but this object was causing some issues in SharePoint Online. I solved it by using the SPListEventProperties.Web.Lists[listname].

Code before:


SPList list = properties.List;

//enable content types
list.ContentTypesEnabled = true;

list.Update();

Code after:


SPList list = properties.List;
 SPWeb web = properties.Web;

// Re-opening the list, seems to be necessary because SharePoint Online has some sort of bug or timing issue.
 list = web.Lists[list.Title];

// enable content types
 list.ContentTypesEnabled = true;

list.Update();

“Cannot import Web Part” fix for Social Web Parts

August 14th, 2012 | Posted by Jeffrey Paarhuis in Uncategorized - (2 Comments)

I’m creating a sandboxed web template for Office 365. I’m adding several web parts to a page and it’s working fine until I add Social web parts, like a Note Board or Tag Cloud. When I add one of these to a page I receive the famous “Cannot import Web Part” error upon deploying a site with this template.

The problem is that SharePoint exports the web part definition as a .webpart (v3) file. As it seems, Social Collaboration Web Parts don’t work well together with a v3 web part definition. When you change it to an old v2 (.dwp) definition it works perfectly.

So, this doesn’t work:


<webParts>
 <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
 <metaData>
 SocialCommentWebPart, Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
 <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
 </metaData>
 <data>
 <properties>
 <property name="Height" type="string" />
 <property name="AllowZoneChange" type="bool">True</property>
 <property name="AllowConnect" type="bool">True</property>
 <property name="ExportMode" type="exportmode">All</property>
 <property name="HelpUrl" type="string" />
 <property name="Hidden" type="bool">False</property>
 <property name="TitleUrl" type="string" />
 <property name="WebPartPropertyDisplayItems" type="int">20</property>
 Enable users to leave short, publicly-viewable notes about this page.
 <property name="AllowHide" type="bool">True</property>
 <property name="ChromeState" type="chromestate">Normal</property>
 <property name="AllowMinimize" type="bool">True</property>
 <property name="Title" type="string">Posts</property>
 <property name="ChromeType" type="chrometype">TitleOnly</property>
 <property name="MissingAssembly" type="string">Cannot import this Web Part.</property>
 <property name="Width" type="string" />
 <property name="WebPartPropertySpecifiedAddress" type="string" null="true" />
 <property name="HelpMode" type="helpmode">Modeless</property>
 <property name="CatalogIconImageUrl" type="string" />
 <property name="AllowEdit" type="bool">True</property>
 <property name="TitleIconImageUrl" type="string" />
 <property name="Direction" type="direction">NotSet</property>
 <property name="AllowClose" type="bool">True</property>
 <property name="WebPartPropertyAllowNewComment" type="bool">True</property>
 </properties>
 </data>
 </webPart>
</webParts>

And this works:


xmlns="http://schemas.microsoft.com/WebPart/v2">
 <Title>Posts</Title>
 <FrameType>TitleBarOnly</FrameType>
 <AllowRemove>true</AllowRemove>
 <AllowMinimize>true</AllowMinimize>
 <Description>Displays a list of your colleagues and their recent changes.</Description>
 <Assembly>Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
 <TypeName>Microsoft.SharePoint.Portal.WebControls.SocialCommentWebPart</TypeName>
</WebPart>

Note the change from “http://schemas.microsoft.com/WebPart/v3” to “http://schemas.microsoft.com/WebPart/v2“. Also note the absence of the <webParts> tag, which isn’t allowed in the v2 web part definition.

In this sample I’ve taken the Note Board Web Part (SocialCommentWebPart), but you can use whatever web part you want by changing the TypeName.

Windows Server 2012 very slow on VirtualBox

July 27th, 2012 | Posted by Jeffrey Paarhuis in Uncategorized - (3 Comments)

I’ve installed Windows Server 2012 Release Candidate on my VirtualBox today. Everything worked fine till I installed the Guest Additions, it suddenly became slow and unresponsive from time to time, especially when using Internet Explorer. Because it happened after installing the Guest Additions, I thought it had something to do with the 3D acceleration, so I disabled that and now it works fine.

So, just disable 3D acceleration on the video properties and you’re good to go!

Installing SharePoint 2013 Preview on Windows Server 2008 r2

July 17th, 2012 | Posted by Jeffrey Paarhuis in Uncategorized - (3 Comments)

I found an article on the internet that could tell me how I can install SharePoint 2013 on a Windows Server 2012 machine. I however, just want to install the new SharePoint 2013 Preview on an a familiar machine, without all the Windows Server 2012 Preview nonsense.

I cloned an existing SharePoint 2010 virtual machine, with AD and SQL Server 2008 r2 already configured, uninstalled SharePoint Server 2010 and SharePoint Designer 2010 and started from there. This will also work on a newly configured machine, keeping in mind that we are using AD and SQL Server 2008 r2.

On the first attempt to install SharePoint 2013 (download link), the  autorun wasn’t working, so I opened the folder and ran setup.exe. Then I got a message telling me I need a few components:

  • This product requires Microsoft Identity Extensions.
  • This product requires  Windows Server Appfabric. Install Windows Server Appfabric and re-run setup.
  • This product requires Cumulative Update Package 1 for Microsoft AppFabric 1.1 for Windows Server (KB2671763)
  • This product requires Microsoft SQL Server 2008 R2 SP1 Native Client.
  • This product requires the Microsoft WCF Data Services 5.0.
  • This product requires Microsoft Information Protection and Control Client (MSIPC)

So, obviously I forgot to launch the pre-requisites installer first, prerequisiteinstaller.exe.

Now I could install SharePoint 2013 successfully and launch the Configuration Wizard afterwards. But upon configuring the database my system tells me that my SQL server has an unsupported version. I need to apply SP1 of SQL Server 2008 r2: http://www.microsoft.com/en-us/download/details.aspx?id=26727

After completing the configuration wizard, it dropped me into the Central Administration. I tried to run the wizard that generates a complete environment, but it failed and resulted in a corrupt web application. So my advice is, just like SharePoint 2010: skip the wizard and do it manually. I created a new web application and site collection and it works like a charm.

SharePoint Server 2013 Preview is released, download it now!

July 16th, 2012 | Posted by Jeffrey Paarhuis in Uncategorized - (0 Comments)

Microsoft has released SharePoint Server 2013 Preview this afternoon, or evening at my time. Download it here: http://technet.microsoft.com/en-US/evalcenter/hh973397.aspx?wt.mc_id=TEC_121_1_33

It’s also possible to get yourself an Office 365 setup with the new Office Suite, including SharePoint Online (yes, SharePoint 2013 Preview). With a few clicks you can walk into your own SharePoint 2013 setup: http://www.microsoft.com/office/preview/en/office-365-enterprise

%d bloggers like this: