Thursday, August 18, 2011

Sharepoint 2010 : Custom ListView WebPart

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

namespace OPSSitePages.Layouts.OPSSitePages
{
    public partial class Test : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            SPSite site = null;
            SPWeb web = null;
            try
            {
                site = new SPSite(SPContext.Current.Site.Url.ToString());
                web = site.OpenWeb();
                web.AllowUnsafeUpdates = true;
                SPList list = web.Lists["Events"];
                ListViewWebPart lvwp = new ListViewWebPart();
                lvwp.ListName = list.ID.ToString("B").ToUpper();
                lvwp.Title = "Custom ListView Webpart";
                lvwp.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.TitleAndBorder;


                SPViewCollection collViews = list.Views;
                bool isTrue = false;

                // checking customview is avilable in the list
                foreach (SPView view in collViews)
                {
                    if (view.Title.Equals("CustomView"))
                    {
                        isTrue = true;
                        break;
                    }
                }

                // delete a view
                if (isTrue)
                {
                    list.Views.Delete(list.Views["CustomView"].ID);
                    isTrue = false;
                }


                if (!isTrue)  //create a custom view if not avilable in the list
                {
                    string strViewName = "CustomView";
                    System.Collections.Specialized.StringCollection collViewFields = new System.Collections.Specialized.StringCollection();
                    collViewFields.Add("LinkTitle");
                    //collViewFields.Add("Location");
                    collViewFields.Add("EventDate");
                    collViewFields.Add("EndDate");
                    collViewFields.Add("OPSWorkspaceLink");

                    string strQuery = "<Where><Gt><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Gt></Where>";
                    collViews.Add(strViewName, collViewFields, strQuery, 50, true, false);
                }


                SPView spView = list.Views["CustomView"];
                lvwp.ViewGuid = spView.ID.ToString("B").ToUpper();

                //hide Listview Webpart toolbar
                spView.GetType().InvokeMember("EnsureFullBlownXmlDocument", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, spView, null, System.Globalization.CultureInfo.CurrentCulture);
                PropertyInfo nodeProp = spView.GetType().GetProperty("Node", BindingFlags.NonPublic | BindingFlags.Instance);
                XmlNode node = nodeProp.GetValue(spView, null) as XmlNode;

                //XmlNode parambindings = node.SelectSingleNode("ParameterBindings");
                //parambindings.ChildNodes[0].Attributes[1].Value = "";
                //parambindings.ChildNodes[1].Attributes[1].Value = "";

                XmlNode toolbarNode = node.SelectSingleNode("Toolbar");
                if (toolbarNode != null)
                {
                    if (!toolbarNode.Attributes["Type"].Value.Equals("Freeform"))
                    {
                        toolbarNode.Attributes["Type"].Value = "Freeform";
                        //toolbarNode.Attributes["ShowAlways"].Value = "";
                        //ShowAlways="TRUE"

                        string toolBarType = "Freeform";

                        if (String.Compare(toolBarType, "Freeform", true, System.Globalization.CultureInfo.InvariantCulture) == 0)
                        {
                            string newItemString = "";
                            XmlAttribute positionNode = toolbarNode.OwnerDocument.CreateAttribute("Position");
                            positionNode.Value = "After";
                            toolbarNode.Attributes.Append(positionNode);
                            switch (spView.ParentList.BaseTemplate)
                            {
                                case SPListTemplateType.Announcements:
                                    newItemString = "announcement";
                                    break;
                                case SPListTemplateType.Events:
                                    newItemString = "event";
                                    break;
                                case SPListTemplateType.Tasks:
                                    newItemString = "task";
                                    break;
                                case SPListTemplateType.DiscussionBoard:
                                    newItemString = "discussion";
                                    break;
                                case SPListTemplateType.Links:
                                    newItemString = "link";
                                    break;
                                case SPListTemplateType.GenericList:
                                    newItemString = "item";
                                    break;
                                case SPListTemplateType.DocumentLibrary:
                                    newItemString = "document";
                                    break;
                                default:
                                    newItemString = "item";
                                    break;
                            }
                            // newItemString = "event";
                            if (spView.ParentList.BaseType == SPBaseType.DocumentLibrary)
                            {
                                newItemString = "document";
                            }

         //no need of below code to show "Add new item", if u r using XSLTListViewWebPart
 
                            // this code userful for (ListViewWebPart) to show "Add new item"
                            toolbarNode.InnerXml = @"<IfHasRights><RightsChoices><RightsGroup PermAddListItems=""required"" /></RightsChoices><Then><HTML><![CDATA[ <table width=100% cellpadding=0 cellspacing=0 border=0 > <tr> <td colspan=""2"" class=""ms-partline""><IMG src=""/_layouts/images/blank.gif"" width=1 height=1 alt=""""></td> </tr> <tr> <td class=""ms-addnew"" style=""padding-bottom: 3px""> <img src=""/_layouts/images/rect.gif"" alt="""">&nbsp;<a class=""ms-addnew"" ID=""idAddNewItem"" href=""]]></HTML><URL Cmd=""New"" /><HTML><![CDATA["" ONCLICK=""javascript:NewItem(']]></HTML><URL Cmd=""New"" /><HTML><![CDATA[', true);javascript:return false;"" target=""_self"">]]></HTML><HTML>Add new " + newItemString + @"</HTML><HTML><![CDATA[</a> </td> </tr> <tr><td><IMG src=""/_layouts/images/blank.gif"" width=1 height=5 alt=""""></td></tr> </table>]]></HTML></Then></IfHasRights>";
                        }

                    }
                }

                spView.Update();
                //list.Update();
                div1.Controls.Add(lvwp);
            }
            finally
            {

                web.Dispose();
                site.Dispose();

            }
        }
    }
}


No comments:

Post a Comment

Followers

Blog Archive