SQL Azure Limit for SharePoint Autohosted App

To find the SQL Azure Edition and its maximum size in the SharePoint Online Autohosted app, firstly we need to get the connection string of the database used by the app by displaying it in an aspx page or more subtly returning it using Web API. Don’t forget that the connection string is defined in the AppSettings, snippet below shows how to get the value from the code.

public string GetConnectionString()
   return WebConfigurationManager.AppSettings["SqlAzureConnectionString"];

Normally you will get something like below.

Data Source=(a_server).database.windows.net;
Initial Catalog=db_(guid);
User ID=db_(guid_as_above)_dbo;

Put this connection string to Visual Studio’s SQL explorer or SQL Management Studio. The User ID won’t have access to master db so that you need to connect directly to the database.
After it gets connected run this below scripts to get the SQL Edition and limit, thanks to Azure How-to talk series’ post


And the result is Azure Web Edition and Max limit is 1 GB, and unfortunately we can’t change this using ALTER DATABASE command as shown here as we don’t have access to master db. Below is the screen shot from VS 2013′s SQL Server Explorer:


P.S. please let me know if there is a way to change the limit :)

SharePoint 2013 Execute Javascript function after MDS load

SharePoint 2013 introduces a performance improvement to increase the page load response time called Minimal Download Strategy (MDS). It is a feature that by default is activated in the team site. The MDS is performed by functions inside javascript file called start.js. This blog explains the flow of a MDS call.

So it is a bit tricky to execute a javascript function to modify the DOM after the MDS finish as a normal jQuery’s document.ready function or _spBodyOnLoadFunctionNames are executed before the MDS script finishes.

Inside the start.js file there is an object called asyncDeltaManager that perform most of the async page loading. After several times code stepping in a javascript debugger, I could see what is going on under the hood, at the end the process a function called _scriptsLoadComplete is called and inside it, a function called _endRequest is called. And in the _endRequest function there is a routine to iterate a eventHandlerList called endRequest. So there is a event handler that we can hook our function into the asyncDeltaManager pipeline. Shortly, below code shows how to perform this task.

$(function () {
      ExecuteOrDelayUntilScriptLoaded(function () {
          if (typeof asyncDeltaManager != "undefined")
          else setFullScreenMode();
      }, "start.js");


function setFullScreenMode() {
     return false;

In the code above the function setFullScreenMode will set the page in full screen mode and hide the siteaction and the full screen button. This code need to be executed after the MDS finishes. I hooked this function inside the jQuery load to the MDS script pipeline asyncDeltaManager.add_endRequest to make sure that the function executed after everything is loaded.

Apply DesignPackage using SharePoint 2013 Client Object Model

SharePoint 2013 introduced Design Manager as part of Publishing feature to provision look and feel such as master page, page layouts, theme, composed look,etc. We can also package the design files as a design package which actually is a sandbox solution file (wsp). Whenever we import a design package to SharePoint site, in the background it performs these tasks:

  • Rename the wsp (design package) file and upload it to the Solution Gallery
  • Activate the solution file and Activate the features inside the solution. The feature will provision the design files into the master page gallery
  • And finally apply the design such as applying the master page to the site.

There are also API available both server and client APIs to perform this task. The client API gives us the ability to push the Design Package to remote site such as SharePoint Online (Office 365). This below code show how to use the client API to provision design package to remote site. The fileUrl is the location of the file in the document libraryrelative to the site such as “SiteAssets/Bootstrap.wsp”. This post shows the routine to upload the file to the document library using Client Object Model.

private static void ApplyDesign(ClientContext context, string fileUrl)

  if (context.IsSiteFeatureActivated(PublishingFeature))
    DesignPackageInfo info = new DesignPackageInfo()
       PackageGuid = Guid.Empty,
       MajorVersion = 1,
       MinorVersion = 1,
       PackageName = "Bootstrap"
    Console.WriteLine("Installing design package ");

    string fileRelativePath = GetSiteRelativePath(context.Url) + fileUrl;
    DesignPackage.Install(context, context.Site, info, fileRelativePath);

    Console.WriteLine("Applying Design Package!");
    DesignPackage.Apply(context, context.Site, info);

First we check if publishing feature GUID (f6924d36-2fa8-4f0b-b16d-06b7250180fa) is activated in site collection level. This method as well as the GetSiteRelativePath are a custom methods. The DesignPackageInstall url requires to pass the wsp url relative to the root for example in a site collection: https://xx.sharepoint.com/sites/TestA/ we need to pass /sites/TestA/SiteAssets/Bootstrap.wsp to the method.

 public static bool IsSiteFeatureActivated(this ClientContext context, Guid guid)
    var features = context.Site.Features;

    foreach (var f in features)
        if (f.DefinitionId.Equals(guid))
          return true;
    return false;

  public static string GetSiteRelativePath(string url)
     string urlDoubleSlsRemoved = url.Replace("://", string.Empty);
     int index = urlDoubleSlsRemoved.IndexOf("/");
     return urlDoubleSlsRemoved.Substring(index);

Another thing that I found is the DesignPackage.Install method will install the wsp file into the solution gallery. As there is no client API to perform Sandbox solution installation we can literally use this method(DesignPackage.Install) to install ANY sandbox solution packages.

Upload a wsp file to Office 365 (SP 2013) using WebClient

In recent project I need to create a script to apply a design package to site collections in SharePoint Online. The first thing I need to do is to upload the design package (wsp file) to a document library. There is Client API to upload the file to SharePoint but there is size limitation, so I decided to use WebClient  PUT method instead. There are some catches that I found when using this approach:

  • I couldn’t just set the WebClient.Credentials to SharePoint Online Credentials as I would get 403 error. Solution: I need to send the SharePoint security token back as a FedAuth cookie, as a result I need to have a custom WebClient that has Cookie Container
  • I was getting ServerException (Unable to read cabinet info from) when run the Client API DesignPackage.Install on the uploaded wsp file. And I found that the WebClient.UploadFile method somehow didn’t maintain the file format properly. Solution: I need to use the OpenWrite method and upload it in a stream

Below is the working code. First I created a custom web client that accepts Cookie. There is other benefit of using this approach as we have more control on the web request object such as we can increase the timeout as well.

    class SPWebClient : WebClient
        public CookieContainer CookieContainer { get; set; }

        protected override WebRequest GetWebRequest(Uri address)
            HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest;
            if (request != null)
                request.CookieContainer = CookieContainer;
            return request;

Below is the upload routine. the site Url is the siteUrl in Office 365, the file Url is the document library url such as “SiteAssets/MyDesignPackage.wsp”, local path is the location of the wsp file in the filesystem.

As I mentioned above that I need to have claim base authentication cookie (“FedAuth”) and set its value as the security token found in the SPOIDCRL. And lastly upload the file in a stream (DON’T use UploadFile method!)

   private static void UploadFile(string siteUrl ,string fileUrl, string localPath, SharePointOnlineCredentials credentials)
            var targetSite = new Uri(siteUrl);

            using (var spWebClient = new SPWebClient())
                var authCookie = credentials.GetAuthenticationCookie(targetSite);
                spWebClient.CookieContainer = new CookieContainer();
                spWebClient.CookieContainer.Add(new Cookie("FedAuth",
                          authCookie.Replace("SPOIDCRL=", string.Empty),
                          string.Empty, targetSite.Authority));
                spWebClient.UseDefaultCredentials = false;
                    Stream stream = spWebClient.OpenWrite(targetSite + fileUrl, "PUT");
                    BinaryWriter writer = new BinaryWriter(stream);
                catch (WebException we)

In the next post I will show you how to use the DesignPackage CSOM to install and apply the Design Package.