Tuesday, December 24, 2013

How to create App Part in SharePoint 2013

Copyright from: jeffreypaarhuis.com
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 
 on the default App page Default.aspx.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
'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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
Now let’s create the same hello-worldish script on this page. Therefor we need the following script reference in the header:
1
And the following markup in the body:
1
2
3
4
5
6
<div>
 <p id="message">
  
  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!

App Designs in SharePoint 2013

copyright from: www.shillier.com

3/18/2013 Update: After presenting this material at a few user groups, I decided this article needs a better structure to present the ideas. So I have updated the table to reflect more nuance than just "good" and "bad". I'm interested in feedback as I am using this as a cornerstone for many presentations. Catch me on Twitter @ScotHillier.
The SharePoint 2013 app model supports three hosting models and two client APIs. This means that there are 12 permutations of app design as shown in the table below. In this post, I’ll briefly explain when you should use each app design.
Key
Key

Excellent ChoiceExcellent Choice
SharePoint-Hosted App, JavaScript, REST
SharePoint-Hosted apps must use JavaScript and can use either REST or CSOM (aka, JSOM). However, REST is by far the preferred approach. Why? Bringing the SharePoint context to the client is a uniquely-SharePoint idea that no other technology uses. REST is a standard that many technologies understand. Your code is a lot more “standard” when you use REST. Furthermore, key libraries such as jQuery already support REST.
Cloud-Hosted apps, C#, CSOM
Cloud-hosted apps (meaning both provider-hosted and autohosted) can use JavaScript/REST, C#/REST, or C#/CSOM. Again, some combinations are better than others.
When working with cloud-hosted apps, you often have to work with OAuth tokens. The TokenHelper class, which is included in Visual Studio makes this much easier. So, immediately you can see that C# is a better choice than JavaScript where tokens are important. JavaScript does have a role to play, but it is primarily when you need to use the cross-domain library to overcome firewall limitations.
Once you have concluded that C# is the way to go, you’ll discover that writing CSOM code is much easier than REST. CSOM can be used synchronously in the managed object model, which makes the code very simple and straightforward. REST calls, on the other hand, have some serious drawbacks. First, they require async round trips to the server to acquire the FormDigest from SharePoint. Second, you have to create POST messages by hand in code, which is ugly.

Cross-Domain SupportCross-Domain Support
Cloud-Hosted, JavaScript, CSOM
JavaScript/CSOM (aka JSOM) requires a SharePoint context. In classic JSOM, the context is created in code within a page that is hosted in SharePoint. When you consider that cloud-hosted apps never run pages that have such a context, it is clear to see that classic JSOM can't work in remote webs.
SharePoint 2013, however,provides a workaround utilizing the SP.ProxyWebRequestExecutorFactory that allows you to make cross-domain calls using JSOM. These cross-domain calls are in lieu of using C#, CSOM, and the OAuth plumbing, which is a better choice but may not be possible in all situations. If, for example, you have a remote web that is outside the firewall and needs to call back to an on-premise SharePoint farm, OAuth will not work. In this case, the cross-domain JSOM solves the problem.
Cloud-Hosted, JavaScript, REST
There is also a REST version of the cross-domain library, which accomplishes the same ends. Again, REST is more standard than JSOM, so you should favor this approach.

Impossible Impossible
C# cannot be used in a SharePoint-hosted app in any way because server-side code is not allowed. Period. There is no way around this fact.

Beer Debate Debate Over Beers
SharePoint-Hosted App, JavaScript, JSOM
I've already made the argument that REST is more of a standard than JSOM. However, there are legitimate advantages to using JSOM over REST. The most-important advantage is that JSOM is currently a superset of REST functionality. There are some operations that can't be performed in REST. For example, updating content types and pushing changes to all lists is simple in JSOM, but not supported in REST as far as I can tell (even though the API sort of suggests it is).
The second advanatge of JSOM is that it currently has much better documentation than the REST API. This is because it has been around since 2010. I'm sure the REST documentation will catch up, but right now, it is often frustrating to do anything beyond CRUD operations.
Finally, JSOM payloads are smaller than REST payloads. Some non-scientific Fiddler analysis has shown REST payloads to be as much as 2x JSOM payloads.
Cloud-Hosted, C#, REST
I've already gone over some of the reasons why I think this combination is not a winner. I've listed it here because it is possible so someone may want to debate its merits. However, I think this is pattern is so tedious that I would never use it.

Conclusions
A couple of clear takeaways from this analysis:
·        If you are writing a SharePoint-Hosted app, use JavaScript and the REST API, not JSOM
    unless you are involved with operations not currently supported by REST
·        If you are writing cloud-hosted apps, use C# and the managed version of CSOM, not REST

·        If you are writing a cloud-hosted app and you are trying to call back into an on-premises
    SharePoint farm through a firewall, use JavaScript, REST, and the cross-domain library.