How to remove the Add New Item Option from a XsltListViewWebPart

Posted on

Unfortunately, XslListViewWebpart does not have a method to make it disappear, it has to be done using reflection, which of course is not supported.

I tried many ways I found on the forums and the only one that works fine is:

http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/a4b27730-dd81-4ff6-8003-a002a2fdec88

 

Here is my code:

#region Remove the toolbar.
MethodInfo ensureViewMethod = lvwpOrganisation.GetType().GetMethod(“EnsureView”, BindingFlags.Instance | BindingFlags.NonPublic);
object[] ensureViewParams = { };
ensureViewMethod.Invoke(lvwpOrganisation, ensureViewParams);
FieldInfo viewFieldInfo = lvwpOrganisation.GetType().GetField(“view”, BindingFlags.NonPublic | BindingFlags.Instance);
SPView spview = viewFieldInfo.GetValue(lvwpOrganisation) as SPView;
Type[] toolbarMethodParamTypes = { Type.GetType(“System.String”) };
MethodInfo setToolbarTypeMethod = spview.GetType().GetMethod(“SetToolbarType”, BindingFlags.Instance | BindingFlags.NonPublic, null, toolbarMethodParamTypes, null);
object[] setToolbarParam = { “None” }; //set the type here
setToolbarTypeMethod.Invoke(spview, setToolbarParam);
spview.Update();
#endregion

 

 

 

Advertisements

2 thoughts on “How to remove the Add New Item Option from a XsltListViewWebPart

    rinoleboffe said:
    February 4, 2014 at 11:08 am

    Hi,
    when i execute spview.Update();
    return this exception:
    Operation is not valid due to the current state of the object.
    And the toolbar not hide.
    Why?

    Thanks
    Rino

    Olaf Hengesbach said:
    May 28, 2015 at 2:03 pm

    In case someone stumbles upon this – there is an easier way: the toolbar is defined in the view XML which is accessible via GetViewXml(). It can be done in an almost-one-liner:

    selectedView.SetViewXml(selectedView.GetViewXml().Replace(“”, “”));
    selectedView.Update();

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s