Skip to content

ShareCoffee.Commons

Thorsten Hans edited this page Oct 28, 2013 · 2 revisions

ShareCoffee.Commons provides serveral useful helpers that can make your SharePoint-App-Developer life easier.

ShareCoffee.Commons.getAppWebUrl

There isn't a perfect way how to receive the AppWebUrl. _spPageContext info object is only available in SharePoint-Hosted samples. Therefore ShareCoffee's implementation offers a little bit more. It looks for the AppWebUrl in

  • _spPageContextInfo (if persent)
  • SPAppWebUrl parameter in the QueryString
  • it executes a custom method that can be injected to define where it should load the url from

It's evaluating the sources in the following order

  1. your injected method
  2. _spPageContextInfo
  3. QueryString

Usage

// use ShareCoffee's default implementation
// writes an error to the console if nothing found.
var appWebUrl = ShareCoffee.Commons.getAppWebUrl();

// use your custom implementation
ShareCoffee.Commons.loadAppWebUrlFrom = function(){ 
    // load from cookie or database ...
    return "https://my-app-web.sharepoint.com/";
};
var appWebUrl = ShareCoffee.Commons.getAppWebUrl();

ShareCoffee.Commons.getApiRootUrl

Returns the combination of ShareCoffee.Commons.getAppWebUrl() + "/_api/"

Usage

var apiRoot = ShareCoffee.Commons.getApiRootUrl()

ShareCoffee.Commons.getHostWebUrl

The method is going to load the HostWebUrl from the QueryString or by invoking your custom method

  • SPHostUrl parameter in the QueryString
  • it executes a custom method that can be injected to define where it should load the url from

It's evaluating the sources in the following order

  1. your injected method
  2. QueryString

Usage

// use ShareCoffee's default implementation
// writes an error to the console if nothing found.
var hostWebUrl = ShareCoffee.Commons.getHostWebUrl();

// use your custom implementation
ShareCoffee.Commons.loadHostWebUrlFrom = function(){ 
    // load from cookie or database ...
    return "https://myhostweb.sharepoint.com/";
};
var hostWebUrl = ShareCoffee.Commons.getHostWebUrl();

ShareCoffee.Commons.getFormDigest

In order to perform write operations by using SharePoint APIs, you've to pass the FormDigest-Value. The getFormDigest method is going to return the required value.

Usage

var formDigestValue = ShareCoffee.Commons.getFormDigest();

ShareCoffee.Commons.getQueryString

This method returns the entire QueryString currently appended to the URL.

Usage

var queryString = ShareCoffee.Commons.getQueryString();

ShareCoffee.Commons.getQueryStringParameter

This method reutrns a single value from the entire QueryString by its Name. if the Name isn't present in the current QueryString it will return an empty string.

Usage

var hostWebUrl = ShareCoffee.Commons.getQueryStringParameter('SPHostUrl');