Home > development, Microsoft SharePoint 2010, SharePoint 2010, Uncategorized > Enable Full Toolbar on an XSLT List View Web Part (XsltListViewWebPart) / on a list

Enable Full Toolbar on an XSLT List View Web Part (XsltListViewWebPart) / on a list

Enabling toolbar for a list programmatically means to enable it on xslt list view web part..Here’s the code:

private void EnableToolbarOnXsltListViewWP(string viewName, string listName, string webUrl)
        {
            using (var spSite = new SPSite(webUrl))
            {
                using (SPWeb web = spSite.OpenWeb())
                {
                    spSite.AllowUnsafeUpdates = true;
                    web.AllowUnsafeUpdates = true;
                    SPList spList = web.Lists[listName];
                    SPView spView = spList.Views[viewName];
                    SPLimitedWebPartManager spLimitedWebPartManager = web.GetLimitedWebPartManager(spView.Url, PersonalizationScope.Shared);
                    foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in spLimitedWebPartManager.WebParts)
                    {
                        if (webPart.GetType().Name.Equals("XsltListViewWebPart") && webPart.Title == Config.ListName_MyCVs)
                        {
                            try
                            {
                                MethodInfo ensureViewMethod = webPart.GetType().GetMethod("EnsureView", BindingFlags.Instance | BindingFlags.NonPublic);
                                object[] ensureViewParams = { };
                                ensureViewMethod.Invoke(webPart, ensureViewParams);
                                FieldInfo viewFieldInfo = webPart.GetType().GetField("view", BindingFlags.NonPublic | BindingFlags.Instance);
                                var view = viewFieldInfo.GetValue(webPart) as SPView;
                                Type[] toolbarMethodParamTypes = { Type.GetType("System.String") };
                                MethodInfo setToolbarTypeMethod = view.GetType().GetMethod("SetToolbarType", BindingFlags.Instance | BindingFlags.NonPublic, null, toolbarMethodParamTypes, null);
                                object[] setToolbarParam = { "ShowToolbar" };
                                setToolbarTypeMethod.Invoke(view, setToolbarParam);
                                view.Update();
                            }
                            catch (Exception ex)
                            {
                                Log.LogError("MyProjName", ex);
                            }
                        }
                    }
                    spSite.AllowUnsafeUpdates = false;
                }
            }
        }

Hope this helps-
Rehman.

About these ads
  1. October 29, 2012 at 4:09 pm

    Hi Rehman,

    the hiding of the toolbar works perfect for me thank you. Excellent code!!

    Kind regards
    Stefan

  2. Hannes
    November 4, 2012 at 9:12 pm

    This approach doesnt seem to work without problems. Running this code with users that do not have owner rights causes an “access denied” error when (for example) clicking on the column headings for sorting… Any ideas?

  3. April 3, 2013 at 10:53 am

    Thanks Rehman

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

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

Follow

Get every new post delivered to your Inbox.

Join 37 other followers

%d bloggers like this: