Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I tried create and render CustomListViewWebPart . I tried with the following code.

public class CustomListViewWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
    #region Variable Declaration
    private string strlisttolink = string.Empty;
    private string strViewOfSourceList = string.Empty;
    private string strQuery = string.Empty;

    #endregion
    #region Properties

    //Get and Set Property of the Source List
    [Personalizable(true),
     WebBrowsable(),
     WebDisplayName("List Name"),
     WebDescription("Pass the Name of the List")]
    //Get and Set Property of the Source List
    public string ListToLink
    {
        get
        {
            return strlisttolink;
        }
        set
        {
            strlisttolink = value;
        }
    }

    [Personalizable(true),
     WebBrowsable(),
     WebDisplayName("View"),
     WebDescription("Pass Name of the View that you will like to apply to the List")]
    //Get and Set Property of the Source List
    public string ViewOfSourceList
    {
        get
        {
            return strViewOfSourceList;
        }
        set
        {
            strViewOfSourceList = value;
        }
    }

    [Personalizable(true),
     WebBrowsable(),
     WebDisplayName("Query"),
     WebDescription("Pass the Filter Query")]
    //Get and Set Property of the Source List
    public string FilterQuery
    {
        get
        {
            return strQuery;
        }
        set
        {
            strQuery = value;
        }
    }

    #endregion

    public CustomListViewWebPart()
    {
        this.ExportMode = WebPartExportMode.All;
    }



    protected override void CreateChildControls()
    {
        base.CreateChildControls();

        try
        {

            SPWeb web = SPContext.Current.Web;
            SPList list = web.Lists[ListToLink];

            ViewToolBar toolbar = new ViewToolBar();
            SPContext context = SPContext.GetContext(this.Context, list.Views[ViewOfSourceList].ID, list.ID, SPContext.Current.Web);
            toolbar.RenderContext = context;
            Controls.Add(toolbar);

            // Instantiate the web part
            ListViewWebPart lvwp = new ListViewWebPart();
            lvwp.ListName = list.ID.ToString("B").ToUpper();
            lvwp.ViewGuid = list.Views[ViewOfSourceList].ID.ToString("B").ToUpper();

            SPView webPartView = web.Lists[ListToLink].Views[ViewOfSourceList];
            SPList objList = web.Lists[ListToLink];
            webPartView.Query = FilterQuery;

            // Remove the Toolbar!
            // First Option: Do it throught OOTB.This needs to be done from the VIEW of the Web Part
            // Second Option: Do it trhough Coding.This line is required to ensure that all the appropriate internal nodes of the SPView are populated

            String temp = webPartView.SchemaXml;
            System.Reflection.PropertyInfo ViewProp = lvwp.GetType().GetProperty("View", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            SPView spView = ViewProp.GetValue(lvwp, null) as SPView;
            // This forces a refresh of the views internal xml or the node's cild nodes are not populated


            PropertyInfo nodeProp = webPartView.GetType().GetProperty("Node", BindingFlags.NonPublic | BindingFlags.Instance);
            XmlNode node = nodeProp.GetValue(webPartView, null) as XmlNode;

            // Now get the Toolbar node from the view so we can update its type property
            XmlNode toolbarNode = node.SelectSingleNode("Toolbar");
            if (toolbarNode != null)
            {
                toolbarNode.Attributes["Type"].Value = "None";
                web.AllowUnsafeUpdates = true;
                webPartView.Update();
                web.AllowUnsafeUpdates = false;
            }
            //End Remove the Toolbar!


            web.AllowUnsafeUpdates = true;
            webPartView.Update();
            objList.Update();
            web.AllowUnsafeUpdates = false;
            lvwp.GetDesignTimeHtml();
            this.Controls.Add(lvwp);
        }
        catch (Exception ex)
        {
            Label lbl = new Label();
            lbl.Text = "Error occured: ";
            lbl.Text += ex.Message;
            this.Controls.Add(lbl);
        }
    }


    protected override void Render(HtmlTextWriter writer)
    {
        EnsureChildControls();
        base.Render(writer);
    }
}

But with this code, Items don't show.

share|improve this question
    
Have you tried debugging? Does the code fail to execute any line? Do you get any errors? What are you trying to achieve with this that you can't with the OOTB ListView? –  James Love Jan 14 '12 at 10:34
    
the code doesn't fail . when i add WebPart and select list for display , don't show any item of list . –  ar.gorgin Jan 14 '12 at 11:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.