首页 > 代码库 > SharePoint 2013 扩展 TilesViewWebPart

SharePoint 2013 扩展 TilesViewWebPart

SharePoint 2013 增加了一个新的比较实用的 web part, 它就是TilesViewWebPart.

在新建一个Team site 后就可以在首页上看见它了:


它也可以新建议一个Promoted Link 列表作为数据源


但是如果要使用自定义的数据源怎么办呢? 

答案是继承TilesViewWebPart类,并重写 GetTiles方法:

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.WebPartPages;
using Microsoft.SharePoint.Utilities;
using System.Collections.Generic;

namespace TilesView.MyTilesWebPart
{
    [ToolboxItemAttribute(false)]
    public class MyTilesWebPart : TilesViewWebPart
    {
        private readonly string layoutsPath = "/" + SPUtility.ContextLayoutsFolder + "/TilesView/";
        private readonly string tilesTitle = "My First Custom TilesWebPart";

        public MyTilesWebPart()
        {
            BaseViewID = ((int)TilesBaseViewID.TileView).ToString();
        }

        protected override TileData[] GetTiles()
        {
            List<TileData> myTiles = new List<TileData>();
            myTiles.Add(new TileData
            {
                Title = "Google Search",
                Description = "Google Search is the search service provided by Google",
                LaunchBehavior = TileLaunchBehavior.Dialog,
                LinkLocation = "http://www.google.com",
                ID = 10,
                BackgroundImageLocation = layoutsPath + "google.jpg",
                TileOrder = 0
            });
            myTiles.Add(new TileData
            {
                Title = "Baidu search",
                Description = "Baidu search is the best Chinese Search",
                LaunchBehavior = TileLaunchBehavior.Dialog,
                LinkLocation = "http://www.baidu.com",
                ID = 20,
                BackgroundImageLocation = layoutsPath + "Baidu.jpg",
                TileOrder = 1
            });
            myTiles.Add(new TileData
            {
                Title = "Bing Search",
                Description = "Bing Search is a search service which provided by Microsoft",
                LaunchBehavior = TileLaunchBehavior.Dialog,
                LinkLocation = "http://www.bing.com",
                ID = 30,
                BackgroundImageLocation = layoutsPath + "bing.jpg",
                TileOrder = 2,
            });
            return myTiles.ToArray();
        }

        protected override string ViewTitle
        {
            get { return tilesTitle; }
        }      
    }
}

扩展的结果如下: