Beispiel #1
0
        protected override void OnBeforeDeploy(ListItemModelHost host, WebPartDefinitionBase webpart)
        {
            base.OnBeforeDeploy(host, webpart);

            // pre-load web id
            if (!host.HostWeb.IsPropertyAvailable("Id"))
            {
                host.HostWeb.Context.Load(host.HostWeb, w => w.Id);
                host.HostWeb.Context.ExecuteQueryWithTrace();
            }

            var context = host.HostClientContext;
            var wpModel = webpart.WithAssertAndCast <ListViewWebPartDefinition>("model", value => value.RequireNotNull());

            // save the old default view ID, then restore in OnAfterDeploy
            _currentListBindContext = XsltListViewWebPartModelHandler.LookupBindContext(host,
                                                                                        wpModel.WebUrl, wpModel.WebId,
                                                                                        wpModel.ListUrl, wpModel.ListTitle, wpModel.ListId,
                                                                                        wpModel.ViewUrl, wpModel.ViewName, wpModel.ViewId,
                                                                                        wpModel.TitleUrl);

            if (_currentListBindContext.TargetView != null)
            {
                _currentListBindContext.TargetView.DefaultView = true;
                _currentListBindContext.TargetView.Update();

                context.ExecuteQueryWithTrace();
            }
        }
Beispiel #2
0
        protected override void OnAfterDeploy(ListItemModelHost host, WebPartDefinitionBase webpart)
        {
            if (_currentListBindContext != null)
            {
                var context = host.HostClientContext;
                var wpModel = webpart.WithAssertAndCast <ListViewWebPartDefinition>("model", value => value.RequireNotNull());

                var bindingContext = XsltListViewWebPartModelHandler.LookupBindContext(host,
                                                                                       wpModel.WebUrl, wpModel.WebId,
                                                                                       wpModel.ListUrl, wpModel.ListTitle, wpModel.ListId,
                                                                                       wpModel.ViewUrl, wpModel.ViewName, wpModel.ViewId,
                                                                                       wpModel.TitleUrl);

                // reverting back the dafult view
                var view = bindingContext.List.GetView(_currentListBindContext.DefaultViewId);
                view.DefaultView = true;
                view.Update();

                context.ExecuteQueryWithTrace();
            }
        }
Beispiel #3
0
        protected override string GetWebpartXmlDefinition(ListItemModelHost listItemModelHost, WebPartDefinitionBase webPartModel)
        {
            var wpModel = webPartModel.WithAssertAndCast <ListViewWebPartDefinition>("model", value => value.RequireNotNull());

            var bindContext = XsltListViewWebPartModelHandler.LookupBindContext(listItemModelHost,
                                                                                wpModel.WebUrl, wpModel.WebId,
                                                                                wpModel.ListUrl, wpModel.ListTitle, wpModel.ListId,
                                                                                wpModel.ViewUrl, wpModel.ViewName, wpModel.ViewId,
                                                                                wpModel.TitleUrl);

            var webId = listItemModelHost.HostWeb.Id;

            var wpXml = WebpartXmlExtensions.LoadWebpartXmlDocument(BuiltInWebPartTemplates.ListViewWebPart)
                        .SetOrUpdateListVieweWebPartProperty("ListName", bindContext.ListId.ToString("B"))
                        .SetOrUpdateListVieweWebPartProperty("ListId", bindContext.ListId.ToString("D").ToLower())
                        .SetOrUpdateListVieweWebPartProperty("WebId", webId.ToString("D").ToLower())
                        //.SetOrUpdateListVieweWebPartProperty("ViewGuid", bindContext.ViewId.ToString("D").ToLower())
                        .SetTitleUrl(bindContext.TitleUrl)
                        .ToString();

            return(wpXml);
        }
Beispiel #4
0
        protected override void InternalOnAfterWebPartProvision(WebPartProcessingContext provisionContext)
        {
            base.InternalOnAfterWebPartProvision(provisionContext);

            var webPartModel = provisionContext.WebPartDefinition;

            var listItemModelHost = provisionContext.ListItemModelHost;
            var typedDefinition   = webPartModel.WithAssertAndCast <ListViewWebPartDefinition>("model", value => value.RequireNotNull());

            var webPartStoreKey = provisionContext.WebPartStoreKey;
            var context         = provisionContext.ListItemModelHost.HostWeb.Context;

            var bindContext = XsltListViewWebPartModelHandler.LookupBindContext(listItemModelHost,
                                                                                typedDefinition.WebUrl, typedDefinition.WebId,
                                                                                typedDefinition.ListUrl, typedDefinition.ListTitle, typedDefinition.ListId,
                                                                                typedDefinition.ViewUrl, typedDefinition.ViewName, typedDefinition.ViewId,
                                                                                typedDefinition.TitleUrl);

            if (provisionContext.WebPartStoreKey.HasValue &&
                provisionContext.WebPartStoreKey.Value != default(Guid))
            {
                var targetWeb = listItemModelHost.HostWeb;

                if (typedDefinition.WebId.HasGuidValue() || !string.IsNullOrEmpty(typedDefinition.WebUrl))
                {
                    targetWeb = new LookupFieldModelHandler()
                                .GetTargetWeb(this.CurrentClientContext.Site, typedDefinition.WebUrl, typedDefinition.WebId);
                }

                var list       = XsltListViewWebPartModelHandler.LookupList(targetWeb, typedDefinition.ListUrl, typedDefinition.ListTitle, typedDefinition.ListId);
                var hiddenView = list.Views.GetById(provisionContext.WebPartStoreKey.Value);

                context.Load(hiddenView, s => s.HtmlSchemaXml);

                context.Load(hiddenView);
                context.ExecuteQueryWithTrace();

                // patching the toolbar value

                if (!string.IsNullOrEmpty(typedDefinition.Toolbar))
                {
                    var htmlSchemaXml = XDocument.Parse(hiddenView.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();
                        }
                    }

#if !NET35
                    hiddenView.ListViewXml = htmlSchemaXml.Root.GetInnerXmlAsString();
#endif

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