Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

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.in/2012/08/programatically-add-xsltlist-view.html

share|improve this answer
    
Just tried the view method and that works perfectly. Also +1 for the link :) –  Spongeroberto Squarepantalones Nov 14 '12 at 16:14
add comment

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; 
}
share|improve this answer
add comment

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);
            }
        }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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