Thursday, November 5, 2015

SharePoint 2013 – Apps Vs Farm solutions,Difference between Apps and Farm solutions in SharePoint 2013,Comparison between Apps and Farm solutions in sharepoint 2013
SharePoint 2013 has introduces the new Cloud App Model that enables you to create apps.
In SharePoint 2010 you had an Option to Add custom components to SharePoint via SharePoint Sandboxed or Farm solutions.But in SharePoint 2013 sandboxed solutions are deprecated.So all we got is the New App model and the Old SharePoint Farm solutions.
Advertisement
SharePoint 2013 – Apps Vs Farm solutions
To begin developing on SharePoint 2013 you need to first know what would you choose an App or farm solution? Here is a little Comparison chart –
AppsFarm Solutions
What are apps –
An app for SharePoint is a small, easy-to-use, stand-alone app that solves a specific end-user or business need.
What are Farm solutions – Farm solutions are pieces of functionality that extend the capabilities of a SharePoint website. They are Installed to the solution store of a farm by a farm administrator.
where does it run – The code for an app runs in different places, depending on where your app is hosted.They never run in the context of SharePoint Server, but they will run in the context of the browser or in the context of the hosted platform.
  • SharePoint-hosted apps
  • Provider-hosted and autohosted apps – In the cloud
  • Apps that have a mix of components in SharePoint and in the cloud
where does it run – They are Installed to the solution store of a farm by a farm administrator. They run from the sharepoint server.
How Does It run – When you deploy a SharePoint-hosted app, SharePoint creates a new website called the app web. You can think of it as a dynamically created safe space for your app. Besides allowing you to store pages, lists, and libraries, the app web is also an isolated endpoint that your app can securely call client side by using JavaScript.How Does It run –
when you deploy a Farm solution, It gets deployed to one or more web apps (any existing or new one). You can use it in any site collection of the web app where you deployed it.
Authentication options – Before you can call SharePoint APIs from your app, you need to authenticate to SharePoint. Which authentication mechanism you use depends upon where the code of your app is running.
* Inside SharePoint: You have to use HTML and JavaScript, and authentication is already taken care for you.
* In the cloud: You have two choices:
Use client-side code along with the cross-domain library.
User server-side code along with OAuth.
*REST APIs
Authentication options – The components in the solution can, and usually do, run in full trust
Resource Allocation –
Site collection administrators and tenant administrators can monitor apps and change the resources allocated to them.
Resource Allocation – No resource usage restrictions are placed on them
What Can be Created as Apps –
  • Custom Web Parts (remote pages that contain custom Web Parts)
  • Event receivers and Feature receivers(remote event receivers)
  • Custom field (column) types ()
  • Custom web services built on the SharePoint Service Application Framework
  • Application pages
    Cannot be created as Apps –
  • Apps cannot call SharePoint server side code
  • Apps cannot access SharePoint components that are not on the same site
  • Apps cannot communicate with each other
  • Custom site definitions
  • Custom themes
  • Custom action groups and custom action hiding
  • User controls (.ascx files)
  • Delegate controls
What Can be Created as Farm soltuion – You can deploy almost all the components as Farm solution.

Sharepoint Apps Vs Farm Solutions

Sharepoint Apps
SharePoint 2013 has introduces the new Cloud App Model that enables you to create apps.
In SharePoint 2010 you had an Option to Add custom components to SharePoint via SharePoint Sandboxed or Farm solutions.
But in SharePoint 2013 sandboxed solutions are deprecated.So all we got is the New App model and the Old SharePoint Farm solutions.

SharePoint 2013 – Apps Vs Farm solutions

AppsFarm Solutions
What are apps –
An app for SharePoint is a small, easy-to-use, stand-alone app that solves a specific end-user or business need.
What are Farm solutions – Farm solutions are pieces of functionality that extend the capabilities of a SharePoint website. They are Installed to the solution store of a farm by a farm administrator.
where does it run – The code for an app runs in different places, depending on where your app is hosted.They never run in the context of SharePoint Server, but they will run in the context of the browser or in the context of the hosted platform.
  • SharePoint-hosted apps
  • Provider-hosted and autohosted apps – In the cloud
  • Apps that have a mix of components in SharePoint and in the cloud
where does it run – They are Installed to the solution store of a farm by a farm administrator. They run from the sharepoint server.
How Does It run – When you deploy a SharePoint-hosted app, SharePoint creates a new website called the app web. You can think of it as a dynamically created safe space for your app. Besides allowing you to store pages, lists, and libraries, the app web is also an isolated endpoint that your app can securely call client side by using JavaScript.How Does It run –
when you deploy a Farm solution, It gets deployed to one or more web apps (any existing or new one). You can use it in any site collection of the web app where you deployed it.
Authentication options – Before you can call SharePoint APIs from your app, you need to authenticate to SharePoint. Which authentication mechanism you use depends upon where the code of your app is running.
* Inside SharePoint: You have to use HTML and JavaScript, and authentication is already taken care for you.
* In the cloud: You have two choices:
Use client-side code along with the cross-domain library.
User server-side code along with OAuth.
*REST APIs
Authentication options – The components in the solution can, and usually do, run in full trust
Resource Allocation –
Site collection administrators and tenant administrators can monitor apps and change the resources allocated to them.
Resource Allocation – No resource usage restrictions are placed on them
What Can be Created as Apps –
  • Custom Web Parts (remote pages that contain custom Web Parts)
  • Event receivers and Feature receivers(remote event receivers)
  • Custom field (column) types ()
  • Custom web services built on the SharePoint Service Application Framework
  • Application pages
    Cannot be created as Apps –
  • Apps cannot call SharePoint server side code
  • Apps cannot access SharePoint components that are not on the same site
  • Apps cannot communicate with each other
  • Custom site definitions
  • Custom themes
  • Custom action groups and custom action hiding
  • User controls (.ascx files)
  • Delegate controls
What Can be Created as Farm soltuion – You can deploy almost all the components as Farm solution.

SharePoint 2010 ECMA Scripts code snippets

ECMScript 

ECMScript you need to create a client context before you can retrieve data from a SharePoint site.

With ECMAScript you can retrieve the client context from the SharePoint site in which the script runs.
Use the Load method to specify which data to load from SharePoint.

Use the asynchronous method executeQueryAsync to retrieve the information from SharePoint. This method needs a reference to a callback method when the execution succeeds and a reference to a callback method when the execution fails.

To Set and Get the Properties there are ways to get the information.
  1. get_methodname
  2. set_Methodname
example get_web(), set_title etc




Update, Insert, Delete Code snippets


1. Load Site Data  


<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
clientContext.load(web, 'Title');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onSiteLoadSuccess), Function.createDelegate(this, this.onQueryFailed));
}
function onSiteLoadSuccess(sender, args) {
alert("site title : " + web.get_title());
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>​

2.Load List Items Data 

<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
var list = web.get_lists().getByTitle("Pages");
var camlQuery = new SP.CamlQuery();
var q = '<View><RowLimit>5</RowLimit></View>';
camlQuery.set_viewXml(q);
this.listItems = list.getItems(camlQuery);
clientContext.load(listItems, 'Include(DisplayName,Id)');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess),
Function.createDelegate(this, this.onQueryFailed));
}
function onListItemsLoadSuccess(sender, args) {
var listEnumerator = this.listItems.getEnumerator();
//iterate though all of the items
while (listEnumerator.moveNext()) {
    var item = listEnumerator.get_current();               
    var title = item.get_displayName();
    var id = item.get_id();
            alert("List title : " + title + "; List ID : "+ id);
        }
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>​

3. Load List Data


<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
this.list = web.get_lists().getByTitle("Images");
clientContext.load(list, 'Title', 'Id');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onListLoadSuccess),
Function.createDelegate(this, this.onQueryFailed));
}
function onListLoadSuccess(sender, args) {
alert("List title : " + this.list.get_title() + "; List ID : "+ this.list.get_id());
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>​

4.  Create List


<script type="text/javascript">

var clientContext = null;
var web = null;

ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");

function Initialize()
{

clientContext = new SP.ClientContext.get_current();

web = clientContext.get_web();

var itemCreateInfo = new SP.ListItemCreationInformation();

var listCreationInfo = new SP.ListCreationInformation();

listCreationInfo.set_title('My Custom Generic List');

listCreationInfo.set_templateType(SP.ListTemplateType.genericList);

this.oList = web.get_lists().add(listCreationInfo);

clientContext.load(oList, 'Title', 'Id');

clientContext.executeQueryAsync(Function.createDelegate(this, this.onListCreateSuccess),

Function.createDelegate(this, this.onQueryFailed));

}

function onListCreateSuccess(sender, args) {
alert("List title : " + this.oList.get_title() + "; List ID : "+ this.oList.get_id());
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>​

5. Create Site


<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
var webCreateInfo = new SP.WebCreationInformation();
    webCreateInfo.set_description("All about Rare solutions.");
    webCreateInfo.set_language(1033);
    webCreateInfo.set_title("Rare Solutions - SharePoint and .NET");
    webCreateInfo.set_url("RareSolutionsSharePoint");
    webCreateInfo.set_useSamePermissionsAsParentSite(true);
    webCreateInfo.set_webTemplate("BLOG#0");

    this.oNewWebsite = this.web.get_webs().add(webCreateInfo);

    clientContext.load(this.oNewWebsite, 'ServerRelativeUrl', 'Created');

clientContext.executeQueryAsync(Function.createDelegate(this, this.onCreateWebSuccess),

Function.createDelegate(this, this.onQueryFailed));
}
function onCreateWebSuccess(sender, args) {
alert("Web site url : " + this.oNewWebsite.get_serverRelativeUrl());
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>​

6. Update List



<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
this.list = web.get_lists().getByTitle('My custom generic list');
list.set_description('My custom generic list description');
list.update();
clientContext.load(list, 'Description');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onSiteLoadSuccess), Function.createDelegate(this, this.onQueryFailed));
}
function onSiteLoadSuccess(sender, args) {
alert("list description : " + this.list.get_description());
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>​

7.  Update List Item


<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
this.list = web.get_lists().getByTitle('My custom generic list');
this.oListItem = list.getItemById(1);
oListItem.set_item('Title', 'Praveen Battula Updated');
oListItem.update();

clientContext.executeQueryAsync(Function.createDelegate(this, this.onUpdateListItemSuccess), Function.createDelegate(this, this.onQueryFailed));
}
function onUpdateListItemSuccess(sender, args) {
alert("list item updated");
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>


8. Delete List



<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
this.list = web.get_lists().getByTitle('My custom generic list');
list.deleteObject();

clientContext.executeQueryAsync(Function.createDelegate(this, this.onListDeleteSuccess),

Function.createDelegate(this, this.onQueryFailed));
}
function onListDeleteSuccess(sender, args) {
alert("list deleted");
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>​

9. Delete Site



<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
this.website = web.get_webs().getByTitle('Rare Solutions');
website.deleteObject();

clientContext.executeQueryAsync(Function.createDelegate(this, this.onSiteDeleteSuccess),

Function.createDelegate(this, this.onQueryFailed));
}
function onSiteDeleteSuccess(sender, args) {
alert("site deleted");
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>​

10. Delete List Item



<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
this.list = web.get_lists().getByTitle('My custom generic list');
this.oListItem = list.getItemById(1);
oListItem.deleteObject();

clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemDeleteSuccess),

Function.createDelegate(this, this.onQueryFailed));
}
function onListItemDeleteSuccess(sender, args) {
alert("list item deleted");
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>​