示例#1
0
 public static void AddXsltViewWebPart(SPWeb web, SPList list, string pageUrl, string webPartName, string zoneID,
                                       int zoneIndex, bool isHidden, string viewTitle)
 {
     using (SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager(
                pageUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
     {
         bool isExists = false;
         foreach (System.Web.UI.WebControls.WebParts.WebPart wp in webPartManager.WebParts)
         {
             if (wp.Title.Equals(webPartName))
             {
                 isExists = true;
                 break;
             }
             else
             {
                 isExists = false;
             }
         }
         if (!isExists)
         {
             XsltListViewWebPart webPart = new XsltListViewWebPart();
             webPart.ListId     = list.ID;
             webPart.Title      = webPartName;
             webPart.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.TitleAndBorder;
             SPView view = list.Views[viewTitle];
             webPart.ViewGuid      = view.ID.ToString();
             webPart.XmlDefinition = view.GetViewXml();
             webPartManager.AddWebPart(webPart, zoneID, zoneIndex);
         }
     }
 }
示例#2
0
        public override WebPart CreateWebPart(SPWeb web, Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager webPartManager)
        {
            SPList list = GetList(web, ListUrl, ListName);

            if (list == null)
            {
                return(null);
            }

            Microsoft.SharePoint.WebPartPages.ListViewWebPart webPart = new Microsoft.SharePoint.WebPartPages.ListViewWebPart();
            webPart.ListId = list.ID;
            webPart.Title  = Title;
            webPart.WebId  = list.ParentWeb.ID;


            //webPart.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.TitleAndBorder;
            SPView view = GetView(list, ViewName);

            webPart.ViewGuid    = view.ID.ToString();
            webPart.ListViewXml = view.GetViewXml();

            webPart.ExportMode = WebPartExportMode.All;
            base.UpdateProperties(webPart);
            return(webPart);
        }
        private void ModifyCAMLFilter(SPView view)
        {
            var xml = new XmlDocument();

            xml.LoadXml(view.GetViewXml());
            if (xml.DocumentElement == null)
            {
                return;
            }

            var query = xml.DocumentElement.SelectSingleNode(CAML.Query);

            if (query != null)
            {
                var where = query.SelectSingleNode(CAML.Where);
                if (where == null)
                {
                    where = xml.CreateNode(XmlNodeType.Element, CAML.Where, string.Empty);
                    query.AppendChild(where);
                    var w = CAMLGenerator.JoinFilters(CAMLPredicates, CAML.And);
                    where.InnerXml = w;
                }
                else
                {
                    var whereParts = new List <string> {
                        where.InnerXml
                    };
                    whereParts.AddRange(CAMLPredicates);
                    where.InnerXml = CAMLGenerator.JoinFilters(whereParts, CAML.And);
                }
            }
            view.SetViewXml(xml.OuterXml);

            Debug.WriteLine(xml.OuterXml);
        }
示例#4
0
        /// <summary>
        /// Update list id in a webpart
        /// </summary>
        /// <param name="web">Current web</param>
        /// <param name="pageUrl">Url of page contains web part that you want to change list id</param>
        /// <param name="webPartName">The name of web part that you want to change list id</param>
        /// <param name="list">Updated list</param>
        /// <param name="viewTitle">Updated list view title</param>
        public static void UpdateListId(SPWeb web, string pageUrl, string webPartName, SPList list, string viewTitle)
        {
            using (SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager(
                       pageUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
            {
                foreach (System.Web.UI.WebControls.WebParts.WebPart wp in webPartManager.WebParts)
                {
                    if (wp.Title.Equals(webPartName))
                    {
                        (wp as XsltListViewWebPart).ListId = list.ID;
                        wp.TitleUrl = list.RootFolder.Url;
                        SPView view = list.DefaultView;
                        (wp as XsltListViewWebPart).ViewGuid      = view.ID.ToString();
                        (wp as XsltListViewWebPart).ViewId        = int.Parse(view.BaseViewID);
                        (wp as XsltListViewWebPart).XmlDefinition = view.GetViewXml();

                        webPartManager.SaveChanges(wp);

                        break;
                    }
                }
            }
        }
        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
        }
示例#6
0
        private void AddView()
        {
            XsltListViewWebPart xlvWebPart = null;
            //SPSite  spSite =// new SPSite("http://xqx2012/personal/xueqingxia");
            SPWeb  spWeb    = SPContext.Current.Web;
            SPList currList = spWeb.Lists.TryGetList(webObj.ListMediaLib);//"文档" );
            //GetList(ref spWeb, ref currList);
            SPView view = currList.Views[0];

            foreach (SPView tmpView in currList.Views)
            {
                if (tmpView.Title == "缩略图")
                {
                    view = tmpView;
                    break;
                }
            }
            xlvWebPart            = new XsltListViewWebPart();
            xlvWebPart.ID         = "xlvPics";
            xlvWebPart.Toolbar    = "None";
            xlvWebPart.WebId      = spWeb.ID;
            xlvWebPart.ListId     = currList.ID;
            xlvWebPart.ChromeType = PartChromeType.TitleOnly;
            xlvWebPart.ViewGuid   = view.ID.ToString();
            xlvWebPart.ListName   = currList.ID.ToString();
            //xlvWebPart.Title = currList.Title;
            xlvWebPart.ClientRender  = true;
            xlvWebPart.ListUrl       = currList.RootFolder.Url;
            xlvWebPart.CssClass      = view.CssStyleSheet;
            xlvWebPart.CssStyleSheet = view.CssStyleSheet;
            xlvWebPart.JSLink        = view.JSLink;

            xlvWebPart.AllowClose = false;

            xlvWebPart.AllowConnect = false;

            xlvWebPart.AllowEdit = false;

            xlvWebPart.AllowHide = false;

            xlvWebPart.AllowMinimize = false;

            xlvWebPart.AllowZoneChange = false;

            xlvWebPart.ChromeType = PartChromeType.Default;
            //xlvWebPart.Title = "";
            //xlvWebPart.ViewSelectorFetchAsync = false;
            //xlvWebPart.InplaceSearchEnabled = false;
            //xlvWebPart.ServerRender = false;
            //xlvWebPart.InitialAsyncDataFetch = false;
            //xlvWebPart.PageSize = -1; xlvWebPart.UseSQLDataSourcePaging = true; xlvWebPart.DataSourceID = ""; xlvWebPart.ShowWithSampleData = false; xlvWebPart.AsyncRefresh = false; xlvWebPart.ManualRefresh = false; xlvWebPart.AutoRefresh = false; xlvWebPart.AutoRefreshInterval = 60; xlvWebPart.SuppressWebPartChrome = false; xlvWebPart.ZoneID = "Main"; xlvWebPart.ChromeState = PartChromeState.Normal; xlvWebPart.AllowClose = true; xlvWebPart.AllowZoneChange = true; xlvWebPart.AllowMinimize = true; xlvWebPart.AllowConnect = true; xlvWebPart.AllowEdit = true; xlvWebPart.AllowHide = true; xlvWebPart.Hidden = false;
            xlvWebPart.XmlDefinition     = view.GetViewXml();
            xlvWebPart.ParameterBindings = view.ParameterBindings;

            StringBuilder txt = new StringBuilder();

            txt.AppendLine("<div id = \"MSOZoneCell_WebPartWPQ2\" class=\"ms-webpartzone-cell ms-webpart-cell-vertical ms-fullWidth s4-wpcell\" onkeyup=\"WpKeyUp(event)\" onmouseup=\"WpClick(event)\">");
            txt.AppendLine("<div class=\"ms-webpart-chrome ms-webpart-chrome-vertical ms-webpart-chrome-fullWidth \">");
            //txt.AppendLine(xlvWebPart.ToString ());
            //txt.AppendLine("</div></div>");
            try
            {
                this.Controls.Add(new LiteralControl(txt.ToString()));
                this.Controls.Add(xlvWebPart);
                this.Controls.Add(new LiteralControl("</div></div>"));
                //divRoll.Controls.Add(xlvWebPart);
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.ToString();
            }
        }