Following example shows how to add XSLT list view webpart to a webpart page:
Please note: The code to set the toolbar will work in MOSS 2007 and SP 2010 only. It doesn't work in SP 2013.
//Display items with latest modified date
string query = "<OrderBy><FieldRef Name=\"Modified\" Ascending='FALSE'/></OrderBy>";

//Total number of rows to be shown in Lists on homepage
uint rowCount = 20;

//Get Reference of Task List
SPList taskList = oWeb.Lists["Tasks"];
StringCollection relevantColumns = CreateViewColumns();
SPView taskView = taskList.Views.Add("Recent Tasks", relevantColumns, query, rowCount, false, false);
string zoneID = "Zone ID";
int zoneIndex = 1;
AddListsToHomePage(taskList, taskList.Title, zoneID, zoneIndex, taskView);

private StringCollection CreateViewColumns()
{
    StringCollection viewFieldsCollection = new StringCollection();
    string tasksColumns = "ID;Title";
    string[] columns = tasksColumns.Split(';');
    foreach (string column in columns)
    {
        viewFieldsCollection.Add(column);
    }
    return viewFieldsCollection;
}

private void AddListsToHomePage(SPList listToAdd, string title, string zoneID, int zoneIndex, SPView view)
{
    XsltListViewWebPart lvwp = new XsltListViewWebPart();
    lvwp.ListName = listToAdd.ID.ToString("B").ToUpper();
    lvwp.ViewGuid = view.ID.ToString("B").ToUpper();
    lvwp.Title = title;
    using (SPLimitedWebPartManager webpartManager = web.GetLimitedWebPartManager(web.Url + "default.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
    {
        webpartManager.AddWebPart(lvwp, zoneID, zoneIndex);
        webpartManager.Web.Dispose();
    }

    //Set toolbar type to Freeform
    MethodInfo ensureViewMethod = lvwp.GetType().GetMethod("EnsureView", BindingFlags.Instance | BindingFlags.NonPublic);
    object[] ensureViewParams = { };
    ensureViewMethod.Invoke(lvwp, ensureViewParams);
    FieldInfo viewFieldInfo = lvwp.GetType().GetField("view", BindingFlags.NonPublic | BindingFlags.Instance);
    SPView spView = viewFieldInfo.GetValue(lvwp) as SPView;
    Type[] toolbarMethodParamTypes = { Type.GetType("System.String") };
    MethodInfo setToolbarTypeMethod = spView.GetType().GetMethod("SetToolbarType", BindingFlags.Instance | BindingFlags.NonPublic, null, toolbarMethodParamTypes, null);
    object[] setToolbarParam = { "Freeform" };
    setToolbarTypeMethod.Invoke(spView, setToolbarParam);

    //Set tabular view to false, so that bulk editing is not allowed
    spView.TabularView = false;
    spView.Update();
    //Update the default view
    SPView defaultView = listToAdd.DefaultView;

    //Total number of rows to be shown in Deafault view of Lists
    uint rowCount = 100;
    defaultView.RowLimit = rowCount;
    defaultView.Paged = true;
    defaultView.Update();
}


2 comments

  1. Sunil Kumar // 14 December 2014 at 23:39  

    MethodInfo ensureViewMethod = lvwp.GetType().GetMethod("EnsureView", BindingFlags.Instance | BindingFlags.NonPublic);
    object[] ensureViewParams = { };
    ensureViewMethod.Invoke(lvwp, ensureViewParams);


    The third line is giving error "as null reference" in SharePoint 2013 environment, due to this i am unable to execute the complete code.


    I want to set the toolbar type to "Show Toolbar", kindly provide help on this.

  2. Nadeem Yousuf // 15 December 2014 at 23:04  

    Hi Sunil,


    This code will work only in SP 2010.


    Thanks,
    Nadeem

My SharePoint CodePlex Projects

SharePoint 2010 Autocomplete Lookup Field

Export Version History Of SharePoint 2010 List Items to Microsoft Excel.