XsltListViewWebPart in Sharepoint 2007   

The DataFormWebPart is hard to work with (but extremely flexible). When customizing the UI is not necessary the DataFormWebPart is too cumbersome. I came up with a simple user control that allows you to register an inline view inside of it.

The control is implemented with a Toolbar and a ListViewWebPart. We bind both controls during the load event by setting the list name of both controls and setting the view. In this example we are also setting a filter by using a query string parameter.

The result is a control that behaves like the XsltListViewWebPart from 2010 (not as rich of course) and can be used as the starting point for a flexible alternative to both the DataFormWebPart and ListViewWebPart.
 

 Sample




  
    
      
    
    
      
        
		{0}
      
    
  
  
    
    
    
    
  
  100
  


		

 Web Control Markup

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CustomListView.ascx.cs"
    Inherits="n.SharePoint.WebControls.CustomListView, n.SharePoint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c17764ad0f7561ef" %>


 Code Snippet

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Xml;
using System.Reflection;
using Microsoft.SharePoint.WebControls;

namespace n.SharePoint.WebControls
{
    [ParseChildren(ChildrenAsProperties = true)]
    public partial class CustomListView : System.Web.UI.UserControl, INamingContainer
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            CreateToolbar();

            DataBindListView();
        }

        private void CreateToolbar()
        {
            try
            {
                SPList list = SPContext.Current.Web.Lists[ListName];

                SPContext context = SPContext.GetContext(this.Context, list.Views[ViewName].ID, 
                    list.ID, SPContext.Current.Web); 
                
                toolbar.RenderContext = context; 
            }
            catch (Exception ex)
            {
                Trace.Write(ex.Message);
            }
        }

        
        string listName = string.Empty;

        public string ListName
        {
            get { return listName; }
            set { listName = value; }
        }

        string viewName = string.Empty;

        public string ViewName
        {
            get { return viewName; }
            set { viewName = value; }
        }

        string viewXml = string.Empty;

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public string ViewXml
        {
            get { return viewXml; }
            set { viewXml = value; }
        }        

        private void DataBindListView()
        {
            SPWeb oWebsite = SPContext.Current.Web;

            SPList oList = oWebsite.Lists[ListName];

            oListViewWP.ListName = oList.ID.ToString("B").ToUpper();
            
            string viewXml = string.Empty;

            string where = string.Empty;

            SPView spView = null;           

            try
            {
                spView = oList.Views[this.ViewName];

                if (String.IsNullOrEmpty(this.ViewXml))
                {
                    oListViewWP.ListViewXml = Resources.Views.UserPayments;// spView.HtmlSchemaXml;
                }
                else
                    oListViewWP.ListViewXml = this.ViewXml;
                
                string memberId = Request.QueryString["ID"].ToString();

                oListViewWP.ListViewXml =oListViewWP.ListViewXml.Replace("{0}" , memberId);
            }
            catch 
            {
                spView= oList.DefaultView; // Code.ItemDiscussion.Resources.Views.SubjectView;   
            }
            
            oListViewWP.GetDesignTimeHtml();
        }
    }
}
Published  on  2/19/2010  by  xsolon
  Comments  |  Trackback Url  | 0  Links to this post | Bookmark this post with:          
Tags: ListViewWebPart, xsl | Categories: SharePoint
Technorati Tags:
 

Links to this post

Comments

Title:
Receieve Notification:
Website:
Email:
Comments:

CAPTCHA Image Validation