zoukankan      html  css  js  c++  java
  • 继承并自定制SharePoint中的Content Query Web Part (CQWP)

    1:CQWP 是什么, 作用是什么

    2:将Site 以及 Site Collection 的Publishing Feature 打开才能看到

    3:在页面中应用sharepoint自带的content query web part,之后Edit 其中的Query以及Filter 条件,然后Apply查看结果

    4:将含有Query 和 Filter 条件的WebPart 导出,会得到webpart此时的所有 property

    5:在VS中添加自己的 Visual WebPart 让其继承CQWP, 然后将导出来的Property 添加到对应的 .WebPart 文件中(这样完成了属性的自定义)

    6:之后根据 ItemStyle.xsl 完全自定义一份自己的ItemStyle.xsl(后续简称 MyItemStyle)

    7:property中的ItemXslLink 制定我们用哪个ItemStyle文件,再次填写默认值,指定到我们的 MyItemStyle

    8:在MyItemStyle可以自定义css,也可以自定制那些元素可以显示在页面上 (这样完成了ItemStyle的自定义)

    9:在VS中创建一个Module然后将 MyItemStyle 部署到SharePoint中指定位置【注:可以将MyItemStyle的Build action 设置为none,以保证build通过】

    10:9要依赖于我们Feature的部署,在Feature中将9中的Module加上

    11:还可以 Custom Toolpart properties for CQWP【注意properties的持久化问题】

    如下代码:

    EricSunArticlesQueryWebPart.cs :

    using System;
    using System.ComponentModel;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.Publishing.WebControls;
    using Microsoft.SharePoint.WebPartPages;
    using System.Collections.Generic;
    
    namespace EricSunSharePointProject.WebParts.EricSunArticlesQueryWebPart
    {
        [ToolboxItemAttribute(false)]
        public class EricSunArticlesQueryWebPart : ContentByQueryWebPart
        {
            private string articleCategory = string.Empty;
    
            [WebBrowsable(true),
             Personalizable(PersonalizationScope.Shared),
             WebPartStorage(Storage.Personal),
             WebDisplayName("User Category Field"),
             WebDescription("Enter the Category name which the CQWP will filter on"),
             SPWebCategoryName("CategoryConfigureQuery")]
            public string ArticleCategory
            {
                get
                {
                    return articleCategory;
                }
                set
                {
                    articleCategory = value;
                }
            }
    
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
            }
    
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
            }
    
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
            }
    
            public override ToolPart[] GetToolParts()
            {
                List<ToolPart> _toolParts = new List<ToolPart>(base.GetToolParts());
                _toolParts.Insert(0, new EricSunExtendedCQWPToolPart(ArticleCategory));
                return _toolParts.ToArray();
            }
    
            /// <summary>
            /// Change web part properties
    
            /// </summary>
            public void ApplyChanges()
            {
                this.Title = string.Format("{0}", ArticleCategory);
                this.TitleUrl = "/Pages/CategoryLanding.aspx?Title=" + this.Title;
                this.FilterOperator1 = FilterFieldQueryOperator.Contains;
                this.FilterDisplayValue1 = ArticleCategory;
                this.FilterValue1 = ArticleCategory;
                this.FilterField1 = "EricSunCategoryString";
            }
    
            protected override void Render(HtmlTextWriter writer)
            {
                string articlesCount = string.Empty;
                if (!string.IsNullOrEmpty(ArticleCategory))
                {
                    articlesCount = " ( " + GetArticlesCount(ArticleCategory).ToString() + " ) ";
                    writer.Write("<div><a href=/Pages/CategoryLanding.aspx?Title=" + HttpUtility.UrlPathEncode(ArticleCategory) + ">" + ArticleCategory + "</a><span style=\"font-family:Arial; font-size:18px; font-weight:normal; color:#666666\">" + articlesCount + "</span></div>");
                }
                base.Render(writer);
            }
    
            /// <summary>
            /// Get articles count
            /// </summary>
            /// <returns></returns>
            private int GetArticlesCount(string category)
            {
                int articlesCount = 0;
                SPSite currentSite = null;
                SPWeb currentWeb = null;
                try
                {
                    if (!string.IsNullOrEmpty(category))
                    {
                        currentSite = SPContext.Current.Site;
                        currentWeb = currentSite.RootWeb;
                        SPList articlesList = currentWeb.GetList("/Lists/EricSunArticlesList");
                        for (int i = 0; i < articlesList.ItemCount; i++)
                        {
                            if (articlesList.Items[i]["EricSunCategoryString"].ToString().Contains(category) && articlesList.Items[i]["EricSunStatus"].ToString().Equals("Published", StringComparison.OrdinalIgnoreCase))
                            {
                                articlesCount++;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                }
                return articlesCount;
            }
        }
    }

    EricSunExtendedCQWPToolPart .cs

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebPartPages;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.UI.WebControls;
    
    namespace EricSunSharePointProject.WebParts.EricSunArticlesQueryWebPart
    {
        public class EricSunExtendedCQWPToolPart : ToolPart
        {
            public Literal Ltrl { get; set; }
            public DropDownList CategoryDdl { get; set; }
    
            public EricSunExtendedCQWPToolPart(string _ddlValue)
            {
                List<string> categories = GetArticleCategoryNames();
                CategoryDdl = new DropDownList();
                CategoryDdl.Items.Add(new ListItem("Select a category for the web part", ""));
                foreach (string category in categories)
                {
                    CategoryDdl.Items.Add(new ListItem(category, category));
                }
    
                if (!string.IsNullOrEmpty(_ddlValue))
                {
                    CategoryDdl.SelectedValue = _ddlValue;
                }
            }
    
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
    
                this.Title = "Extended Settings";
    
                Ltrl = new Literal();
                Ltrl.Text = "<b>Category Selection</b><br/>";
    
                this.Controls.Add(Ltrl);
                this.Controls.Add(CategoryDdl);
    
            }
    
            /// <summary>
            /// Apply Changes
            /// </summary>
            public override void ApplyChanges()
            {
                EricSunArticlesQueryWebPart _parentWebPart = (EricSunArticlesQueryWebPart)this.ParentToolPane.SelectedWebPart;
                if (_parentWebPart != null && CategoryDdl.SelectedIndex > 0)
                {
                    _parentWebPart.ArticleCategory = CategoryDdl.SelectedValue;
    
                    _parentWebPart.ApplyChanges();
                }
                base.ApplyChanges();
            }
    
            /// <summary>
            /// Get article category names
            /// </summary>
            /// <returns></returns>
            private List<string> GetArticleCategoryNames()
            {
                List<string> categoryNameList = new List<string>();
                SPSite currentSite = null;
                SPWeb currentWeb = null;
                try
                {
                    currentSite = SPContext.Current.Site;
                    currentWeb = currentSite.RootWeb;
                    SPList categoryList = currentWeb.GetList("/Lists/EricSunCategoriesList");
                    for (int i = 0; i < categoryList.ItemCount; i++)
                    {
                        categoryNameList.Add(categoryList.Items[i].Name);
                    }
                }
                catch (Exception ex)
                {
                }
                return categoryNameList;
            }
        }
    }


    Elements.xml

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/" >
      <Module Name="EricSunArticlesQueryWebPart" List="113" Url="_catalogs/wp">
        <File Path="EricSunArticlesQueryWebPart\EricSunArticlesQueryWebPart.webpart" Url="EricSunSharePointProject_EricSunArticlesQueryWebPart.webpart" Type="GhostableInLibrary">
          <Property Name="Group" Value="Custom" />
        </File>
      </Module>
    </Elements>

    EricSunArticlesQueryWebPart.webpart

    <?xml version="1.0" encoding="utf-8"?>
    <webParts>
      <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
        <metaData>
          <type name="EricSunSharePointProject.WebParts.EricSunArticlesQueryWebPart.EricSunArticlesQueryWebPart, $SharePoint.Project.AssemblyFullName$" />
          <importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>
        </metaData>
        <data>
          <properties>
            <property name="Title" type="string">EricSun Articles Query WebPart</property>
            <property name="Description" type="string">My Web Part</property>
          </properties>
        </data>
      </webPart>
    </webParts>

    非常感谢如下文章的支持:

    http://sharepoint-snippets.com/using-content-query-web-part-cqwp/

    http://blog.csdn.net/crazysharepoint/article/details/6183541

    http://blog.csdn.net/crazysharepoint/article/details/6308669

    https://maulikdhorajia.blogspot.com/2012/01/sharepoint-custom-toolpart-properties.html

    http://msdn.microsoft.com/en-us/library/aa981241.aspx **********

    http://sharepoint.infoyen.com/2012/03/14/custom-content-query-webpart/ 

  • 相关阅读:
    清空DB
    C#生成PDF
    C#程序打包发布
    用C#实现生成PDF文档的方法
    SCOPE_IDENTITY、IDENT_CURRENT 和 @@IDENTITY的比较
    如何在DataGridView中实现下拉列表可变的联动
    TreeList控件实现数据过滤功能
    SQL函数大全
    远程链接调用sql脚本
    gb2312简繁转换js兼容各种浏览器
  • 原文地址:https://www.cnblogs.com/mingmingruyuedlut/p/2875550.html
Copyright © 2011-2022 走看看