3

I add an XsltListViewWebpart to a page in an event receiver like this:

public void CreateWebPart(SPList documentsList, SPLimitedWebPartManager wpm)
{
    var documentsWP = new XsltListViewWebPart();
    documentsWP.ListId = documentsList.ID
    wpm.AddWebPart(documentsWP, "Left", 0);
}

This works fine, but I want to change it around a bit. I want to edit the columns that are displayed, and I want to remove the little toolbar at the bottom that allows you to add items. My first attempt was this:

public void CreateWebPart(SPList documentsList, SPLimitedWebPartManager wpm)
{
    var documentsWP = new XsltListViewWebPart();
    documentsWP.ListId = documentsList.ID;
    documentsWP.Toolbar = "None"; //this does nothing?
    wpm.AddWebPart(documentsWP, "Left", 0);
}

But that did nothing. After that I tried the following:

public void CreateWebPart(SPList documentsList, SPLimitedWebPartManager wpm)
{
    var documentsWP= new XsltListViewWebPart();
    documentsWP.ListId = documentsList.ID;
    StringBuilder xml = new StringBuilder();
    xml.Append("<View MobileView='TRUE' Type='HTML'>");
    xml.Append("<Query></Query>");
    xml.Append("<ViewFields><FieldRef Name='DocIcon'/><FieldRef Name='LinkFilename'/></ViewFields>");
    xml.Append("<RowLimit Paged='TRUE'>30</RowLimit>");
    xml.Append("<Aggregations Value='Off'/>");
    xml.Append("<Toolbar Type='None'/></View>");
    documentsWP.XmlDefinition = xml.ToString();
    documentsWP.AllowEdit = false;            
    wpm.AddWebPart(documentsWP, "Left", 0);
}

But still the toolbar is shown, and still other fields are shown (the Modifier field). How can I programmatically set the displayed columns? How can I hide the toolbar?

3 Answers 3

4

You can create your own view and decide which fields to show in the view. The method below creates a StringCollection which can be passed to the method while creating view List.Views.Add()

    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;
    }

Also, you can use reflection to set the Toolbar to None

   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 = { "None" };
        setToolbarTypeMethod.Invoke(spView, setToolbarParam);

For full example, please look into here http://sharepointnadeem.blogspot.com/2012/08/programatically-add-xsltlist-view.html

3
  • Just tried the view method and that works perfectly. Also +1 for the link :) Nov 14, 2012 at 16:14
  • This seems to be SP2010 specific and does not work for SharePoint 2013. May 12, 2015 at 16:17
  • @ peter, you are right. It doesn't work in SP2013 May 15, 2015 at 13:47
4

I have created this in an application page, which is working fine. Please refer tto the code below that also has your part of code.

      using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
      {
            using (SPWeb web = site.OpenWeb())
            {
                SPList list = web.Lists[MyList];
                XsltListViewWebPart XsltListViewWebPart1 = new XsltListViewWebPart();
                XsltListViewWebPart1.ListUrl = @"/Lists/MyList";
                XsltListViewWebPart1.ListId = list.ID;
                SPView lstview = list.DefaultView;
                XsltListViewWebPart1.Toolbar = "";
                // XsltListViewWebPart1.ViewGuid = list.DefaultView.ID.ToString("B").ToString();
                StringBuilder xml = new StringBuilder();
                xml.Append("<View Name='{" + list.DefaultView.ID.ToString("B").ToString() + "}' MobileView='TRUE' Type='HTML'  Hidden='TRUE' DisplayName='' Url='" + Request.Url.ToString() + "' Level='1' BaseViewID='1' ContentTypeID='0x' ImageUrl='/_layouts/images/generic.png'>");
                xml.Append("<Query><OrderBy><FieldRef Name='ID'/></OrderBy></Query>");
                xml.Append("<ViewFields> <FieldRef Name='LinkTitle'/><FieldRef Name='IsActive'/></ViewFields>");
                xml.Append("<RowLimit Paged='TRUE'>30</RowLimit>");
                xml.Append("<Aggregations Value='Off'/>");
                xml.Append("<Toolbar Type='Freeform'/></View>");
                //  documentsWP.XmlDefinition = xml.ToString();
                XsltListViewWebPart1.XmlDefinition = xml.ToString();//lstview.GetViewXml();
                PlaceHolder1.Controls.Add(XsltListViewWebPart1);
            }
        }
1

How to create XsltListViewWebPart (XLV)

In order to create XLV for a custom view, XmlDefinition and ViewGuid properties should be specified:

private static WebPart CreateListViewWebPart(SPWeb web,string webPartTitle, string listUrl,string viewName)
{
      var list = web.GetList(SPUrlUtility.CombineUrl(web.ServerRelativeUrl, listUrl));
      var view = list.Views[viewName];
      var listView = new XsltListViewWebPart();
      listView.Title = webPartTitle;
      listView.ListUrl = listUrl;
      listView.ListId = list.ID;
      listView.ViewGuid = view.ID.ToString("B").ToUpper();
      listView.XmlDefinition = view.GetViewXml();
      listView.TitleUrl = view.ServerRelativeUrl;
      return listView; 
}

Your Answer

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

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