I want to add Xsltlistviewwebpart with some pre difened property(like list,view,zoneid) and also apply xsl to it.Does anyone has any example/code sample to show how can it be achieved.

Thanks for reply.


Here are very good examples to add Xsltlistviewwebpart programmatically.




Sample code:

//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)
    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);

    //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;
    //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;
  • Please do not provide link only answers. Add the details to your post, and use the link as source. If the link breaks, your answer will be of no help to future readers Jun 19 '14 at 8:39
  • Thanks Nadeem and Robert for editing. The code part was quite long that way i avoided that. But in future i will take care of this.
    – Aanchal
    Jun 19 '14 at 8:45
  • It's not working for Sharepoint-Online Sandbox Solution. Feb 18 '15 at 4:34
  • Does work with SharePoint 2013 Mar 26 '15 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.