protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition webpartModel)
        {
            base.ProcessWebpartProperties(webpartInstance, webpartModel);

            var typedWebpart = webpartInstance.WithAssertAndCast<PageViewerWebPart>("webpartInstance", value => value.RequireNotNull());
            var definition = webpartModel.WithAssertAndCast<PageViewerWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            if (!string.IsNullOrEmpty(definition.ContentLink))
            {
                var contentLinkValue = definition.ContentLink ?? string.Empty;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Original contentLinkValue: [{0}]", contentLinkValue);

                contentLinkValue = TokenReplacementService.ReplaceTokens(new TokenReplacementContext
                {
                    Value = contentLinkValue,
                    Context = CurrentHost.HostFile.Web
                }).Value;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Token replaced contentLinkValue: [{0}]", contentLinkValue);

                typedWebpart.ContentLink = contentLinkValue;
            }

            if (!string.IsNullOrEmpty(definition.SourceType))
            {
                typedWebpart.SourceType = (PathPattern)Enum.Parse(typeof(PathPattern), definition.SourceType);
            }
        }
        protected override void OnBeforeDeployModel(WebpartPageModelHost host, WebPartDefinition webpartModel)
        {
            _host = host;

            var typedModel = webpartModel.WithAssertAndCast<ClientWebPartDefinition>("webpartModel", value => value.RequireNotNull());
            typedModel.WebpartType = typeof(ClientWebPart).AssemblyQualifiedName;
        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition webpartModel)
        {
            base.ProcessWebpartProperties(webpartInstance, webpartModel);

            var typedWebpart = webpartInstance.WithAssertAndCast<SPUserCodeWebPart>("webpartInstance", value => value.RequireNotNull());
            var typedModel = webpartModel.WithAssertAndCast<UserCodeWebPartDefinition>("webpartModel", value => value.RequireNotNull());


            // TODO
            typedWebpart.SolutionId = typedModel.SolutionId;
            typedWebpart.AssemblyFullName = typedModel.AssemblyFullName;
            typedWebpart.TypeFullName = typedModel.TypeFullName;

            foreach (var prop in typedModel.UserCodeProperties)
            {
                var currentProperty = typedWebpart.Properties
                    .OfType<SPUserCodeProperty>()
                    .FirstOrDefault(p => p.Name.ToUpper() == prop.Name.ToUpper());

                if (currentProperty == null)
                {
                    currentProperty = new SPUserCodeProperty();
                    currentProperty.Name = prop.Name;

                    typedWebpart.Properties.Add(currentProperty);
                }

                currentProperty.Value = prop.Value;
            }
        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition webpartModel)
        {
            base.ProcessWebpartProperties(webpartInstance, webpartModel);

            var typedWebpart = webpartInstance.WithAssertAndCast<SilverlightWebPart>("webpartInstance", value => value.RequireNotNull());
            var typedDefinition = webpartModel.WithAssertAndCast<SilverlightWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            if (!string.IsNullOrEmpty(typedDefinition.Url))
            {
                var linkValue = typedDefinition.Url;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Original Url: [{0}]", linkValue);

                linkValue = TokenReplacementService.ReplaceTokens(new TokenReplacementContext
                {
                    Value = linkValue,
                    Context = CurrentHost.HostFile.Web
                }).Value;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Token replaced Url: [{0}]", linkValue);

                typedWebpart.Url = linkValue;
            }

            if (!string.IsNullOrEmpty(typedDefinition.CustomInitParameters))
            {
                typedWebpart.CustomInitParameters = typedDefinition.CustomInitParameters;
            }
        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition webpartModel)
        {
            base.ProcessWebpartProperties(webpartInstance, webpartModel);

            var typedWebpart = webpartInstance.WithAssertAndCast<ContactFieldControl>("webpartInstance", value => value.RequireNotNull());
            var typedModel = webpartModel.WithAssertAndCast<ContactFieldControlDefinition>("webpartModel", value => value.RequireNotNull());

        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition webpartModel)
        {
            base.ProcessWebpartProperties(webpartInstance, webpartModel);

            var typedWebpart = webpartInstance.WithAssertAndCast<UserDocsWebPart>("webpartInstance", value => value.RequireNotNull());
            var definition = webpartModel.WithAssertAndCast<UserDocsWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            // TODO, specific provision
        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition webpartModel)
        {
            base.ProcessWebpartProperties(webpartInstance, webpartModel);

            var typedWebpart = webpartInstance.WithAssertAndCast<ScriptEditorWebPart>("webpartInstance", value => value.RequireNotNull());
            var typedModel = webpartModel.WithAssertAndCast<ScriptEditorWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            typedWebpart.Content = typedModel.Content ?? string.Empty;
        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition webpartModel)
        {
            base.ProcessWebpartProperties(webpartInstance, webpartModel);

            var typedWebpart = webpartInstance.WithAssertAndCast<ClientWebPart>("webpartInstance", value => value.RequireNotNull());
            var typedModel = webpartModel.WithAssertAndCast<ClientWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            typedWebpart.FeatureId = typedModel.FeatureId;
            typedWebpart.ProductId = typedModel.ProductId;
            typedWebpart.WebPartName = typedModel.WebPartName;
            typedWebpart.ProductWebId = _host.SPLimitedWebPartManager.Web.ID;
        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition definition)
        {
            base.ProcessWebpartProperties(webpartInstance, definition);

            var typedWebpart = webpartInstance.WithAssertAndCast<RefinementScriptWebPart>("webpartInstance", value => value.RequireNotNull());
            var typedModel = definition.WithAssertAndCast<RefinementScriptWebPartDefinition>("webpartModel", value => value.RequireNotNull());


            if (!string.IsNullOrEmpty(typedModel.SelectedRefinementControlsJson))
                typedWebpart.SelectedRefinementControlsJson = typedModel.SelectedRefinementControlsJson;

            if (!string.IsNullOrEmpty(typedModel.EmptyMessage))
                typedWebpart.EmptyMessage = typedModel.EmptyMessage;
        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition webpartModel)
        {
            base.ProcessWebpartProperties(webpartInstance, webpartModel);

            var typedWebpart = webpartInstance.WithAssertAndCast<SiteFeedWebPart>("webpartInstance", value => value.RequireNotNull());
            var typedModel = webpartModel.WithAssertAndCast<SiteFeedWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            //typedWebpart.ContentLink = typedModel.ContentLink ?? string.Empty;

            //var xmlDoc = new XmlDocument();
            //var xmlElement = xmlDoc.CreateElement("ContentElement");
            //xmlElement.InnerText = typedModel.Content ?? string.Empty;

            //typedWebpart.Content = xmlElement;
        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition webpartModel)
        {
            base.ProcessWebpartProperties(webpartInstance, webpartModel);

            var typedWebpart = webpartInstance.WithAssertAndCast<SPUserCodeWebPart>("webpartInstance", value => value.RequireNotNull());
            var typedModel = webpartModel.WithAssertAndCast<UserCodeWebPartDefinition>("webpartModel", value => value.RequireNotNull());


            // TODO
            typedWebpart.SolutionId = typedModel.SolutionId;
            typedWebpart.AssemblyFullName = typedModel.AssemblyFullName;
            typedWebpart.TypeFullName = typedModel.TypeFullName;

            foreach (var prop in typedModel.UserCodeProperties)
            {
                var currentProperty = typedWebpart.Properties
                    .OfType<SPUserCodeProperty>()
                    .FirstOrDefault(p => p.Name.ToUpper() == prop.Name.ToUpper());

                if (currentProperty == null)
                {
                    currentProperty = new SPUserCodeProperty
                    {
                        Name = prop.Name
                    };

                    typedWebpart.Properties.Add(currentProperty);
                }

                if (prop.IsTokenisable.HasValue && prop.IsTokenisable.Value)
                {
                    // Enhance 'UserCodeWebPartDefinition' 
                    // UserCodeProperty should support ~sitecollection/~site tokens #485

                    var value = TokenReplacementService.ReplaceTokens(new TokenReplacementContext
                    {
                        Value = prop.Value,
                        Context = CurrentHost.HostFile.Web
                    }).Value;

                    currentProperty.Value = value;
                }
                else
                {
                    currentProperty.Value = prop.Value;
                }
            }
        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition definition)
        {
            base.ProcessWebpartProperties(webpartInstance, definition);

            var typedWebpart = webpartInstance.WithAssertAndCast<ContentBySearchWebPart>("webpartInstance", value => value.RequireNotNull());
            var typedModel = definition.WithAssertAndCast<ContentBySearchWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            // templates
            if (!string.IsNullOrEmpty(typedModel.GroupTemplateId))
                typedWebpart.GroupTemplateId = typedModel.GroupTemplateId;

            if (!string.IsNullOrEmpty(typedModel.ItemTemplateId))
                typedWebpart.ItemTemplateId = typedModel.ItemTemplateId;

            if (!string.IsNullOrEmpty(typedModel.RenderTemplateId))
                typedWebpart.RenderTemplateId = typedModel.RenderTemplateId;

            if (!string.IsNullOrEmpty(typedModel.DataProviderJSON))
                typedWebpart.DataProviderJSON = typedModel.DataProviderJSON;

            if (!string.IsNullOrEmpty(typedModel.PropertyMappings))
                typedWebpart.PropertyMappings = typedModel.PropertyMappings;

            if (typedModel.OverwriteResultPath.HasValue)
                typedWebpart.OverwriteResultPath = typedModel.OverwriteResultPath.Value;

            if (typedModel.ShouldHideControlWhenEmpty.HasValue)
                typedWebpart.ShouldHideControlWhenEmpty = typedModel.ShouldHideControlWhenEmpty.Value;

            if (typedModel.LogAnalyticsViewEvent.HasValue)
                typedWebpart.LogAnalyticsViewEvent = typedModel.LogAnalyticsViewEvent.Value;

            if (typedModel.AddSEOPropertiesFromSearch.HasValue)
                typedWebpart.AddSEOPropertiesFromSearch = typedModel.AddSEOPropertiesFromSearch.Value;

            if (typedModel.StartingItemIndex.HasValue)
                typedWebpart.StartingItemIndex = typedModel.StartingItemIndex.Value;

            // misc
            if (typedModel.NumberOfItems.HasValue)
                typedWebpart.NumberOfItems = typedModel.NumberOfItems.Value;

            if (typedModel.ResultsPerPage.HasValue)
                typedWebpart.ResultsPerPage = typedModel.ResultsPerPage.Value;
        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition webpartModel)
        {
            base.ProcessWebpartProperties(webpartInstance, webpartModel);

            var typedWebpart = webpartInstance.WithAssertAndCast<ResultScriptWebPart>("webpartInstance", value => value.RequireNotNull());
            var definition = webpartModel.WithAssertAndCast<ResultScriptWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            if (!string.IsNullOrEmpty(definition.DataProviderJSON))
                typedWebpart.DataProviderJSON = definition.DataProviderJSON;

            if (!string.IsNullOrEmpty(definition.EmptyMessage))
                typedWebpart.EmptyMessage = definition.EmptyMessage;

            if (definition.ResultsPerPage.HasValue)
                typedWebpart.ResultsPerPage = definition.ResultsPerPage.Value;

            if (definition.ShowResultCount.HasValue)
                typedWebpart.ShowResultCount = definition.ShowResultCount.Value;
        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition webpartModel)
        {
            base.ProcessWebpartProperties(webpartInstance, webpartModel);

            var typedWebpart = webpartInstance.WithAssertAndCast<ClientWebPart>("webpartInstance", value => value.RequireNotNull());
            var typedModel = webpartModel.WithAssertAndCast<ClientWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            // Enhance 'ClientWebPart' provision - ProductWebId should be current web by default #623
            // https://github.com/SubPointSolutions/spmeta2/issues/623
            var productId = typedModel.ProductId;

            if (!productId.HasGuidValue())
                productId = _host.SPLimitedWebPartManager.Web.ID;

            typedWebpart.FeatureId = typedModel.FeatureId;
            typedWebpart.ProductId = productId.Value;
            typedWebpart.WebPartName = typedModel.WebPartName;
            typedWebpart.ProductWebId = _host.SPLimitedWebPartManager.Web.ID;
        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition webpartModel)
        {
            base.ProcessWebpartProperties(webpartInstance, webpartModel);

            var typedWebpart = webpartInstance.WithAssertAndCast<ListViewWebPart>("webpartInstance", value => value.RequireNotNull());
            var typedModel = webpartModel.WithAssertAndCast<ListViewWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            var web = _host.SPLimitedWebPartManager.Web;
            SPList list = null;

            if (typedModel.ListId.HasGuidValue())
                list = web.Lists[typedModel.ListId.Value];
            else if (!string.IsNullOrEmpty(typedModel.ListUrl))
                list = web.GetList(SPUrlUtility.CombineUrl(web.ServerRelativeUrl, typedModel.ListUrl));
            else if (!string.IsNullOrEmpty(typedModel.ListTitle))
                list = web.Lists.TryGetList(typedModel.ListTitle);


            typedWebpart.ListName = list.ID.ToString();
            typedWebpart.ListId = list.ID;

            // view check
            if (list != null)
            {
                SPView view = null;

                if (typedModel.ViewId.HasGuidValue())
                    view = list.Views[typedModel.ViewId.Value];
                else if (!string.IsNullOrEmpty(typedModel.ViewName))
                    view = list.Views[typedModel.ViewName];

                if (view != null)
                {
                    typedWebpart.ViewGuid = view.ID.ToString("B").ToUpperInvariant();
                    typedWebpart.TitleUrl = view.ServerRelativeUrl;
                }
            }

            // able to 'reset', if NULL or use list-view based URLs
            if (!string.IsNullOrEmpty(typedModel.TitleUrl))
                typedWebpart.TitleUrl = typedModel.TitleUrl;
        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition webpartModel)
        {
            base.ProcessWebpartProperties(webpartInstance, webpartModel);

            var typedWebpart = webpartInstance.WithAssertAndCast<ContentEditorWebPart>("webpartInstance", value => value.RequireNotNull());
            var typedModel = webpartModel.WithAssertAndCast<ContentEditorWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            if (!string.IsNullOrEmpty(typedModel.ContentLink))
            {
                var contentLinkValue = typedModel.ContentLink ?? string.Empty;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Original contentLinkValue: [{0}]",
                    contentLinkValue);

                contentLinkValue = TokenReplacementService.ReplaceTokens(new TokenReplacementContext
                {
                    Value = contentLinkValue,
                    Context = CurrentHost.PageListItem.Web
                }).Value;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Token replaced contentLinkValue: [{0}]", contentLinkValue);

                typedWebpart.ContentLink = contentLinkValue;
            }

            if (!string.IsNullOrEmpty(typedModel.Content))
            {

                var xmlDoc = new XmlDocument();
                var xmlElement = xmlDoc.CreateElement("ContentElement");

                var content = typedModel.Content ?? string.Empty;

                xmlElement.InnerText = content;
                typedWebpart.Content = xmlElement;
            }
        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition webpartModel)
        {
            base.ProcessWebpartProperties(webpartInstance, webpartModel);

            var typedWebpart = webpartInstance.WithAssertAndCast<XsltListViewWebPart>("webpartInstance", value => value.RequireNotNull());
            var typedModel = webpartModel.WithAssertAndCast<XsltListViewWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            var web = _host.SPLimitedWebPartManager.Web;

            // bind list
            SPList list = null;

            if (typedModel.ListId.HasValue && typedModel.ListId != default(Guid))
                list = web.Lists[typedModel.ListId.Value];
            else if (!string.IsNullOrEmpty(typedModel.ListUrl))
                list = web.GetList(SPUrlUtility.CombineUrl(web.ServerRelativeUrl, typedModel.ListUrl));
            else if (!string.IsNullOrEmpty(typedModel.ListTitle))
                list = web.Lists.TryGetList(typedModel.ListTitle);
            else
            {
                throw new SPMeta2Exception("ListUrl, ListTitle or ListId should be defined.");
            }

            if (list != null)
            {
                typedWebpart.ListName = list.ID.ToString("B").ToUpperInvariant();
                typedWebpart.TitleUrl = list.DefaultViewUrl;
            }

            // view check
            if (list != null)
            {
                SPView srcView = null;

                if (typedModel.ViewId.HasValue && typedModel.ViewId != default(Guid))
                    srcView = list.Views[typedModel.ViewId.Value];
                else if (!string.IsNullOrEmpty(typedModel.ViewName))
                    srcView = list.Views[typedModel.ViewName];

                if (srcView != null)
                {
                    if (!string.IsNullOrEmpty(typedWebpart.ViewGuid))
                    {
                        // update hidden view, otherwise we can have weird SharePoint exception
                        // https://github.com/SubPointSolutions/spmeta2/issues/487

                        var hiddenView = list.Views[new Guid(typedWebpart.ViewGuid)];

                        hiddenView.ViewFields.DeleteAll();

                        foreach (string f in srcView.ViewFields)
                            hiddenView.ViewFields.Add(f);

                        hiddenView.RowLimit = srcView.RowLimit;
                        hiddenView.Query = srcView.Query;
#if !NET35
                        hiddenView.JSLink = srcView.JSLink;
#endif
                        hiddenView.IncludeRootFolder = srcView.IncludeRootFolder;
                        hiddenView.Scope = srcView.Scope;

                        hiddenView.Update();
                    }
                    else
                    {
                        typedWebpart.ViewGuid = srcView.ID.ToString("B").ToUpperInvariant();
                    }

                    typedWebpart.TitleUrl = srcView.ServerRelativeUrl;
                }
            }

            // able to 'reset', if NULL or use list-view based URLs
            if (!string.IsNullOrEmpty(typedModel.TitleUrl))
                typedWebpart.TitleUrl = typedModel.TitleUrl;

            // weird, but it must be set to avoid null-ref exceptions
            typedWebpart.GhostedXslLink = "main.xsl";

#if !NET35
            // rest
            typedWebpart.JSLink = typedModel.JSLink;
#endif

            if (typedModel.CacheXslStorage.HasValue)
                typedWebpart.CacheXslStorage = typedModel.CacheXslStorage.Value;

            if (typedModel.CacheXslTimeOut.HasValue)
                typedWebpart.CacheXslTimeOut = typedModel.CacheXslTimeOut.Value;

#if !NET35
            if (typedModel.ShowTimelineIfAvailable.HasValue)
                typedWebpart.ShowTimelineIfAvailable = typedModel.ShowTimelineIfAvailable.Value;
#endif

            if (!string.IsNullOrEmpty(typedModel.Xsl))
            {
                typedWebpart.Xsl = typedModel.Xsl;
            }

            if (!string.IsNullOrEmpty(typedModel.XslLink))
            {
                var urlValue = typedModel.XslLink;

                typedWebpart.XslLink = urlValue;
            }

            if (!string.IsNullOrEmpty(typedModel.XmlDefinition))
            {
                typedWebpart.XmlDefinition = typedModel.XmlDefinition;
            }

            if (!string.IsNullOrEmpty(typedModel.XmlDefinitionLink))
            {
                var urlValue = typedModel.XmlDefinitionLink;
                typedWebpart.XmlDefinitionLink = urlValue;
            }

            if (!string.IsNullOrEmpty(typedModel.GhostedXslLink))
            {
                var urlValue = typedModel.GhostedXslLink;
                typedWebpart.GhostedXslLink = urlValue;
            }

            if (!string.IsNullOrEmpty(typedModel.BaseXsltHashKey))
                typedWebpart.BaseXsltHashKey = typedModel.BaseXsltHashKey;

            if (typedModel.DisableColumnFiltering.HasValue)
                typedWebpart.DisableColumnFiltering = typedModel.DisableColumnFiltering.Value;

#if !NET35
            if (typedModel.DisableSaveAsNewViewButton.HasValue)
                typedWebpart.DisableSaveAsNewViewButton = typedModel.DisableSaveAsNewViewButton.Value;

            if (typedModel.DisableViewSelectorMenu.HasValue)
                typedWebpart.DisableViewSelectorMenu = typedModel.DisableViewSelectorMenu.Value;

            if (typedModel.InplaceSearchEnabled.HasValue)
                typedWebpart.InplaceSearchEnabled = typedModel.InplaceSearchEnabled.Value;
#endif
        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition definition)
        {
            base.ProcessWebpartProperties(webpartInstance, definition);

            var typedWebpart = webpartInstance.WithAssertAndCast<ContentByQueryWebPart>("webpartInstance", value => value.RequireNotNull());
            var typedDefinition = definition.WithAssertAndCast<ContentByQueryWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            // xslt links
            if (!string.IsNullOrEmpty(typedDefinition.MainXslLink))
            {
                var linkValue = typedDefinition.MainXslLink;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Original MainXslLink: [{0}]", linkValue);

                linkValue = TokenReplacementService.ReplaceTokens(new TokenReplacementContext
                {
                    Value = linkValue,
                    Context = CurrentHost.PageListItem.Web
                }).Value;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Token replaced MainXslLink: [{0}]", linkValue);

                typedWebpart.MainXslLink = linkValue;
            }

            if (!string.IsNullOrEmpty(typedDefinition.ItemXslLink))
            {
                var linkValue = typedDefinition.ItemXslLink;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Original ItemXslLink: [{0}]", linkValue);

                linkValue = TokenReplacementService.ReplaceTokens(new TokenReplacementContext
                {
                    Value = linkValue,
                    Context = CurrentHost.PageListItem.Web
                }).Value;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Token replaced ItemXslLink: [{0}]", linkValue);

                typedWebpart.ItemXslLink = linkValue;
            }

            if (!string.IsNullOrEmpty(typedDefinition.HeaderXslLink))
            {
                var linkValue = typedDefinition.HeaderXslLink;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Original HeaderXslLink: [{0}]", linkValue);

                linkValue = TokenReplacementService.ReplaceTokens(new TokenReplacementContext
                {
                    Value = linkValue,
                    Context = CurrentHost.PageListItem.Web
                }).Value;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Token replaced HeaderXslLink: [{0}]", linkValue);

                typedWebpart.HeaderXslLink = linkValue;
            }

            // styles
            if (!string.IsNullOrEmpty(typedDefinition.ItemStyle))
                typedWebpart.ItemStyle = typedDefinition.ItemStyle;

            if (!string.IsNullOrEmpty(typedDefinition.GroupStyle))
                typedWebpart.GroupStyle = typedDefinition.GroupStyle;


            // cache settings
            if (typedDefinition.UseCache.HasValue)
                typedWebpart.UseCache = typedDefinition.UseCache.Value;

            if (typedDefinition.CacheXslStorage.HasValue)
                typedWebpart.CacheXslStorage = typedDefinition.CacheXslStorage.Value;

            if (typedDefinition.CacheXslTimeOut.HasValue)
                typedWebpart.CacheXslTimeOut = typedDefinition.CacheXslTimeOut.Value;

            // item limit
            if (typedDefinition.ItemLimit.HasValue)
                typedWebpart.ItemLimit = typedDefinition.ItemLimit.Value;

            // mappings
            if (!string.IsNullOrEmpty(typedDefinition.DataMappings))
                typedWebpart.DataMappings = typedDefinition.DataMappings;

            if (!string.IsNullOrEmpty(typedDefinition.DataMappingViewFields))
                typedWebpart.DataMappingViewFields = typedDefinition.DataMappingViewFields;

            // misc
            if (typedDefinition.ShowUntargetedItems.HasValue)
                typedWebpart.ShowUntargetedItems = typedDefinition.ShowUntargetedItems.Value;

            if (typedDefinition.PlayMediaInBrowser.HasValue)
                typedWebpart.PlayMediaInBrowser = typedDefinition.PlayMediaInBrowser.Value;

            // FilterTypeXXX
            if (!string.IsNullOrEmpty(typedDefinition.FilterType1))
                typedWebpart.FilterType1 = typedDefinition.FilterType1;

            if (!string.IsNullOrEmpty(typedDefinition.FilterType2))
                typedWebpart.FilterType2 = typedDefinition.FilterType2;

            if (!string.IsNullOrEmpty(typedDefinition.FilterType3))
                typedWebpart.FilterType3 = typedDefinition.FilterType3;

            // FilterFieldXXX
            if (!string.IsNullOrEmpty(typedDefinition.FilterField1))
                typedWebpart.FilterField1 = typedDefinition.FilterField1;

            if (!string.IsNullOrEmpty(typedDefinition.FilterField2))
                typedWebpart.FilterField2 = typedDefinition.FilterField2;

            if (!string.IsNullOrEmpty(typedDefinition.FilterField3))
                typedWebpart.FilterField3 = typedDefinition.FilterField3;

            // FilterXXXIsCustomValue
            if (typedDefinition.Filter1IsCustomValue.HasValue)
                typedWebpart.Filter1IsCustomValue = typedDefinition.Filter1IsCustomValue.Value;

            if (typedDefinition.Filter2IsCustomValue.HasValue)
                typedWebpart.Filter2IsCustomValue = typedDefinition.Filter2IsCustomValue.Value;

            if (typedDefinition.Filter3IsCustomValue.HasValue)
                typedWebpart.Filter3IsCustomValue = typedDefinition.Filter3IsCustomValue.Value;

            // FilterValueXXX
            if (!string.IsNullOrEmpty(typedDefinition.FilterValue1))
                typedWebpart.FilterValue1 = typedDefinition.FilterValue1;

            if (!string.IsNullOrEmpty(typedDefinition.FilterValue2))
                typedWebpart.FilterValue2 = typedDefinition.FilterValue2;

            if (!string.IsNullOrEmpty(typedDefinition.FilterValue3))
                typedWebpart.FilterValue3 = typedDefinition.FilterValue3;


            if (!string.IsNullOrEmpty(typedDefinition.Filter1ChainingOperator))
            {
                typedWebpart.Filter1ChainingOperator = (ContentByQueryWebPart.FilterChainingOperator)
                   Enum.Parse(typeof(ContentByQueryWebPart.FilterChainingOperator), typedDefinition.Filter1ChainingOperator);
            }

            if (!string.IsNullOrEmpty(typedDefinition.Filter2ChainingOperator))
            {
                typedWebpart.Filter2ChainingOperator = (ContentByQueryWebPart.FilterChainingOperator)
                   Enum.Parse(typeof(ContentByQueryWebPart.FilterChainingOperator), typedDefinition.Filter2ChainingOperator);
            }


            // sorting
            if (!string.IsNullOrEmpty(typedDefinition.SortBy))
                typedWebpart.SortBy = typedDefinition.SortBy;

            if (!string.IsNullOrEmpty(typedDefinition.SortByDirection))
                typedWebpart.SortByDirection = (ContentByQueryWebPart.SortDirection)
                    Enum.Parse(typeof(ContentByQueryWebPart.SortDirection), typedDefinition.SortByDirection);

            if (!string.IsNullOrEmpty(typedDefinition.SortByFieldType))
                typedWebpart.SortByFieldType = typedDefinition.SortByFieldType;

            if (!string.IsNullOrEmpty(typedDefinition.GroupByDirection))
            {
                typedWebpart.GroupByDirection = (ContentByQueryWebPart.SortDirection)
                    Enum.Parse(typeof(ContentByQueryWebPart.SortDirection), typedDefinition.GroupByDirection);
            }


            // FilterOperatorXXX
            if (!string.IsNullOrEmpty(typedDefinition.FilterOperator1))
            {
                typedWebpart.FilterOperator1 = (ContentByQueryWebPart.FilterFieldQueryOperator)
                    Enum.Parse(typeof(ContentByQueryWebPart.FilterFieldQueryOperator), typedDefinition.FilterOperator1);
            }

            if (!string.IsNullOrEmpty(typedDefinition.FilterOperator2))
            {
                typedWebpart.FilterOperator2 = (ContentByQueryWebPart.FilterFieldQueryOperator)
                    Enum.Parse(typeof(ContentByQueryWebPart.FilterFieldQueryOperator), typedDefinition.FilterOperator2);
            }

            if (!string.IsNullOrEmpty(typedDefinition.FilterOperator3))
            {
                typedWebpart.FilterOperator3 = (ContentByQueryWebPart.FilterFieldQueryOperator)
                    Enum.Parse(typeof(ContentByQueryWebPart.FilterFieldQueryOperator), typedDefinition.FilterOperator3);
            }

            // FilterDisplayValueXXX

            if (!string.IsNullOrEmpty(typedDefinition.FilterDisplayValue1))
                typedWebpart.FilterDisplayValue1 = typedDefinition.FilterDisplayValue1;

            if (!string.IsNullOrEmpty(typedDefinition.FilterDisplayValue2))
                typedWebpart.FilterDisplayValue2 = typedDefinition.FilterDisplayValue2;

            if (!string.IsNullOrEmpty(typedDefinition.FilterDisplayValue3))
                typedWebpart.FilterDisplayValue3 = typedDefinition.FilterDisplayValue3;


            // bindings
            if (typedDefinition.ServerTemplate.HasValue)
                typedWebpart.ServerTemplate = typedDefinition.ServerTemplate.ToString();

            if (!string.IsNullOrEmpty(typedDefinition.ContentTypeName))
                typedWebpart.ContentTypeName = typedDefinition.ContentTypeName;

            if (!string.IsNullOrEmpty(typedDefinition.ContentTypeBeginsWithId))
                typedWebpart.ContentTypeBeginsWithId = typedDefinition.ContentTypeBeginsWithId;

            if (typedDefinition.ListId.HasGuidValue())
                typedWebpart.ListId = typedDefinition.ListId.Value;

            if (typedDefinition.ListGuid.HasGuidValue())
                typedWebpart.ListGuid = typedDefinition.ListGuid.Value.ToString("D");

            if (!string.IsNullOrEmpty(typedDefinition.ListName))
                typedWebpart.ListName = typedDefinition.ListName;

            if (!string.IsNullOrEmpty(typedDefinition.WebUrl))
                typedWebpart.WebUrl = typedDefinition.WebUrl;

            // overrides
            if (!string.IsNullOrEmpty(typedDefinition.ListsOverride))
                typedWebpart.ListsOverride = typedDefinition.ListsOverride;

            if (!string.IsNullOrEmpty(typedDefinition.ViewFieldsOverride))
                typedWebpart.ViewFieldsOverride = typedDefinition.ViewFieldsOverride;

            if (!string.IsNullOrEmpty(typedDefinition.QueryOverride))
                typedWebpart.QueryOverride = typedDefinition.QueryOverride;

            if (!string.IsNullOrEmpty(typedDefinition.CommonViewFields))
                typedWebpart.CommonViewFields = typedDefinition.CommonViewFields;

            if (typedDefinition.FilterByAudience.HasValue)
                typedWebpart.FilterByAudience = typedDefinition.FilterByAudience.Value;
        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition webpartModel)
        {
            base.ProcessWebpartProperties(webpartInstance, webpartModel);

            var typedWebpart = webpartInstance.WithAssertAndCast<XsltListViewWebPart>("webpartInstance", value => value.RequireNotNull());
            var typedModel = webpartModel.WithAssertAndCast<XsltListViewWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            var web = _host.SPLimitedWebPartManager.Web;

            // bind list
            var targetWeb = web;

            if (!string.IsNullOrEmpty(typedModel.WebUrl) || typedModel.WebId.HasGuidValue())
                targetWeb = new LookupFieldModelHandler().GetTargetWeb(web.Site, typedModel.WebUrl, typedModel.WebId);

            var list = GetTargetList(targetWeb, typedModel.ListTitle, typedModel.ListUrl, typedModel.ListId);

            if (list != null)
            {
                typedWebpart.ListName = list.ID.ToString("B").ToUpperInvariant();
                typedWebpart.TitleUrl = list.DefaultViewUrl;
            }

            // view check
            if (list != null)
            {
                SPView srcView = null;

                if (typedModel.ViewId.HasValue && typedModel.ViewId != default(Guid))
                    srcView = list.Views[typedModel.ViewId.Value];
                else if (!string.IsNullOrEmpty(typedModel.ViewName))
                    srcView = list.Views[typedModel.ViewName];

                if (srcView != null)
                {
                    if (!string.IsNullOrEmpty(typedWebpart.ViewGuid))
                    {
                        // update hidden view, otherwise we can have weird SharePoint exception
                        // https://github.com/SubPointSolutions/spmeta2/issues/487

                        var hiddenView = list.Views[new Guid(typedWebpart.ViewGuid)];

                        hiddenView.SetViewXml(srcView.GetViewXml());
                     
                        hiddenView.Update();
                    }
                    else
                    {
                        typedWebpart.ViewGuid = srcView.ID.ToString("B").ToUpperInvariant();
                    }

                    typedWebpart.TitleUrl = srcView.ServerRelativeUrl;
                }
            }

            // able to 'reset', if NULL or use list-view based URLs
            if (!string.IsNullOrEmpty(typedModel.TitleUrl))
                typedWebpart.TitleUrl = typedModel.TitleUrl;

            // weird, but it must be set to avoid null-ref exceptions
            typedWebpart.GhostedXslLink = "main.xsl";

#if !NET35
            // rest
            typedWebpart.JSLink = typedModel.JSLink;
#endif

            if (typedModel.CacheXslStorage.HasValue)
                typedWebpart.CacheXslStorage = typedModel.CacheXslStorage.Value;

            if (typedModel.CacheXslTimeOut.HasValue)
                typedWebpart.CacheXslTimeOut = typedModel.CacheXslTimeOut.Value;

#if !NET35
            if (typedModel.ShowTimelineIfAvailable.HasValue)
                typedWebpart.ShowTimelineIfAvailable = typedModel.ShowTimelineIfAvailable.Value;
#endif

            if (!string.IsNullOrEmpty(typedModel.Xsl))
            {
                typedWebpart.Xsl = typedModel.Xsl;
            }

            if (!string.IsNullOrEmpty(typedModel.XslLink))
            {
                var urlValue = typedModel.XslLink;

                typedWebpart.XslLink = urlValue;
            }

            if (!string.IsNullOrEmpty(typedModel.XmlDefinition))
            {
                typedWebpart.XmlDefinition = typedModel.XmlDefinition;
            }

            if (!string.IsNullOrEmpty(typedModel.XmlDefinitionLink))
            {
                var urlValue = typedModel.XmlDefinitionLink;
                typedWebpart.XmlDefinitionLink = urlValue;
            }

            if (!string.IsNullOrEmpty(typedModel.GhostedXslLink))
            {
                var urlValue = typedModel.GhostedXslLink;
                typedWebpart.GhostedXslLink = urlValue;
            }

            if (!string.IsNullOrEmpty(typedModel.BaseXsltHashKey))
                typedWebpart.BaseXsltHashKey = typedModel.BaseXsltHashKey;

            if (typedModel.DisableColumnFiltering.HasValue)
                typedWebpart.DisableColumnFiltering = typedModel.DisableColumnFiltering.Value;

#if !NET35
            if (typedModel.DisableSaveAsNewViewButton.HasValue)
                typedWebpart.DisableSaveAsNewViewButton = typedModel.DisableSaveAsNewViewButton.Value;

            if (typedModel.DisableViewSelectorMenu.HasValue)
                typedWebpart.DisableViewSelectorMenu = typedModel.DisableViewSelectorMenu.Value;

            if (typedModel.InplaceSearchEnabled.HasValue)
                typedWebpart.InplaceSearchEnabled = typedModel.InplaceSearchEnabled.Value;
#endif
        }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition webpartModel)
        {
            base.ProcessWebpartProperties(webpartInstance, webpartModel);

            var typedWebpart = webpartInstance.WithAssertAndCast<ListViewWebPart>("webpartInstance", value => value.RequireNotNull());
            var typedModel = webpartModel.WithAssertAndCast<ListViewWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            var web = _host.SPLimitedWebPartManager.Web;

            var targetWeb = web;

            if (!string.IsNullOrEmpty(typedModel.WebUrl) || typedModel.WebId.HasGuidValue())
                targetWeb = new LookupFieldModelHandler().GetTargetWeb(web.Site, typedModel.WebUrl, typedModel.WebId);

            var list = XsltListViewWebPartModelHandler.GetTargetList(targetWeb, typedModel.ListTitle, typedModel.ListUrl, typedModel.ListId);

            typedWebpart.ListName = list.ID.ToString();
            typedWebpart.ListId = list.ID;

            // view check
            if (list != null)
            {
                SPView view = null;

                if (typedModel.ViewId.HasGuidValue())
                    view = list.Views[typedModel.ViewId.Value];
                else if (!string.IsNullOrEmpty(typedModel.ViewName))
                    view = list.Views[typedModel.ViewName];
                else if (!string.IsNullOrEmpty(typedModel.ViewUrl))
                {
                    view = list.Views.OfType<SPView>()
                        .FirstOrDefault(v => v.ServerRelativeUrl.ToUpper().EndsWith(typedModel.ViewUrl.ToUpper()));
                }

                if (view != null)
                {
                    typedWebpart.ViewGuid = view.ID.ToString("B").ToUpperInvariant();
                    typedWebpart.TitleUrl = view.ServerRelativeUrl;
                }
            }

            // able to 'reset', if NULL or use list-view based URLs
            if (!string.IsNullOrEmpty(typedModel.TitleUrl))
                typedWebpart.TitleUrl = typedModel.TitleUrl;
        }
 protected override void OnBeforeDeployModel(WebpartPageModelHost host, WebPartDefinition webpartModel)
 {
     var typedModel = webpartModel.WithAssertAndCast<PictureLibrarySlideshowWebPartDefinition>("webpartModel", value => value.RequireNotNull());
     typedModel.WebpartType = typeof(PictureLibrarySlideshowWebPart).AssemblyQualifiedName;
 }
        protected override void ProcessWebpartProperties(WebPart webpartInstance, WebPartDefinition definition)
        {
            base.ProcessWebpartProperties(webpartInstance, definition);

            var typedWebpart = webpartInstance.WithAssertAndCast<ContentByQueryWebPart>("webpartInstance", value => value.RequireNotNull());
            var typedDefinition = definition.WithAssertAndCast<ContentByQueryWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            // xslt links
            if (!string.IsNullOrEmpty(typedDefinition.MainXslLink))
            {
                var linkValue = typedDefinition.MainXslLink;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Original MainXslLink: [{0}]", linkValue);

                linkValue = TokenReplacementService.ReplaceTokens(new TokenReplacementContext
                {
                    Value = linkValue,
                    Context = CurrentHost.HostFile.Web
                }).Value;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Token replaced MainXslLink: [{0}]", linkValue);

                typedWebpart.MainXslLink = linkValue;
            }

            if (!string.IsNullOrEmpty(typedDefinition.ItemXslLink))
            {
                var linkValue = typedDefinition.ItemXslLink;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Original ItemXslLink: [{0}]", linkValue);

                linkValue = TokenReplacementService.ReplaceTokens(new TokenReplacementContext
                {
                    Value = linkValue,
                    Context = CurrentHost.HostFile.Web
                }).Value;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Token replaced ItemXslLink: [{0}]", linkValue);

                typedWebpart.ItemXslLink = linkValue;
            }

            if (!string.IsNullOrEmpty(typedDefinition.HeaderXslLink))
            {
                var linkValue = typedDefinition.HeaderXslLink;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Original HeaderXslLink: [{0}]", linkValue);

                linkValue = TokenReplacementService.ReplaceTokens(new TokenReplacementContext
                {
                    Value = linkValue,
                    Context = CurrentHost.HostFile.Web
                }).Value;

                TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Token replaced HeaderXslLink: [{0}]", linkValue);

                typedWebpart.HeaderXslLink = linkValue;
            }

            // styles
            if (!string.IsNullOrEmpty(typedDefinition.ItemStyle))
                typedWebpart.ItemStyle = typedDefinition.ItemStyle;

            if (!string.IsNullOrEmpty(typedDefinition.GroupStyle))
                typedWebpart.GroupStyle = typedDefinition.GroupStyle;

            // cache settings
            if (typedDefinition.UseCache.HasValue)
                typedWebpart.UseCache = typedDefinition.UseCache.Value;

            if (typedDefinition.CacheXslStorage.HasValue)
                typedWebpart.CacheXslStorage = typedDefinition.CacheXslStorage.Value;

            if (typedDefinition.CacheXslTimeOut.HasValue)
                typedWebpart.CacheXslTimeOut = typedDefinition.CacheXslTimeOut.Value;

            // item limit
            if (typedDefinition.ItemLimit.HasValue)
                typedWebpart.ItemLimit = typedDefinition.ItemLimit.Value;

            // mappings
            if (!string.IsNullOrEmpty(typedDefinition.DataMappings))
                typedWebpart.DataMappings = typedDefinition.DataMappings;

            if (!string.IsNullOrEmpty(typedDefinition.DataMappingViewFields))
                typedWebpart.DataMappingViewFields = typedDefinition.DataMappingViewFields;

            // misc
            if (typedDefinition.ShowUntargetedItems.HasValue)
                typedWebpart.ShowUntargetedItems = typedDefinition.ShowUntargetedItems.Value;

            if (typedDefinition.PlayMediaInBrowser.HasValue)
                typedWebpart.PlayMediaInBrowser = typedDefinition.PlayMediaInBrowser.Value;

            if (typedDefinition.UseCopyUtil.HasValue)
                typedWebpart.UseCopyUtil = typedDefinition.UseCopyUtil.Value;

            // FilterTypeXXX
            if (!string.IsNullOrEmpty(typedDefinition.FilterType1))
                typedWebpart.FilterType1 = typedDefinition.FilterType1;

            if (!string.IsNullOrEmpty(typedDefinition.FilterType2))
                typedWebpart.FilterType2 = typedDefinition.FilterType2;

            if (!string.IsNullOrEmpty(typedDefinition.FilterType3))
                typedWebpart.FilterType3 = typedDefinition.FilterType3;

            // FilterFieldXXX
            if (!string.IsNullOrEmpty(typedDefinition.FilterField1))
                typedWebpart.FilterField1 = typedDefinition.FilterField1;

            if (!string.IsNullOrEmpty(typedDefinition.FilterField2))
                typedWebpart.FilterField2 = typedDefinition.FilterField2;

            if (!string.IsNullOrEmpty(typedDefinition.FilterField3))
                typedWebpart.FilterField3 = typedDefinition.FilterField3;

            // FilterXXXIsCustomValue
            if (typedDefinition.Filter1IsCustomValue.HasValue)
                typedWebpart.Filter1IsCustomValue = typedDefinition.Filter1IsCustomValue.Value;

            if (typedDefinition.Filter2IsCustomValue.HasValue)
                typedWebpart.Filter2IsCustomValue = typedDefinition.Filter2IsCustomValue.Value;

            if (typedDefinition.Filter3IsCustomValue.HasValue)
                typedWebpart.Filter3IsCustomValue = typedDefinition.Filter3IsCustomValue.Value;

            // FilterValueXXX
            if (!string.IsNullOrEmpty(typedDefinition.FilterValue1))
                typedWebpart.FilterValue1 = typedDefinition.FilterValue1;

            if (!string.IsNullOrEmpty(typedDefinition.FilterValue2))
                typedWebpart.FilterValue2 = typedDefinition.FilterValue2;

            if (!string.IsNullOrEmpty(typedDefinition.FilterValue3))
                typedWebpart.FilterValue3 = typedDefinition.FilterValue3;

            if (!string.IsNullOrEmpty(typedDefinition.Filter1ChainingOperator))
            {
                typedWebpart.Filter1ChainingOperator = (ContentByQueryWebPart.FilterChainingOperator)
                   Enum.Parse(typeof(ContentByQueryWebPart.FilterChainingOperator), typedDefinition.Filter1ChainingOperator);
            }

            if (!string.IsNullOrEmpty(typedDefinition.Filter2ChainingOperator))
            {
                typedWebpart.Filter2ChainingOperator = (ContentByQueryWebPart.FilterChainingOperator)
                   Enum.Parse(typeof(ContentByQueryWebPart.FilterChainingOperator), typedDefinition.Filter2ChainingOperator);
            }

            // sorting
            if (!string.IsNullOrEmpty(typedDefinition.SortBy))
                typedWebpart.SortBy = typedDefinition.SortBy;

            if (!string.IsNullOrEmpty(typedDefinition.SortByDirection))
                typedWebpart.SortByDirection = (ContentByQueryWebPart.SortDirection)
                    Enum.Parse(typeof(ContentByQueryWebPart.SortDirection), typedDefinition.SortByDirection);

            if (!string.IsNullOrEmpty(typedDefinition.SortByFieldType))
                typedWebpart.SortByFieldType = typedDefinition.SortByFieldType;

            if (!string.IsNullOrEmpty(typedDefinition.GroupByDirection))
            {
                typedWebpart.GroupByDirection = (ContentByQueryWebPart.SortDirection)
                    Enum.Parse(typeof(ContentByQueryWebPart.SortDirection), typedDefinition.GroupByDirection);
            }

            // FilterOperatorXXX
            if (!string.IsNullOrEmpty(typedDefinition.FilterOperator1))
            {
                typedWebpart.FilterOperator1 = (ContentByQueryWebPart.FilterFieldQueryOperator)
                    Enum.Parse(typeof(ContentByQueryWebPart.FilterFieldQueryOperator), typedDefinition.FilterOperator1);
            }

            if (!string.IsNullOrEmpty(typedDefinition.FilterOperator2))
            {
                typedWebpart.FilterOperator2 = (ContentByQueryWebPart.FilterFieldQueryOperator)
                    Enum.Parse(typeof(ContentByQueryWebPart.FilterFieldQueryOperator), typedDefinition.FilterOperator2);
            }

            if (!string.IsNullOrEmpty(typedDefinition.FilterOperator3))
            {
                typedWebpart.FilterOperator3 = (ContentByQueryWebPart.FilterFieldQueryOperator)
                    Enum.Parse(typeof(ContentByQueryWebPart.FilterFieldQueryOperator), typedDefinition.FilterOperator3);
            }

            // FilterDisplayValueXXX

            if (!string.IsNullOrEmpty(typedDefinition.FilterDisplayValue1))
                typedWebpart.FilterDisplayValue1 = typedDefinition.FilterDisplayValue1;

            if (!string.IsNullOrEmpty(typedDefinition.FilterDisplayValue2))
                typedWebpart.FilterDisplayValue2 = typedDefinition.FilterDisplayValue2;

            if (!string.IsNullOrEmpty(typedDefinition.FilterDisplayValue3))
                typedWebpart.FilterDisplayValue3 = typedDefinition.FilterDisplayValue3;

            // bindings
            if (typedDefinition.ServerTemplate.HasValue)
                typedWebpart.ServerTemplate = typedDefinition.ServerTemplate.ToString();

            if (!string.IsNullOrEmpty(typedDefinition.ContentTypeName))
                typedWebpart.ContentTypeName = typedDefinition.ContentTypeName;

            if (!string.IsNullOrEmpty(typedDefinition.ContentTypeBeginsWithId))
                typedWebpart.ContentTypeBeginsWithId = typedDefinition.ContentTypeBeginsWithId;

            if (!string.IsNullOrEmpty(typedDefinition.WebUrl))
            {
                // go with OOTB one, no changes are required

                //var webLookup = new LookupFieldModelHandler();

                //using (var targetWeb = webLookup.GetTargetWeb(CurrentHost.HostFile.Web.Site,
                //    typedDefinition.WebUrl,
                //    typedDefinition.WebId))
                //{
                //    typedWebpart.WebUrl = targetWeb.ServerRelativeUrl;
                //}

                typedWebpart.WebUrl = typedDefinition.WebUrl;
            }

            #region list URL

            if (typedDefinition.ListId.HasGuidValue())
            {
                //typedWebpart.ListId = typedDefinition.ListId.Value;
                // fallback to ListGuid

                typedWebpart.ListGuid = typedDefinition.ListId.Value.ToString("D");
            }

            if (typedDefinition.ListGuid.HasGuidValue())
            {
                typedWebpart.ListGuid = typedDefinition.ListGuid.Value.ToString("D");
            }

            if (!string.IsNullOrEmpty(typedDefinition.ListName))
            {
                // lookup from the target web
                // fallback to ListGuid

                var webLookup = new LookupFieldModelHandler();

                using (var targetWeb = webLookup.GetTargetWeb(CurrentHost.HostFile.Web.Site,
                    typedDefinition.WebUrl,
                    typedDefinition.WebId))
                {
                    var list = targetWeb.Lists[typedDefinition.ListName];
                    typedWebpart.ListGuid = list.ID.ToString("D");
                }
            }

            if (!string.IsNullOrEmpty(typedDefinition.ListUrl))
            {
                // lookup from the target web
                // fallback to ListGuid

                var webLookup = new LookupFieldModelHandler();

                using (var targetWeb = webLookup.GetTargetWeb(CurrentHost.HostFile.Web.Site,
                    typedDefinition.WebUrl,
                    typedDefinition.WebId))
                {
                    var listUrl = SPUrlUtility.CombineUrl(targetWeb.ServerRelativeUrl, typedDefinition.ListUrl);
                    var list = targetWeb.GetList(listUrl);

                    typedWebpart.ListGuid = list.ID.ToString("D");
                }
            }

            #endregion

            // overrides
            if (!string.IsNullOrEmpty(typedDefinition.ListsOverride))
                typedWebpart.ListsOverride = typedDefinition.ListsOverride;

            if (!string.IsNullOrEmpty(typedDefinition.ViewFieldsOverride))
                typedWebpart.ViewFieldsOverride = typedDefinition.ViewFieldsOverride;

            if (!string.IsNullOrEmpty(typedDefinition.QueryOverride))
                typedWebpart.QueryOverride = typedDefinition.QueryOverride;

            if (!string.IsNullOrEmpty(typedDefinition.CommonViewFields))
                typedWebpart.CommonViewFields = typedDefinition.CommonViewFields;

            if (typedDefinition.FilterByAudience.HasValue)
                typedWebpart.FilterByAudience = typedDefinition.FilterByAudience.Value;
        }
        protected override void OnAfterDeployModel(WebpartPageModelHost host, WebPartDefinition definition)
        {
            var typedDefinition = definition.WithAssertAndCast<XsltListViewWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            if (!string.IsNullOrEmpty(typedDefinition.Toolbar))
            {
                var existingWebPart = host.SPLimitedWebPartManager
                    .WebParts
                    .OfType<System.Web.UI.WebControls.WebParts.WebPart>()
                    .FirstOrDefault(wp => !string.IsNullOrEmpty(wp.ID) &&
                                          wp.ID.ToUpper() == definition.Id.ToUpper());

                if (existingWebPart != null)
                {
                    // patching up the view -> ToolbarType
                    var xsltWebPart = existingWebPart as XsltListViewWebPart;

                    if (xsltWebPart != null)
                    {

                        // big TODO for .NET 35
                        // xsltWebPart.View does not exist for .NET 35
                        // the implementation will be done upon the community demand

#if !NET35

                        var targetView = xsltWebPart.View;

                        // fixing up the Toolbar
                        if (!string.IsNullOrEmpty(typedDefinition.Toolbar))
                        {
                            var htmlSchemaXml = XDocument.Parse(targetView.HtmlSchemaXml);

                            var useShowAlwaysValue =
                                (typedDefinition.Toolbar.ToUpper() == BuiltInToolbarType.Standard.ToUpper())
                                && typedDefinition.ToolbarShowAlways.HasValue
                                && typedDefinition.ToolbarShowAlways.Value;

                            var toolbarNode = htmlSchemaXml.Root
                                .Descendants("Toolbar")
                                .FirstOrDefault();

                            if (toolbarNode == null)
                            {
                                toolbarNode = new XElement("Toolbar");
                                htmlSchemaXml.Root.Add(toolbarNode);
                            }

                            toolbarNode.SetAttributeValue("Type", typedDefinition.Toolbar);

                            if (useShowAlwaysValue)
                            {
                                toolbarNode.SetAttributeValue("ShowAlways", "TRUE");
                            }
                            else
                            {
                                XAttribute attr = toolbarNode.Attribute("ShowAlways");
                                if (attr != null && string.IsNullOrEmpty(attr.Value))
                                    attr.Remove();
                            }

                            var field = targetView.GetType()
                                                  .GetProperty("ListViewXml",
                                                     BindingFlags.NonPublic | BindingFlags.Instance);

                            if (field != null)
                            {
                                field.SetValue(targetView, htmlSchemaXml.Root.GetInnerXmlAsString(), null);
                            }
                        }

                        targetView.Update();

#endif
                    }
                }
            }
        }
        protected override void OnAfterDeployModel(WebpartPageModelHost host, WebPartDefinition definition)
        {
            var typedDefinition = definition.WithAssertAndCast<ListViewWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            if (!string.IsNullOrEmpty(typedDefinition.Toolbar))
            {
                var existingWebPart = host.SPLimitedWebPartManager
                    .WebParts
                    .OfType<System.Web.UI.WebControls.WebParts.WebPart>()
                    .FirstOrDefault(wp => !string.IsNullOrEmpty(wp.ID) &&
                                          wp.ID.ToUpper() == definition.Id.ToUpper());

                if (existingWebPart != null)
                {
                    // patching up the view -> ToolbarType
                    var xsltWebPart = existingWebPart as ListViewWebPart;

                    if (xsltWebPart != null)
                    {
                        if (!string.IsNullOrEmpty(xsltWebPart.ViewGuid))
                        {
                            var targetWeb = new LookupFieldModelHandler().GetTargetWeb(
                                            host.PageListItem.Web.Site,
                                            typedDefinition.WebUrl,
                                            typedDefinition.WebId);

                            var list = XsltListViewWebPartModelHandler.GetTargetList(targetWeb,
                                            typedDefinition.ListTitle,
                                            typedDefinition.ListUrl,
                                            typedDefinition.ListId);

                            var targetView = list.Views[ConvertUtils.ToGuid(xsltWebPart.ViewGuid).Value];

                            // fixing up the Toolbar
                            if (!string.IsNullOrEmpty(typedDefinition.Toolbar))
                            {
                                var htmlSchemaXml = XDocument.Parse(targetView.HtmlSchemaXml);

                                var useShowAlwaysValue =
                                    (typedDefinition.Toolbar.ToUpper() == BuiltInToolbarType.Standard.ToUpper())
                                    && typedDefinition.ToolbarShowAlways.HasValue
                                    && typedDefinition.ToolbarShowAlways.Value;

                                var toolbarNode = htmlSchemaXml.Root
                                    .Descendants("Toolbar")
                                    .FirstOrDefault();

                                if (toolbarNode == null)
                                {
                                    toolbarNode = new XElement("Toolbar");
                                    htmlSchemaXml.Root.Add(toolbarNode);
                                }

                                toolbarNode.SetAttributeValue("Type", typedDefinition.Toolbar);

                                if (useShowAlwaysValue)
                                {
                                    toolbarNode.SetAttributeValue("ShowAlways", "TRUE");
                                }
                                else
                                {
                                    XAttribute attr = toolbarNode.Attribute("ShowAlways");
                                    if (attr != null && string.IsNullOrEmpty(attr.Value))
                                        attr.Remove();
                                }

                                var field = targetView.GetType()
                                    .GetProperty("ListViewXml",
                                        BindingFlags.NonPublic | BindingFlags.Instance);

                                if (field != null)
                                {
                                    field.SetValue(targetView, htmlSchemaXml.Root.GetInnerXmlAsString(), null);
                                }
                            }

                            targetView.Update();
                        }
                    }
                }
            }
        }
 protected override void OnBeforeDeployModel(WebpartPageModelHost host, WebPartDefinition webpartModel)
 {
     var typedModel = webpartModel.WithAssertAndCast<BlogMonthQuickLaunchDefinition>("webpartModel", value => value.RequireNotNull());
     typedModel.WebpartType = typeof(BlogMonthQuickLaunch).AssemblyQualifiedName;
 }