0

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.

1

1 Answer 1

1

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();
}
4
  • 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 Commented Jun 19, 2014 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. Commented Jun 19, 2014 at 8:45
  • It's not working for Sharepoint-Online Sandbox Solution. Commented Feb 18, 2015 at 4:34
  • Does work with SharePoint 2013 Commented Mar 26, 2015 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.