private void ApplyComposedLookUnderWeb(object modelHost, WebModelHost typedModelHost, ComposedLookItemLinkDefinition typedDefinition)
        {
            var web = typedModelHost.HostWeb;
            var rootWeb = typedModelHost.HostSite.RootWeb;

            var context = web.Context;
            var serverRelatieSiteUrl = typedModelHost.HostSite.ServerRelativeUrl;

            var composedLookName = typedDefinition.ComposedLookItemName;

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioning,
                Object = web,
                ObjectType = typeof(Web),
                ObjectDefinition = typedDefinition,
                ModelHost = modelHost
            });

            var composedLookList = web.GetCatalog((int)ListTemplateType.DesignCatalog);
            var items = composedLookList.GetItems(CamlQueryTemplates.ItemByFieldValueQuery("Name", composedLookName));

            context.Load(items);
            context.ExecuteQueryWithTrace();

            var targetComposedLookItem = items.Count > 0 ? items[0] : null;

            if (targetComposedLookItem == null)
                throw new SPMeta2Exception(string.Format("Can't find composed look by name: [{0}]", composedLookName));

            var name = ConvertUtils.ToString(targetComposedLookItem["Name"]);

            var masterPageUrl = GetUrlValue(targetComposedLookItem, "MasterPageUrl");

            if (!string.IsNullOrEmpty(masterPageUrl))
            {
                // server relative fun
                var targetMasterPageUrl = UrlUtility.CombineUrl(new[]
                {
                    web.ServerRelativeUrl,
                    "_catalogs/masterpage",
                    Path.GetFileName(masterPageUrl)
                });

                web.CustomMasterUrl = targetMasterPageUrl;
                web.MasterUrl = targetMasterPageUrl;

                web.Update();
                context.ExecuteQueryWithTrace();
            }

            var themeUrl = GetUrlValue(targetComposedLookItem, "ThemeUrl");
            var fontSchemeUrl = GetUrlValue(targetComposedLookItem, "FontSchemeUrl");
            var imageUrl = GetUrlValue(targetComposedLookItem, "ImageUrl");

            themeUrl = GetServerRelativeUrlFromFullUrl(serverRelatieSiteUrl, themeUrl);
            fontSchemeUrl = GetServerRelativeUrlFromFullUrl(serverRelatieSiteUrl, fontSchemeUrl);
            imageUrl = GetServerRelativeUrlFromFullUrl(serverRelatieSiteUrl, imageUrl);

            web.ApplyTheme(themeUrl,
                          !string.IsNullOrEmpty(fontSchemeUrl) ? fontSchemeUrl : null,
                          !string.IsNullOrEmpty(imageUrl) ? imageUrl : null,
                          false);

            context.ExecuteQueryWithTrace();

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioned,
                Object = web,
                ObjectType = typeof(Web),
                ObjectDefinition = typedDefinition,
                ModelHost = modelHost
            });

            // cirrent web was updated by theme engine
            // http://ehikioya.com/apply-themes-to-sharepoint/ 
            typedModelHost.ShouldUpdateHost = false;
        }
 public static ModelNode AddComposedLookItemLink(this ModelNode model, ComposedLookItemLinkDefinition definition, Action<ModelNode> action)
 {
     return model.AddDefinitionNode(definition, action);
 }
        private void ApplyComposedLookUnderWeb(object modelHost, WebModelHost typedModelHost,
            ComposedLookItemLinkDefinition typedDefinition)
        {
            var web = typedModelHost.HostWeb;
            var rootWeb = typedModelHost.HostWeb.Site.RootWeb;

            var siteRelativeUrl = web.Site.ServerRelativeUrl;

            var composedLookName = typedDefinition.ComposedLookItemName;

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioning,
                Object = web,
                ObjectType = typeof(SPWeb),
                ObjectDefinition = typedDefinition,
                ModelHost = modelHost
            });

            var composedLookList = web.GetCatalog(SPListTemplateType.DesignCatalog);
            var items = composedLookList.GetItems(new SPQuery
            {
                Query = CamlQueryUtils.WhereItemByFieldValueQuery("Name", composedLookName),
                RowLimit = 1
            });
            var targetComposedLookItem = items.Count > 0 ? items[0] : null;

            if (targetComposedLookItem == null)
                throw new SPMeta2Exception(string.Format("Can't find composed look by name: [{0}]", composedLookName));

            var name = ConvertUtils.ToString(targetComposedLookItem["Name"]);

            var masterPageUrl = GetUrlValue(targetComposedLookItem, "MasterPageUrl");

            if (!string.IsNullOrEmpty(masterPageUrl))
            {
                var targetMasterPageUrl = masterPageUrl;

                if (SPUrlUtility.IsUrlFull(masterPageUrl))
                    targetMasterPageUrl = web.GetServerRelativeUrlFromUrl(masterPageUrl);

                web.CustomMasterUrl = targetMasterPageUrl;
                web.MasterUrl = targetMasterPageUrl;

                web.Update();
            }

            var themeUrl = GetUrlValue(targetComposedLookItem, "ThemeUrl");
            var fontSchemeUrl = GetUrlValue(targetComposedLookItem, "FontSchemeUrl");
            var imageUrl = GetUrlValue(targetComposedLookItem, "ImageUrl");

            var themeFile = rootWeb.GetFile(themeUrl);
            SPFile fontFile = null;

            if (!string.IsNullOrEmpty(fontSchemeUrl))
                fontFile = rootWeb.GetFile(fontSchemeUrl);

            SPTheme theme = null;

            if (!string.IsNullOrEmpty(imageUrl))
            {
                theme = SPTheme.Open(name,
                    themeFile,
                    fontFile != null && fontFile.Exists ? fontFile : null,
                    new Uri(imageUrl));
            }
            else
            {
                theme = SPTheme.Open(name,
                    themeFile,
                    fontFile != null && fontFile.Exists ? fontFile : null);
            }

            theme.ApplyTo(web, false);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioned,
                Object = web,
                ObjectType = typeof(SPWeb),
                ObjectDefinition = typedDefinition,
                ModelHost = modelHost
            });

            // cirrent web was updated by theme engine
            // http://ehikioya.com/apply-themes-to-sharepoint/ 
            typedModelHost.ShouldUpdateHost = false;
        }
 public static ModelNode AddComposedLookItemLink(this ModelNode model, ComposedLookItemLinkDefinition definition)
 {
     return AddComposedLookItemLink(model, definition, null);
 }