Question

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.

Was it helpful?

Solution

Here are very good examples to add Xsltlistviewwebpart programmatically.

http://www.c-sharpcorner.com/UploadFile/sagarp/programmatically-adding-xsltlistviewwebpart-inside-panel-in/

http://sharepointnadeem.blogspot.in/2012/08/programatically-add-xsltlist-view.html

http://damneddutch.blogspot.in/2012/08/programmatically-create-sharepoint.html

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)
    {
        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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top