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);
    context.ExecuteQuery();

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

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;
    context.Load(features);
    context.ExecuteQuery();

    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.

2 thoughts on “Apply DesignPackage using SharePoint 2013 Client Object Model

  1. Hello,

    I’ve searched online in may places for a way to activatye a sandbox solution from a SharePoint App. I found no solutions but yours. Does it really work for any sandbox solution?
    Also, I have not been able yet to find out in which assembly and namespace I can find the DesignPackage and DesignPackageInfo classes, can you tell me?

    Kind Regards, Jurgen

    Daniel Laksana Reply:

    The asssembly is Microsoft.SharePoint.Client.Publishing