Comparing Sharepoint Hosted Apps And Client Web Parts For Custom Solutions

What are SharePoint Hosted Apps and Client Web Parts?

SharePoint hosted apps and client web parts are two different methods for building custom solutions on the SharePoint platform. At a high level, SharePoint hosted apps are self-contained applications that run externally to the SharePoint environment, while still having access to SharePoint content and services. In contrast, client web parts are components that run directly within the SharePoint environment as part of a SharePoint site.

Some key characteristics of SharePoint hosted apps include:

  • Self-contained and hosted externally to SharePoint
  • Access SharePoint content/services via web services
  • Can be built using a variety of programming languages and frameworks
  • Offer increased development flexibility compared to web parts

Whereas client web parts have the following key attributes:

  • Run directly within the SharePoint environment
  • Built using SharePoint tools like Visual Studio and .NET
  • Tight integration with SharePoint sites and content
  • Can leverage full SharePoint API in code

When to Use SharePoint Hosted Apps vs Client Web Parts

There are several important factors to evaluate when deciding between using a SharePoint hosted app versus a client web part for a particular business need:

  • Required integration – Web parts offer tighter integration with SP sites.
  • Development skills – Apps open up more dev language options.
  • Security – Apps offer isolated security model.
  • Extensibility – Web parts directly access SP API.

Specific scenarios better suited for SharePoint hosted apps include:

  • Mobile or responsive web applications
  • Application requiring external data sources
  • Flexibility for diverse development skills
  • Application needing isolated security model

Scenarios better addressed by client web parts include:

  • Custom components on SharePoint sites
  • Application logic using SharePoint data
  • Simplified permissions and governance
  • Pixel perfect customizations and branding

Developing with SharePoint Hosted Apps

The key steps for developing a SharePoint hosted app are:

  1. Create app manifest and declarative files
  2. Develop app components and front end code
  3. Connect to SharePoint via client APIs and REST/OData
  4. Deploy the app files to appropriate location
  5. Add the app to SharePoint site using app catalog

SharePoint hosted apps have a clean separation between UI front end code and back end server code. Some example code showing calls to SharePoint REST API from JavaScript client-side code:

// Get SharePoint site documents library
$.getJSON(“https://site_url/_api/web/lists/getbytitle(‘Documents’)”, 
  function(data){
     // Code to process returned SP list JSON  
  } 
);

// Update SharePoint list item
$.ajax({
   url: "https://site_url/_api/lists/getbytitle('Tasks')/items(1)",
   type: "POST",
   data: { “Title” : “New Task” } 
}); 

Developing with Client Web Parts

Building a SharePoint client web part involves:

  1. Creating a project in Visual Studio
  2. Adding a web part item template
  3. Developing server-side .NET code
  4. Designing an ASP.NET front end
  5. Packaging and deploying the web part

Web parts directly leverage SharePoint .NET client object models and have access to the full client API from the server-side code. Example C# code for common web part tasks:

// Load SharePoint list items
var siteUrl = SPContext.Current.Site.Url;
var list = site.OpenWeb().GetList("/docs");  
var items = list.GetItems();

// Update list item properties
var item = list.GetItemById(1);
item["Title"] = "Updated document"; 
item.Update();

Considerations for Customization and Extensibility

SharePoint hosted apps provide more limited customization capabilities compared to web parts. Branding and styling options are restricted since apps run externally. However, apps offer better extensibility for diverse functionality and other systems.

Conversely, web parts permit seamless pixel-perfect customizations through SharePoint composites, CSS, and other frontend techniques. But the .NET and SharePoint server-side dependence makes web parts more constrained for extensible integrations.

Summary and Recommendations

In summary, the choice between using a SharePoint app versus client web part depends largely on the specific functional goals and business needs:

  • Apps for external data/systems, custom security, open dev skills
  • Web parts for UI customization, SP integration

As a general guideline, consider SharePoint hosted apps for application-style solutions needing external capabilities or diverse skillsets. Prefer custom web parts when requirements call for tight UI customization or deep integration with SharePoint content and data services.

Leave a Reply

Your email address will not be published. Required fields are marked *